2019-12-03 20:09:37 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-04-19 20:41:38 +02:00
|
|
|
namespace Engelsystem\Test\Unit\Models;
|
2019-12-03 20:09:37 +01:00
|
|
|
|
2019-10-31 15:18:27 +01:00
|
|
|
use Carbon\Carbon;
|
2019-12-03 20:09:37 +01:00
|
|
|
use Engelsystem\Models\Question;
|
|
|
|
use Engelsystem\Models\User\User;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
2020-05-11 19:57:25 +02:00
|
|
|
class QuestionTest extends ModelTest
|
2019-12-03 20:09:37 +01:00
|
|
|
{
|
|
|
|
private User $user1;
|
|
|
|
|
|
|
|
private User $user2;
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2021-06-29 00:27:57 +02:00
|
|
|
$this->user1 = User::factory()->create();
|
|
|
|
$this->user2 = User::factory()->create();
|
2019-12-03 20:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-07 21:14:08 +01:00
|
|
|
* @covers \Engelsystem\Models\Question::answerer
|
2019-12-03 20:09:37 +01:00
|
|
|
*/
|
2019-12-07 21:14:08 +01:00
|
|
|
public function testAnswerer(): void
|
2019-12-03 20:09:37 +01:00
|
|
|
{
|
|
|
|
$question = $this->createQuestion($this->user1, $this->user2);
|
|
|
|
$loadedQuestion = Question::find($question->id);
|
|
|
|
|
|
|
|
$this->assertSame($this->user1->id, $loadedQuestion->user->id);
|
|
|
|
$this->assertSame($this->user1->id, $loadedQuestion->user_id);
|
|
|
|
$this->assertSame($loadedQuestion->text, $loadedQuestion->text);
|
|
|
|
$this->assertSame($this->user2->id, $loadedQuestion->answerer->id);
|
|
|
|
$this->assertSame($this->user2->id, $loadedQuestion->answerer_id);
|
|
|
|
$this->assertSame($loadedQuestion->answer, $loadedQuestion->answer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-07 21:14:08 +01:00
|
|
|
* @covers \Engelsystem\Models\Question::unanswered
|
2019-12-03 20:09:37 +01:00
|
|
|
*/
|
|
|
|
public function testUnanswered(): void
|
|
|
|
{
|
|
|
|
$question1 = $this->createQuestion($this->user1);
|
|
|
|
$question2 = $this->createQuestion($this->user1);
|
|
|
|
// create some answered questions as well
|
|
|
|
$this->createQuestion($this->user1, $this->user2);
|
|
|
|
$this->createQuestion($this->user1, $this->user2);
|
|
|
|
|
|
|
|
$unAnsweredQuestionIds = Question::unanswered()->pluck('id')->toArray();
|
|
|
|
$this->assertCount(2, $unAnsweredQuestionIds);
|
|
|
|
$this->assertContains($question1->id, $unAnsweredQuestionIds);
|
|
|
|
$this->assertContains($question2->id, $unAnsweredQuestionIds);
|
2019-10-31 15:18:27 +01:00
|
|
|
|
|
|
|
$this->assertNull(Question::find($question1->id)->answered_at);
|
2019-12-03 20:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-07 21:14:08 +01:00
|
|
|
* @covers \Engelsystem\Models\Question::answered
|
2019-12-03 20:09:37 +01:00
|
|
|
*/
|
|
|
|
public function testAnswered(): void
|
|
|
|
{
|
|
|
|
$question1 = $this->createQuestion($this->user1, $this->user2);
|
|
|
|
$question2 = $this->createQuestion($this->user1, $this->user2);
|
|
|
|
// create some unanswered questions as well
|
|
|
|
$this->createQuestion($this->user1);
|
|
|
|
$this->createQuestion($this->user1);
|
|
|
|
|
|
|
|
$answeredQuestionIds = Question::answered()->pluck('id')->toArray();
|
|
|
|
$this->assertCount(2, $answeredQuestionIds);
|
|
|
|
$this->assertContains($question1->id, $answeredQuestionIds);
|
|
|
|
$this->assertContains($question2->id, $answeredQuestionIds);
|
2019-10-31 15:18:27 +01:00
|
|
|
|
|
|
|
$this->assertInstanceOf(Carbon::class, Question::find($question1->id)->answered_at);
|
2019-12-03 20:09:37 +01:00
|
|
|
}
|
|
|
|
|
2019-12-07 21:14:08 +01:00
|
|
|
private function createQuestion(User $user, ?User $answerer = null): Question
|
2019-12-03 20:09:37 +01:00
|
|
|
{
|
|
|
|
$data = [
|
2019-12-07 21:14:08 +01:00
|
|
|
'user_id' => $user->id,
|
2019-12-03 20:09:37 +01:00
|
|
|
'text' => Str::random(),
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($answerer !== null) {
|
|
|
|
$data += [
|
|
|
|
'answerer_id' => $answerer->id,
|
|
|
|
'answer' => Str::random(),
|
2019-10-31 15:18:27 +01:00
|
|
|
'answered_at' => Carbon::now(),
|
2019-12-03 20:09:37 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return Question::create($data);
|
|
|
|
}
|
|
|
|
}
|