summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMichael Weimann <mail@michael-weimann.eu>2019-10-31 20:02:34 +0100
committerIgor Scheller <igor.scheller@igorshp.de>2019-11-10 22:10:11 +0100
commit6534191d59521e1e5a88638b1c6a1c77f74dd12a (patch)
treed381a16b7287c2ff3cff814a793ebc27bdeb4eac /tests
parent8b5b9e2c65193171a12bf99a8df860a66e6900a5 (diff)
Migrate news to model class
Diffstat (limited to 'tests')
-rw-r--r--tests/Unit/Models/News/NewsTest.php80
-rw-r--r--tests/Unit/Models/User/UserTest.php58
2 files changed, 134 insertions, 4 deletions
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 @@
+<?php
+declare(strict_types=1);
+
+use Engelsystem\Models\News\News;
+use Engelsystem\Models\User\User;
+use Engelsystem\Test\Unit\HasDatabase;
+use Engelsystem\Test\Unit\TestCase;
+
+/**
+ * This class provides tests for the News model.
+ */
+class NewsTest extends TestCase
+{
+ use HasDatabase;
+
+ /**
+ * @var array
+ */
+ private $newsData;
+
+ /**
+ * @var User
+ */
+ private $user;
+
+ /**
+ * @return void
+ */
+ protected function setUp(): void
+ {
+ parent::setUp();
+ $this->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;
@@ -26,6 +27,15 @@ class UserTest extends TestCase
];
/**
+ * Prepare test
+ */
+ protected function setUp(): void
+ {
+ parent::setUp();
+ $this->initDatabase();
+ }
+
+ /**
* @return array
*/
public function hasOneRelationsProvider()
@@ -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,
+ ],
+ ]
+ ]
+ ];
}
}