1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Small\CodeGeneration\Creation\Src\Entity\Embeddable\Traits; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Embeddable\Traits\HasEmbeddableCreator; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FileFactory; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FindReplaceFactory; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File\Writer; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Config; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Small\ConfigTest; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
class HasEmbeddableCreatorTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
private const EXPECTED_FILE = <<<'PHP' |
17
|
|
|
<?php declare(strict_types=1); |
18
|
|
|
|
19
|
|
|
namespace Test\Project\Entity\Embeddable\Traits\Foo; |
20
|
|
|
|
21
|
|
|
use Doctrine\ORM\Events; |
22
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
23
|
|
|
use Test\Project\Entity\Embeddable\Interfaces\Foo\HasBarEmbeddableInterface; |
24
|
|
|
use Test\Project\Entity\Embeddable\Interfaces\Objects\Foo\BarEmbeddableInterface; |
25
|
|
|
use Test\Project\Entity\Embeddable\Objects\Foo\BarEmbeddable; |
26
|
|
|
|
27
|
|
|
trait HasBarEmbeddable |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var BarEmbeddableInterface |
31
|
|
|
*/ |
32
|
|
|
private $barEmbeddable; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ClassMetadataBuilder $builder |
36
|
|
|
*/ |
37
|
|
|
protected static function metaForBarEmbeddable(ClassMetadataBuilder $builder): void |
38
|
|
|
{ |
39
|
|
|
$builder->addLifecycleEvent( |
40
|
|
|
'postLoadSetOwningEntityOnBarEmbeddable', |
41
|
|
|
Events::postLoad |
42
|
|
|
); |
43
|
|
|
$builder->createEmbedded( |
44
|
|
|
HasBarEmbeddableInterface::PROP_BAR_EMBEDDABLE, |
45
|
|
|
BarEmbeddable::class |
46
|
|
|
) |
47
|
|
|
->setColumnPrefix( |
48
|
|
|
HasBarEmbeddableInterface::COLUMN_PREFIX_BAR |
49
|
|
|
) |
50
|
|
|
->build(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
|
|
public function getBarEmbeddable(): BarEmbeddableInterface |
57
|
|
|
{ |
58
|
|
|
return $this->barEmbeddable; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function postLoadSetOwningEntityOnBarEmbeddable(): void |
62
|
|
|
{ |
63
|
|
|
$this->barEmbeddable->setOwningEntity($this); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Called at construction time |
68
|
|
|
*/ |
69
|
|
|
private function initBarEmbeddable(): void |
70
|
|
|
{ |
71
|
|
|
$this->setBarEmbeddable( |
72
|
|
|
new BarEmbeddable( |
73
|
|
|
BarEmbeddableInterface::DEFAULT_PROPERTY_ONE, |
74
|
|
|
BarEmbeddableInterface::DEFAULT_PROPERTY_TWO |
75
|
|
|
), |
76
|
|
|
false |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param BarEmbeddable $barEmbeddable |
82
|
|
|
* |
83
|
|
|
* @param bool $notify |
84
|
|
|
* |
85
|
|
|
* @return $this |
86
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
87
|
|
|
*/ |
88
|
|
|
private function setBarEmbeddable(BarEmbeddable $barEmbeddable, bool $notify = true): self |
89
|
|
|
{ |
90
|
|
|
$this->barEmbeddable = $barEmbeddable; |
91
|
|
|
$this->barEmbeddable->setOwningEntity($this); |
92
|
|
|
if (true === $notify) { |
93
|
|
|
$this->notifyEmbeddablePrefixedProperties( |
94
|
|
|
HasBarEmbeddableInterface::PROP_BAR_EMBEDDABLE |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
PHP; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @test |
105
|
|
|
*/ |
106
|
|
|
public function itCanCreateTheFile(): void |
107
|
|
|
{ |
108
|
|
|
$file = $this->getCreator() |
109
|
|
|
->setCatName('Foo') |
110
|
|
|
->setName('Bar') |
111
|
|
|
->createTargetFileObject() |
112
|
|
|
->getTargetFile(); |
113
|
|
|
$expected = self::EXPECTED_FILE; |
114
|
|
|
$actual = $file->getContents(); |
115
|
|
|
self::assertSame($expected, $actual); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function getCreator(): HasEmbeddableCreator |
119
|
|
|
{ |
120
|
|
|
$namespaceHelper = new NamespaceHelper(); |
121
|
|
|
$config = new Config(ConfigTest::SERVER); |
122
|
|
|
|
123
|
|
|
$creator = new HasEmbeddableCreator( |
124
|
|
|
new FileFactory($namespaceHelper, $config), |
125
|
|
|
$namespaceHelper, |
126
|
|
|
new Writer(), |
127
|
|
|
$config, |
128
|
|
|
new FindReplaceFactory() |
129
|
|
|
); |
130
|
|
|
$creator->setProjectRootNamespace('Test\Project'); |
131
|
|
|
|
132
|
|
|
return $creator; |
133
|
|
|
} |
134
|
|
|
} |