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