| 1 |
<?php |
| 2 |
|
| 3 |
namespace QuadLayers\WP_Orm\Tests\Collection\Mapper; |
| 4 |
|
| 5 |
use PHPUnit\Framework\TestCase; |
| 6 |
use QuadLayers\WP_Orm\Entity\CollectionEntity; |
| 7 |
use QuadLayers\WP_Orm\Entity\EntityInterface; |
| 8 |
use QuadLayers\WP_Orm\Factory\CollectionFactory; |
| 9 |
use QuadLayers\WP_Orm\Mapper\CollectionMapper; |
| 10 |
use QuadLayers\WP_Orm\Tests\Collection\CollectionEntityTest; |
| 11 |
|
| 12 |
class CollectionMapperTest extends TestCase |
| 13 |
{ |
| 14 |
private array $testValue; |
| 15 |
private EntityInterface $entity; |
| 16 |
private CollectionFactory $factory; |
| 17 |
private CollectionMapper $mapper; |
| 18 |
|
| 19 |
protected function setUp(): void |
| 20 |
{ |
| 21 |
|
| 22 |
$entity = new CollectionEntityTest(); |
| 23 |
|
| 24 |
$this->testValue = $entity->getDefaults(); |
| 25 |
|
| 26 |
$this->factory = new CollectionFactory('\QuadLayers\WP_Orm\Tests\Collection\CollectionEntityTest'); |
| 27 |
$this->mapper = new CollectionMapper($this->factory); |
| 28 |
$this->entity = $this->mapper->toEntity($this->testValue); |
| 29 |
} |
| 30 |
|
| 31 |
public function testToEntity() |
| 32 |
{ |
| 33 |
|
| 34 |
$entity = $this->mapper->toEntity($this->testValue); |
| 35 |
|
| 36 |
$this->assertInstanceOf(CollectionEntity::class, $entity); |
| 37 |
$this->assertEquals($this->testValue['key1'], $entity->getKey1()); |
| 38 |
$this->assertEquals($this->testValue['key2'], $entity->getKey2()); |
| 39 |
} |
| 40 |
|
| 41 |
public function testToArray() |
| 42 |
{ |
| 43 |
|
| 44 |
// Then we convert the entity back to an array |
| 45 |
$array = $this->mapper->toArray($this->entity); |
| 46 |
|
| 47 |
// Check if the original array and the result are the same |
| 48 |
$this->assertEquals(['id' => 0], $array); |
| 49 |
} |
| 50 |
|
| 51 |
public function testEntityHasDefaultAttributes() |
| 52 |
{ |
| 53 |
|
| 54 |
$entity = $this->mapper->toEntity([]); |
| 55 |
|
| 56 |
$this->assertEquals($this->testValue['key1'], $entity->getKey1()); |
| 57 |
$this->assertEquals($this->testValue['key2'], $entity->getKey2()); |
| 58 |
} |
| 59 |
} |
| 60 |
|