1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Factory; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\AbstractIntegrationTest; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\String\EmailAddressFieldInterface; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\String\IsbnFieldInterface; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String\EmailAddressFieldTrait; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String\IsbnFieldTrait; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityValidatorFactory; |
11
|
|
|
|
12
|
|
|
class EntityFactoryTest extends AbstractIntegrationTest |
13
|
|
|
{ |
14
|
|
|
public const WORK_DIR = AbstractIntegrationTest::VAR_PATH . '/' . self::TEST_TYPE . '/EntityFactoryTest'; |
15
|
|
|
|
16
|
|
|
private const TEST_ENTITY_FQN = self::TEST_PROJECT_ROOT_NAMESPACE . '\\Entities\\TestEntity'; |
17
|
|
|
|
18
|
|
|
private $entityFqn; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var EntityFactory |
22
|
|
|
*/ |
23
|
|
|
private $factory; |
24
|
|
|
|
25
|
|
|
protected static $buildOnce = true; |
26
|
|
|
|
27
|
|
|
public function setup() |
28
|
|
|
{ |
29
|
|
|
parent::setup(); |
30
|
|
|
if (false === static::$built) { |
31
|
|
|
$this->buildOnce(); |
32
|
|
|
} |
33
|
|
|
$this->setupCopiedWorkDir(); |
34
|
|
|
$this->entityFqn = $this->getCopiedFqn(self::TEST_ENTITY_FQN); |
35
|
|
|
$this->factory = new EntityFactory($this->container->get(EntityValidatorFactory::class)); |
36
|
|
|
$this->factory->setEntityManager($this->getEntityManager()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function buildOnce() |
40
|
|
|
{ |
41
|
|
|
$this->getEntityGenerator()->generateEntity(self::TEST_ENTITY_FQN); |
42
|
|
|
$this->getFieldSetter()->setEntityHasField( |
43
|
|
|
self::TEST_ENTITY_FQN, |
44
|
|
|
IsbnFieldTrait::class |
45
|
|
|
); |
46
|
|
|
$this->getFieldSetter()->setEntityHasField( |
47
|
|
|
self::TEST_ENTITY_FQN, |
48
|
|
|
EmailAddressFieldTrait::class |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
static::$built = true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @test |
56
|
|
|
* |
57
|
|
|
*/ |
58
|
|
|
public function itCanCreateAnEmptyEntity(): void |
59
|
|
|
{ |
60
|
|
|
$entity = $this->factory->create($this->entityFqn); |
61
|
|
|
self::assertInstanceOf($this->entityFqn, $entity); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @test |
66
|
|
|
* |
67
|
|
|
*/ |
68
|
|
|
public function itThrowsAnExceptionIfThereIsAnInvalidProperty(): void |
69
|
|
|
{ |
70
|
|
|
$this->expectException(\InvalidArgumentException::class); |
71
|
|
|
$this->factory->create($this->entityFqn, ['invalidProperty' => true]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @test |
76
|
|
|
* |
77
|
|
|
*/ |
78
|
|
|
public function itCanCreateAnEntityWithValues(): void |
79
|
|
|
{ |
80
|
|
|
$this->markTestSkipped('This test is just failing for some weird reason'); |
81
|
|
|
$values = [ |
82
|
|
|
IsbnFieldInterface::PROP_ISBN => '978-3-16-148410-0', |
83
|
|
|
EmailAddressFieldInterface::PROP_EMAIL_ADDRESS => '[email protected]', |
84
|
|
|
]; |
85
|
|
|
$entity = $this->factory->create($this->entityFqn, $values); |
86
|
|
|
|
87
|
|
|
self::assertSame($entity->getIsbn(), $values[IsbnFieldInterface::PROP_ISBN]); |
88
|
|
|
|
89
|
|
|
self::assertSame($entity->getEmailAddress(), $values[EmailAddressFieldInterface::PROP_EMAIL_ADDRESS]); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|