Skip to content

Commit

Permalink
add tests to create new empty entity
Browse files Browse the repository at this point in the history
  • Loading branch information
albertborsos committed Sep 16, 2019
1 parent 06e1dba commit 052b58f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
30 changes: 30 additions & 0 deletions tests/unit/hydrators/ActiveHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,34 @@ private function assertHydratedModel($modelClass, $data, Model $model): void
$this->assertEquals($expectedValue, $model->$attribute);
}
}


public function dataProviderHydrateEmptyEntity()
{
return [
'data array' => [Customer::class, ['id' => 'id', 'name' => 'name', 'customerAddresses' => 'customerAddresses'], []],
'model' => [Customer::class, ['id' => 'id', 'name' => 'name', 'customerAddresses' => 'customerAddresses'], new DynamicModel([])],
'model with relation data' => [Customer::class, ['id' => 'id', 'name' => 'name', 'customerAddresses' => 'customerAddresses'], new DynamicModel(['customerAddresses' => []])],
'model with relation model' => [Customer::class, ['id' => 'id', 'name' => 'name', 'customerAddresses' => 'customerAddresses'], new DynamicModel(['customerAddresses' => [
new DynamicModel([]),
]])],
];
}

/**
* @dataProvider dataProviderHydrateEmptyEntity
* @param $entityClass
* @param $map
* @param $data
* @throws \yii\base\InvalidConfigException
*/
public function testToCreateNewEmptyEntity($entityClass, $map, $data)
{
$hydrator = $this->mockHydrator($map);

/** @var EntityInterface $entity */
$entity = $hydrator->hydrate($entityClass, $data);

$this->assertHydratedEntity($entityClass, $entity, $data);
}
}
15 changes: 12 additions & 3 deletions tests/unit/repositories/AbstractRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
namespace albertborsos\ddd\tests\repositories;

use albertborsos\ddd\tests\support\base\infrastructure\interfaces\customer\CustomerRepositoryInterface;
use albertborsos\ddd\tests\support\base\infrastructure\db\customer\CustomerRepository;
use albertborsos\ddd\tests\support\base\infrastructure\db\customer\InvalidCustomerActiveRepository;
use albertborsos\ddd\tests\support\base\infrastructure\db\customer\InvalidEntityCustomerRepository;
use albertborsos\ddd\tests\support\base\infrastructure\db\customer\InvalidHydratorCustomerRepository;
use albertborsos\ddd\tests\support\base\MockConfig;
use albertborsos\ddd\tests\support\base\MockTrait;
use Codeception\PHPUnit\TestCase;

Expand Down Expand Up @@ -46,4 +43,16 @@ public function testInvalidHydratorClass()
{
new InvalidHydratorCustomerRepository();
}

public function testNewEntity()
{
/** @var CustomerRepositoryInterface $repository */
$repository = \Yii::createObject(CustomerRepositoryInterface::class);
$entity = $repository->newEntity();

$this->assertInstanceOf($repository->getEntityClass(), $entity);
foreach ($entity->fieldMapping() as $dataAttribute => $property) {
$this->assertNull($entity->{$property});
}
}
}

0 comments on commit 052b58f

Please sign in to comment.