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