From 6534191d59521e1e5a88638b1c6a1c77f74dd12a Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Thu, 31 Oct 2019 20:02:34 +0100 Subject: Migrate news to model class --- tests/Unit/Models/News/NewsTest.php | 80 +++++++++++++++++++++++++++++++++++++ tests/Unit/Models/User/UserTest.php | 58 +++++++++++++++++++++++++-- 2 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 tests/Unit/Models/News/NewsTest.php (limited to 'tests') diff --git a/tests/Unit/Models/News/NewsTest.php b/tests/Unit/Models/News/NewsTest.php new file mode 100644 index 00000000..7309c0b0 --- /dev/null +++ b/tests/Unit/Models/News/NewsTest.php @@ -0,0 +1,80 @@ +initDatabase(); + + $this->user = User::make([ + 'name' => 'lorem', + 'password' => '', + 'email' => 'foo@bar.batz', + 'api_key' => '', + ]); + $this->user->save(); + + $this->newsData = [ + 'title' => 'test title', + 'text' => 'test text', + 'user_id' => $this->user->id + ]; + } + + /** + * Tests that creating a News item with default values works. + * + * @return void + */ + public function testCreateDefault(): void + { + $news = News::create($this->newsData); + + $this->assertSame(1, $news->id); + $this->assertSame($this->newsData['title'], $news->title); + $this->assertSame($this->newsData['text'], $news->text); + $this->assertFalse($news->is_meeting); + } + + /** + * Tests that creating a News item with all fill values works. + * + * @return void + */ + public function testCreate(): void + { + $news = News::create( + $this->newsData + ['is_meeting' => true,] + ); + + $this->assertSame(1, $news->id); + $this->assertSame($this->newsData['title'], $news->title); + $this->assertSame($this->newsData['text'], $news->text); + $this->assertTrue($news->is_meeting); + } +} diff --git a/tests/Unit/Models/User/UserTest.php b/tests/Unit/Models/User/UserTest.php index b89f832b..96c2c1b7 100644 --- a/tests/Unit/Models/User/UserTest.php +++ b/tests/Unit/Models/User/UserTest.php @@ -3,6 +3,7 @@ namespace Engelsystem\Test\Unit\Models; use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; +use Engelsystem\Models\News\News; use Engelsystem\Models\User\Contact; use Engelsystem\Models\User\HasUserModel; use Engelsystem\Models\User\PersonalData; @@ -25,6 +26,15 @@ class UserTest extends TestCase 'api_key' => '', ]; + /** + * Prepare test + */ + protected function setUp(): void + { + parent::setUp(); + $this->initDatabase(); + } + /** * @return array */ @@ -93,11 +103,51 @@ class UserTest extends TestCase } /** - * Prepare test + * @covers User::news() + * + * @dataProvider hasManyRelationsProvider + * + * @param string $class Class name of the related models + * @param string $name Name of the accessor for the related models + * @param array $data List of the related models */ - protected function setUp(): void + public function testHasManyRelations(string $class, string $name, array $data): void { - parent::setUp(); - $this->initDatabase(); + $user = new User($this->data); + $user->save(); + + $relatedModelIds = []; + + foreach ($data as $d) { + $stored = $class::create($d + ['user_id' => $user->id]); + $relatedModelIds[] = $stored->id; + } + + $this->assertEquals($relatedModelIds, $user->{$name}->modelKeys()); + } + + /** + * @return array + */ + public function hasManyRelationsProvider(): array + { + return [ + 'news' => [ + News::class, + 'news', + [ + [ + 'title' => 'Hey hoo', + 'text' => 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.', + 'is_meeting' => false, + ], + [ + 'title' => 'Huuhuuu', + 'text' => 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.', + 'is_meeting' => true, + ], + ] + ] + ]; } } -- cgit v1.2.3-54-g00ecf