Skip to content

Commit

Permalink
#72 WIP HasMetasTest
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitriBouteille committed Sep 28, 2024
1 parent 6fa7000 commit b43edde
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions tests/WordPress/Concerns/HasMetasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class HasMetasTest extends TestCase
public function testGetMeta(): void
{
$model = new Post();
$model->setPostTitle('Hello world');
$model->setPostTitle(__FUNCTION__);
$model->save();
$createMeta = $model->setMeta('author', 'Norman FOSTER');

Expand All @@ -45,7 +45,7 @@ public function testGetMeta(): void
public function testSetMeta(): void
{
$model = new Post();
$model->setPostTitle('Hello world');
$model->setPostTitle(__FUNCTION__);
$model->save();
$meta = $model->setMeta('build-by', 'John D.');

Expand All @@ -63,7 +63,7 @@ public function testSetMeta(): void
public function testHasMeta(): void
{
$model = new Post();
$model->setPostTitle('Hello world');
$model->setPostTitle(__FUNCTION__);
$model->save();

$model->setMeta('birthday-date', '17/09/1900');
Expand All @@ -82,7 +82,7 @@ public function testHasMeta(): void
public function testGetMetaValueWithoutCast(): void
{
$model = new Post();
$model->setPostTitle('Hello world');
$model->setPostTitle(__FUNCTION__);

$model->save();
$model->setMeta('build-by', 'John D.');
Expand All @@ -107,7 +107,7 @@ public function testGetMetaValueWithGenericCasts(): void
};

$model = new $object();
$model->setPostTitle('Hello world');
$model->setPostTitle(__FUNCTION__);
$model->save();
$model->setMeta('age', '18');
$model->setMeta('year', '2024');
Expand All @@ -133,7 +133,7 @@ public function testGetMetaValueWithEnumCasts(): void
};

$model = new $object();
$model->setPostTitle('Hello world');
$model->setPostTitle(__FUNCTION__);
$model->save();
$model->setMeta('active', 'yes');

Expand All @@ -158,7 +158,7 @@ public function testGetMetaValueWithDatetimeCasts(): void
};

$model = new $object();
$model->setPostTitle('Hello world');
$model->setPostTitle(__FUNCTION__);
$model->save();
$model->setMeta('created_at', '2022-09-08 07:30:05');
$model->setMeta('uploaded_at', '2024-10-08 10:25:35');
Expand All @@ -174,19 +174,54 @@ public function testGetMetaValueWithDatetimeCasts(): void
$this->assertEquals('2024-10-08 00:00:00', $date->format('Y-m-d H:i:s'), 'The time must be reset to 00:00:00.');
}

/**
* @return void
* @covers HasMetas::getMetaValue
*/
public function testGetMetaValueWithInvalidCasts(): void
{
$object = new class () extends Post {
protected array $metaCasts = [
'my_meta' => 'boolean_',
];
};

$model = new $object();
$model->setPostTitle(__FUNCTION__);
$model->save();
$model->setMeta('my_meta', 'yes');

$this->assertEquals('yes', $model->getMetaValue('my_meta'));
}

/**
* @return void
* @covers HasMetas::deleteMeta
*/
public function testDeleteMeta(): void
{
$model = new Post();
$model->setPostTitle('Hello world');
$model->setPostTitle(__FUNCTION__);
$model->save();

$model->setMeta('architect-name', 'Norman F.');

$this->assertEquals(1, $model->deleteMeta('architect-name'), 'The function must delete only one line.');
$this->assertFalse($model->hasMeta('architect-name'), 'The meta must no longer exist.');
}

/**
* @return void
* @covers HasMetas::deleteMeta
*/
public function testDeleteUndefinedMeta(): void
{
$model = new Post();
$model->setPostTitle(__FUNCTION__);
$model->save();

$model->setMeta('architect-name', 'Norman F.');

$this->assertEquals(0, $model->deleteMeta('fake-meta'));
}
}

0 comments on commit b43edde

Please sign in to comment.