1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Small\CodeGeneration\Creation\Src\Entity\Embeddable\Objects; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Embeddable\Objects\EmbeddableCreator; |
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\Objects\EmbeddableCreator |
17
|
|
|
*/ |
18
|
|
|
class EmbeddableCreatorTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
private const EXPECTED_FILE = <<<'PHP' |
21
|
|
|
<?php declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace Test\Project\Entity\Embeddable\Objects\Foo; |
24
|
|
|
|
25
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
26
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Objects\AbstractEmbeddableObject; |
27
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
28
|
|
|
use Test\Project\Entity\Embeddable\Interfaces\Foo\HasBarEmbeddableInterface; |
29
|
|
|
use Test\Project\Entity\Embeddable\Interfaces\Objects\Foo\BarEmbeddableInterface; |
30
|
|
|
|
31
|
|
|
class BarEmbeddable extends AbstractEmbeddableObject implements BarEmbeddableInterface |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $propertyOne; |
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $propertyTwo; |
41
|
|
|
|
42
|
|
|
public function __construct(string $propertyOne, string $propertyTwo) |
43
|
|
|
{ |
44
|
|
|
$this->validate($propertyOne, $propertyTwo); |
45
|
|
|
$this->propertyOne = $propertyOne; |
46
|
|
|
$this->propertyTwo = $propertyTwo; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function validate(string $propertyOne, string $propertyTwo): void |
50
|
|
|
{ |
51
|
|
|
$errors = []; |
52
|
|
|
if ('' === $propertyOne) { |
53
|
|
|
$errors[] = 'property one is empty'; |
54
|
|
|
} |
55
|
|
|
if ('' === $propertyTwo) { |
56
|
|
|
$errors[] = 'property two is empty'; |
57
|
|
|
} |
58
|
|
|
if ([] === $errors) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
throw new \InvalidArgumentException('Invalid arguments: ' . print_r($errors, true)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param ClassMetadata $metadata |
66
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
67
|
|
|
*/ |
68
|
|
|
public static function loadMetadata(ClassMetadata $metadata): void |
69
|
|
|
{ |
70
|
|
|
$builder = self::setEmbeddableAndGetBuilder($metadata); |
71
|
|
|
MappingHelper::setSimpleFields( |
72
|
|
|
[ |
73
|
|
|
BarEmbeddableInterface::EMBEDDED_PROP_PROPERTY_ONE => MappingHelper::TYPE_STRING, |
74
|
|
|
BarEmbeddableInterface::EMBEDDED_PROP_PROPERTY_TWO => MappingHelper::TYPE_STRING, |
75
|
|
|
], |
76
|
|
|
$builder |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param array $properties |
82
|
|
|
* |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public static function create(array $properties): BarEmbeddableInterface |
86
|
|
|
{ |
87
|
|
|
if (array_key_exists(BarEmbeddableInterface::EMBEDDED_PROP_PROPERTY_ONE, $properties)) { |
88
|
|
|
return new self( |
89
|
|
|
$properties[BarEmbeddableInterface::EMBEDDED_PROP_PROPERTY_ONE], |
90
|
|
|
$properties[BarEmbeddableInterface::EMBEDDED_PROP_PROPERTY_TWO] |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return new self(...array_values($properties)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function __toString(): string |
98
|
|
|
{ |
99
|
|
|
return (string)print_r( |
100
|
|
|
[ |
101
|
|
|
'barEmbeddable' => [ |
102
|
|
|
BarEmbeddableInterface::EMBEDDED_PROP_PROPERTY_ONE => $this->getPropertyOne(), |
103
|
|
|
BarEmbeddableInterface::EMBEDDED_PROP_PROPERTY_TWO => $this->getPropertyTwo(), |
104
|
|
|
], |
105
|
|
|
], |
106
|
|
|
true |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getPropertyOne(): string |
114
|
|
|
{ |
115
|
|
|
return $this->propertyOne; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getPropertyTwo(): string |
122
|
|
|
{ |
123
|
|
|
return $this->propertyTwo; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function getPrefix(): string |
127
|
|
|
{ |
128
|
|
|
return HasBarEmbeddableInterface::PROP_BAR_EMBEDDABLE; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
PHP; |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @test |
136
|
|
|
*/ |
137
|
|
|
public function itCanCreateTheFile(): void |
138
|
|
|
{ |
139
|
|
|
$file = $this->getCreator() |
140
|
|
|
->setCatName('Foo') |
141
|
|
|
->setName('Bar') |
142
|
|
|
->createTargetFileObject() |
143
|
|
|
->getTargetFile(); |
144
|
|
|
$expected = self::EXPECTED_FILE; |
145
|
|
|
$actual = $file->getContents(); |
146
|
|
|
self::assertSame($expected, $actual); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
private function getCreator(): EmbeddableCreator |
150
|
|
|
{ |
151
|
|
|
$namespaceHelper = new NamespaceHelper(); |
152
|
|
|
$config = new Config(ConfigTest::SERVER); |
153
|
|
|
|
154
|
|
|
$creator = new EmbeddableCreator( |
155
|
|
|
new FileFactory($namespaceHelper, $config), |
156
|
|
|
$namespaceHelper, |
157
|
|
|
new Writer(), |
158
|
|
|
$config, |
159
|
|
|
new FindReplaceFactory() |
160
|
|
|
); |
161
|
|
|
$creator->setProjectRootNamespace('Test\Project'); |
162
|
|
|
|
163
|
|
|
return $creator; |
164
|
|
|
} |
165
|
|
|
} |