1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Small\CodeGeneration\Creation\Src\Entity\Fields\Interfaces; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Fields\Interfaces\FieldInterfaceCreator; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FileFactory; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FindReplaceFactory; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File\Writer; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\TypeHelper; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Config; |
13
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
14
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Small\ConfigTest; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @small |
20
|
|
|
*/ |
21
|
|
|
class FieldInterfaceCreatorTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @test |
25
|
|
|
*/ |
26
|
|
|
public function itRequiresTheSuffix(): void |
27
|
|
|
{ |
28
|
|
|
$newObjectFqn = 'EdmondsCommerce\\DoctrineStaticMeta\\Entity\\Fields\\TestArray'; |
29
|
|
|
$this->expectException(InvalidArgumentException::class); |
30
|
|
|
$this->expectExceptionMessage('$newObjectFqn must end in FieldInterface'); |
31
|
|
|
$this->getCreator()->createTargetFileObject($newObjectFqn); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function getCreator(): FieldInterfaceCreator |
35
|
|
|
{ |
36
|
|
|
$namespaceHelper = new NamespaceHelper(); |
37
|
|
|
$config = new Config(ConfigTest::SERVER); |
38
|
|
|
|
39
|
|
|
return new FieldInterfaceCreator( |
40
|
|
|
new FileFactory($namespaceHelper, $config), |
41
|
|
|
$namespaceHelper, |
42
|
|
|
new Writer(), |
43
|
|
|
$config, |
44
|
|
|
new FindReplaceFactory(), |
45
|
|
|
new CodeHelper($namespaceHelper), |
46
|
|
|
new TypeHelper() |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function provideTypesToDefaultsAndExpectedText(): array |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
|
|
MappingHelper::TYPE_STRING => [MappingHelper::TYPE_STRING, 'foo', "'foo'"], |
54
|
|
|
MappingHelper::TYPE_BOOLEAN => [MappingHelper::TYPE_BOOLEAN, true, 'true'], |
55
|
|
|
MappingHelper::TYPE_FLOAT => [MappingHelper::TYPE_FLOAT, 2.2, '2.2'], |
56
|
|
|
MappingHelper::TYPE_FLOAT . '_int' => [MappingHelper::TYPE_FLOAT, 3, '3.0'], |
57
|
|
|
MappingHelper::TYPE_INTEGER => [MappingHelper::TYPE_INTEGER, 4, '4'], |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @test |
63
|
|
|
* @dataProvider provideTypesToDefaultsAndExpectedText |
64
|
|
|
* |
65
|
|
|
* @param string $type |
66
|
|
|
* @param mixed $default |
67
|
|
|
* @param string $match |
68
|
|
|
*/ |
69
|
|
|
public function itCanSetDefaultValues(string $type, $default, string $match): void |
70
|
|
|
{ |
71
|
|
|
$newObjectFqn = 'EdmondsCommerce\\DoctrineStaticMeta\\Entity\\Fields\\Traits\\Test' . ucfirst($type) . 'FieldInterface'; |
72
|
|
|
$contents = $this->getCreator() |
73
|
|
|
->setMappingHelperCommonType($type) |
74
|
|
|
->setDefaultValue($default) |
75
|
|
|
->createTargetFileObject($newObjectFqn) |
76
|
|
|
->getTargetFile() |
77
|
|
|
->getContents(); |
78
|
|
|
self::assertRegExp('%public const DEFAULT_.+? = ' . $match . '%', $contents); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @test |
83
|
|
|
*/ |
84
|
|
|
public function itCanCreateABooleanFieldInterface(): void |
85
|
|
|
{ |
86
|
|
|
$contents = $this->itCanCreateAFieldInterface(MappingHelper::TYPE_BOOLEAN); |
87
|
|
|
self::assertContains('function isTestBoolean(): ?bool', $contents); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @test |
92
|
|
|
* |
93
|
|
|
* @param string $type |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function itCanCreateAFieldInterface(string $type = MappingHelper::PHP_TYPE_ARRAY): string |
98
|
|
|
{ |
99
|
|
|
$newObjectFqn = 'EdmondsCommerce\\DoctrineStaticMeta\\Entity\\Fields\\Traits\\Test' . ucfirst($type) . 'FieldInterface'; |
100
|
|
|
$contents = $this->getCreator() |
101
|
|
|
->setMappingHelperCommonType($type) |
102
|
|
|
->createTargetFileObject($newObjectFqn) |
103
|
|
|
->getTargetFile() |
104
|
|
|
->getContents(); |
105
|
|
|
self::assertNotEmpty($contents); |
106
|
|
|
|
107
|
|
|
return $contents; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @test |
112
|
|
|
*/ |
113
|
|
|
public function itCreatesStringFieldsWithExtraValidation(): void |
114
|
|
|
{ |
115
|
|
|
$contents = $this->itCanCreateAFieldInterface(MappingHelper::PHP_TYPE_STRING); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|