diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2019-12-07 21:14:08 +0100 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2019-12-07 21:29:11 +0100 |
commit | d6cb9c6258c52bf3874bab2ceec97b90591032d1 (patch) | |
tree | 782b9c167230c20b652ad8fd39bb8b993ee1f857 | |
parent | 8fc159f2872a661c83d2a0bd1f4f68f49c33ae8c (diff) |
Questions: Moved user tests to user and fixed attribute names, added @covers
-rw-r--r-- | includes/pages/user_questions.php | 4 | ||||
-rw-r--r-- | tests/Unit/Models/QuestionTest.php | 61 | ||||
-rw-r--r-- | tests/Unit/Models/User/UserTest.php | 45 |
3 files changed, 54 insertions, 56 deletions
diff --git a/includes/pages/user_questions.php b/includes/pages/user_questions.php index ab836c77..13f58b10 100644 --- a/includes/pages/user_questions.php +++ b/includes/pages/user_questions.php @@ -33,8 +33,8 @@ function user_questions() $question = request()->get('question'); if (!empty($question) && $request->hasPostData('submit')) { Question::create([ - 'enquirer_id' => $user->id, - 'question' => $question, + 'user_id' => $user->id, + 'text' => $question, ]); success(__('You question was saved.')); diff --git a/tests/Unit/Models/QuestionTest.php b/tests/Unit/Models/QuestionTest.php index 3ddfb8c1..9207da6e 100644 --- a/tests/Unit/Models/QuestionTest.php +++ b/tests/Unit/Models/QuestionTest.php @@ -52,22 +52,9 @@ class QuestionTest extends TestCase } /** - * @return void + * @covers \Engelsystem\Models\Question::answerer */ - public function testStoreLoadUnAnsweredQuestion(): void - { - $question = $this->createQuestion($this->user1); - $loadedQuestion = Question::find($question->id); - - $this->assertSame($this->user1->id, $loadedQuestion->user->id); - $this->assertSame($this->user1->id, $loadedQuestion->user_id); - $this->assertSame($question->text, $loadedQuestion->text); - } - - /** - * @return void - */ - public function testStoreLoadAnsweredQuestion(): void + public function testAnswerer(): void { $question = $this->createQuestion($this->user1, $this->user2); $loadedQuestion = Question::find($question->id); @@ -81,41 +68,7 @@ class QuestionTest extends TestCase } /** - * @return void - */ - public function testUserQuestionsAsked(): void - { - $question1 = $this->createQuestion($this->user1); - $question2 = $this->createQuestion($this->user1); - // create some questions asked by user 2 to test the correct assignment - $this->createQuestion($this->user2); - $this->createQuestion($this->user2); - - $user1QuestionIds = $this->user1->questionsAsked()->pluck('id')->toArray(); - $this->assertCount(2, $user1QuestionIds); - $this->assertContains($question1->id, $user1QuestionIds); - $this->assertContains($question2->id, $user1QuestionIds); - } - - /** - * @return void - */ - public function testUserQuestionsAnswered(): void - { - $question1 = $this->createQuestion($this->user1, $this->user2); - $question2 = $this->createQuestion($this->user1, $this->user2); - // create some questions answered by user 1 to test the correct assignment - $this->createQuestion($this->user2, $this->user1); - $this->createQuestion($this->user2, $this->user1); - - $user2Answers = $this->user2->questionsAnswered()->pluck('id')->toArray(); - $this->assertCount(2, $user2Answers); - $this->assertContains($question1->id, $user2Answers); - $this->assertContains($question2->id, $user2Answers); - } - - /** - * @return void + * @covers \Engelsystem\Models\Question::unanswered */ public function testUnanswered(): void { @@ -132,7 +85,7 @@ class QuestionTest extends TestCase } /** - * @return void + * @covers \Engelsystem\Models\Question::answered */ public function testAnswered(): void { @@ -149,14 +102,14 @@ class QuestionTest extends TestCase } /** - * @param User $enquirer + * @param User $user * @param User|null $answerer * @return Question */ - private function createQuestion(User $enquirer, ?User $answerer = null): Question + private function createQuestion(User $user, ?User $answerer = null): Question { $data = [ - 'user_id' => $enquirer->id, + 'user_id' => $user->id, 'text' => Str::random(), ]; diff --git a/tests/Unit/Models/User/UserTest.php b/tests/Unit/Models/User/UserTest.php index 5cb8745d..fd8e2396 100644 --- a/tests/Unit/Models/User/UserTest.php +++ b/tests/Unit/Models/User/UserTest.php @@ -5,6 +5,7 @@ namespace Engelsystem\Test\Unit\Models; use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; use Engelsystem\Models\BaseModel; use Engelsystem\Models\News; +use Engelsystem\Models\Question; use Engelsystem\Models\User\Contact; use Engelsystem\Models\User\HasUserModel; use Engelsystem\Models\User\PersonalData; @@ -14,6 +15,7 @@ use Engelsystem\Models\User\User; use Engelsystem\Test\Unit\HasDatabase; use Engelsystem\Test\Unit\TestCase; use Exception; +use Illuminate\Support\Str; class UserTest extends TestCase { @@ -147,6 +149,49 @@ class UserTest extends TestCase } /** + * @covers \Engelsystem\Models\User\User::questionsAsked + */ + public function testQuestionsAsked(): void + { + ($user = new User($this->data))->save(); + ($user2 = new User(array_merge($this->data, ['name' => 'dolor', 'email' => 'dolor@bar.batz'])))->save(); + + ($question1 = new Question(['user_id' => $user->id, 'text' => Str::random()]))->save(); + ($question2 = new Question(['user_id' => $user->id, 'text' => Str::random()]))->save(); + // create some questions asked by user 2 to test the correct assignment + (new Question(['user_id' => $user2->id, 'text' => Str::random()]))->save(); + (new Question(['user_id' => $user2->id, 'text' => Str::random()]))->save(); + + $questionIds = $user->questionsAsked()->pluck('id')->toArray(); + $this->assertCount(2, $questionIds); + $this->assertContains($question1->id, $questionIds); + $this->assertContains($question2->id, $questionIds); + } + + /** + * @covers \Engelsystem\Models\User\User::questionsAnswered + */ + public function testQuestionsAnswered(): void + { + ($user = new User($this->data))->save(); + ($user2 = new User(array_merge($this->data, ['name' => 'dolor', 'email' => 'dolor@bar.batz'])))->save(); + + $questionData = ['user_id' => $user->id, 'text' => Str::random()]; + $answerData = ['answerer_id' => $user2->id, 'answer' => Str::random()]; + ($question1 = new Question(array_merge($questionData, $answerData)))->save(); + ($question2 = new Question(array_merge($questionData, $answerData)))->save(); + // Create some questions asked by user 2 to test the correct assignment + (new Question(array_merge($questionData, $answerData, ['answerer_id' => $user->id])))->save(); + (new Question($questionData))->save(); + (new Question($questionData))->save(); + + $answers = $user2->questionsAnswered()->pluck('id')->toArray(); + $this->assertCount(2, $answers); + $this->assertContains($question1->id, $answers); + $this->assertContains($question2->id, $answers); + } + + /** * Prepare test */ protected function setUp(): void |