From 4f63bbbaacd0db3636f1169f5ad29f1cfb21615b Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Tue, 3 Dec 2019 20:09:37 +0100 Subject: Add Question model --- src/Models/Question.php | 64 ++++++++++++++ src/Models/User/User.php | 22 +++++ tests/Unit/Models/QuestionTest.php | 172 +++++++++++++++++++++++++++++++++++++ 3 files changed, 258 insertions(+) create mode 100644 src/Models/Question.php create mode 100644 tests/Unit/Models/QuestionTest.php diff --git a/src/Models/Question.php b/src/Models/Question.php new file mode 100644 index 00000000..eccf7fe6 --- /dev/null +++ b/src/Models/Question.php @@ -0,0 +1,64 @@ + 'integer', + 'answerer_id' => 'integer', + ]; + + /** + * @return BelongsTo + */ + public function answerer(): BelongsTo + { + return $this->belongsTo(User::class, 'answerer_id'); + } + + /** + * @return Builder + */ + public static function unanswered(): Builder + { + return static::whereAnswererId(null); + } + + /** + * @return Builder + */ + public static function answered(): Builder + { + return static::whereNotNull('answerer_id'); + } +} diff --git a/src/Models/User/User.php b/src/Models/User/User.php index 11d88505..e2ee9b21 100644 --- a/src/Models/User/User.php +++ b/src/Models/User/User.php @@ -6,6 +6,7 @@ use Carbon\Carbon; use Engelsystem\Models\BaseModel; use Engelsystem\Models\News; use Engelsystem\Models\NewsComment; +use Engelsystem\Models\Question; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasOne; @@ -38,6 +39,9 @@ use Illuminate\Database\Query\Builder as QueryBuilder; * @method static QueryBuilder|User[] whereLastLoginAt($value) * @method static QueryBuilder|User[] whereCreatedAt($value) * @method static QueryBuilder|User[] whereUpdatedAt($value) + * + * @property-read Collection|Question[] $questionsAsked + * @property-read Collection|Question[] $questionsAnswered */ class User extends BaseModel { @@ -119,4 +123,22 @@ class User extends BaseModel { return $this->hasMany(NewsComment::class); } + + /** + * @return HasMany + */ + public function questionsAsked(): HasMany + { + return $this->hasMany(Question::class, 'user_id') + ->where('user_id', $this->id); + } + + /** + * @return HasMany + */ + public function questionsAnswered(): HasMany + { + return $this->hasMany(Question::class, 'answerer_id') + ->where('answerer_id', $this->id); + } } diff --git a/tests/Unit/Models/QuestionTest.php b/tests/Unit/Models/QuestionTest.php new file mode 100644 index 00000000..3ddfb8c1 --- /dev/null +++ b/tests/Unit/Models/QuestionTest.php @@ -0,0 +1,172 @@ +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' => '', + ] + ); + } + + /** + * @return void + */ + 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 + { + $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); + } + + /** + * @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 + */ + 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); + } + + /** + * @return void + */ + 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 $enquirer + * @param User|null $answerer + * @return Question + */ + private function createQuestion(User $enquirer, ?User $answerer = null): Question + { + $data = [ + 'user_id' => $enquirer->id, + 'text' => Str::random(), + ]; + + if ($answerer !== null) { + $data += [ + 'answerer_id' => $answerer->id, + 'answer' => Str::random(), + ]; + } + + return Question::create($data); + } +} -- cgit v1.2.3