From b878740f80ce7cfe2a0bc53956e3f7e4e0aa2f78 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sun, 10 Nov 2019 21:30:26 +0100 Subject: News: Bug fixes, cleanup, comments & formatting Use more magically available methods and properties Fixed atom feed and stats not using the new model --- tests/Unit/Models/News/NewsTest.php | 80 ------------------------------------- tests/Unit/Models/NewsTest.php | 80 +++++++++++++++++++++++++++++++++++++ tests/Unit/Models/User/UserTest.php | 40 ++++++++++--------- 3 files changed, 102 insertions(+), 98 deletions(-) delete mode 100644 tests/Unit/Models/News/NewsTest.php create mode 100644 tests/Unit/Models/NewsTest.php (limited to 'tests/Unit/Models') diff --git a/tests/Unit/Models/News/NewsTest.php b/tests/Unit/Models/News/NewsTest.php deleted file mode 100644 index 7309c0b0..00000000 --- a/tests/Unit/Models/News/NewsTest.php +++ /dev/null @@ -1,80 +0,0 @@ -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/NewsTest.php b/tests/Unit/Models/NewsTest.php new file mode 100644 index 00000000..18ca2d9a --- /dev/null +++ b/tests/Unit/Models/NewsTest.php @@ -0,0 +1,80 @@ +initDatabase(); + + $this->user = (new User())->create([ + 'name' => 'lorem', + 'password' => '', + 'email' => 'foo@bar.batz', + 'api_key' => '', + ]); + + $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 = (new News())->create($this->newsData); + $news = $news->find($news->id); + + $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 = (new News())->create( + $this->newsData + ['is_meeting' => true] + ); + $news = $news->find($news->id); + + $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 96c2c1b7..da121a4f 100644 --- a/tests/Unit/Models/User/UserTest.php +++ b/tests/Unit/Models/User/UserTest.php @@ -3,7 +3,8 @@ namespace Engelsystem\Test\Unit\Models; use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; -use Engelsystem\Models\News\News; +use Engelsystem\Models\BaseModel; +use Engelsystem\Models\News; use Engelsystem\Models\User\Contact; use Engelsystem\Models\User\HasUserModel; use Engelsystem\Models\User\PersonalData; @@ -19,6 +20,7 @@ class UserTest extends TestCase use ArraySubsetAsserts; use HasDatabase; + /** @var string[] */ protected $data = [ 'name' => 'lorem', 'password' => '', @@ -26,15 +28,6 @@ class UserTest extends TestCase 'api_key' => '', ]; - /** - * Prepare test - */ - protected function setUp(): void - { - parent::setUp(); - $this->initDatabase(); - } - /** * @return array */ @@ -103,23 +96,25 @@ class UserTest extends TestCase } /** - * @covers User::news() + * @covers \Engelsystem\Models\User\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 + * @param string $class Class name of the related models + * @param string $name Name of the accessor for the related models + * @param array $modelData List of the related models */ - public function testHasManyRelations(string $class, string $name, array $data): void + public function testHasManyRelations(string $class, string $name, array $modelData): void { $user = new User($this->data); $user->save(); $relatedModelIds = []; - foreach ($data as $d) { - $stored = $class::create($d + ['user_id' => $user->id]); + foreach ($modelData as $data) { + /** @var BaseModel $model */ + $model = (new $class); + $stored = $model->create($data + ['user_id' => $user->id]); $relatedModelIds[] = $stored->id; } @@ -127,7 +122,7 @@ class UserTest extends TestCase } /** - * @return array + * @return array[] */ public function hasManyRelationsProvider(): array { @@ -150,4 +145,13 @@ class UserTest extends TestCase ] ]; } + + /** + * Prepare test + */ + protected function setUp(): void + { + parent::setUp(); + $this->initDatabase(); + } } -- cgit v1.2.3-70-g09d2