summaryrefslogtreecommitdiff
path: root/tests/Unit/Models/QuestionTest.php
blob: 9207da6e297104954ef34b0f3f632a3c7d65f19a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php

declare(strict_types=1);

namespace Unit\Models;

use Engelsystem\Models\Question;
use Engelsystem\Models\User\User;
use Engelsystem\Test\Unit\HasDatabase;
use Engelsystem\Test\Unit\TestCase;
use Illuminate\Support\Str;

class QuestionTest extends TestCase
{
    use HasDatabase;

    /**
     * @var User
     */
    private $user1;

    /**
     * @var User
     */
    private $user2;

    /**
     * @return void
     */
    protected function setUp(): void
    {
        parent::setUp();
        $this->initDatabase();

        $this->user1 = User::create(
            [
                'name'     => 'user1',
                'password' => '',
                'email'    => 'user1@example.com',
                'api_key'  => '',
            ]
        );

        $this->user2 = User::create(
            [
                'name'     => 'user2',
                'password' => '',
                'email'    => 'user2@example.com',
                'api_key'  => '',
            ]
        );
    }

    /**
     * @covers \Engelsystem\Models\Question::answerer
     */
    public function testAnswerer(): void
    {
        $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);
    }

    /**
     * @covers \Engelsystem\Models\Question::unanswered
     */
    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);
    }

    /**
     * @covers \Engelsystem\Models\Question::answered
     */
    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);
    }

    /**
     * @param User      $user
     * @param User|null $answerer
     * @return Question
     */
    private function createQuestion(User $user, ?User $answerer = null): Question
    {
        $data = [
            'user_id' => $user->id,
            'text'    => Str::random(),
        ];

        if ($answerer !== null) {
            $data += [
                'answerer_id' => $answerer->id,
                'answer'      => Str::random(),
            ];
        }

        return Question::create($data);
    }
}