1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HelloWordPl\SimpleEntityGeneratorBundle\Tests\Lib; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\ClassConfig; |
7
|
|
|
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\ClassManager; |
8
|
|
|
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\InterfaceManager; |
9
|
|
|
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\TestClassManager; |
10
|
|
|
use HelloWordPl\SimpleEntityGeneratorBundle\Tests\Lib\Helper; |
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* StructureGenerator Test |
15
|
|
|
* |
16
|
|
|
* @author Sławomir Kania <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class StructureGeneratorTest extends KernelTestCase |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
public function testParseToArray() |
22
|
|
|
{ |
23
|
|
|
$classesManagers = $this->getStructureFromYaml(Helper::getStructureYaml()); |
24
|
|
|
$this->assertEquals(2, count($classesManagers)); |
25
|
|
|
|
26
|
|
|
foreach ($classesManagers as $classManager) { |
27
|
|
|
$this->checkCommonClassManager($classManager); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$userEntity = $classesManagers[0]; |
31
|
|
|
$this->assertEquals(7, $userEntity->getProperties()->count()); |
32
|
|
|
$this->assertEquals(15, $userEntity->getMethods()->count()); // getters + setters + boolean |
33
|
|
|
|
34
|
|
|
$userEntityInterface = $userEntity->getInterface(); |
35
|
|
|
$this->assertEquals(15, $userEntityInterface->getMethods()->count()); |
36
|
|
|
|
37
|
|
|
$userEntityTestClass = $userEntity->getTestClass(); |
38
|
|
|
$this->assertEquals(15, $userEntityTestClass->getMethods()->count()); |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
$postEntity = $classesManagers[1]; |
42
|
|
|
$this->assertEquals(3, $postEntity->getProperties()->count()); |
43
|
|
|
$this->assertEquals(6, $postEntity->getMethods()->count()); // getters + setters |
44
|
|
|
|
45
|
|
|
$postEntityInterface = $postEntity->getInterface(); |
46
|
|
|
$this->assertEquals(6, $postEntityInterface->getMethods()->count()); |
47
|
|
|
|
48
|
|
|
$postEntityTestClass = $postEntity->getTestClass(); |
49
|
|
|
$this->assertEquals(6, $postEntityTestClass->getMethods()->count()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @dataProvider dataForTestClassManagerWithInlineConfiguration |
54
|
|
|
*/ |
55
|
|
|
public function testClassManagerWithInlineConfiguration(ClassManager $classManager, ClassConfig $expectedInlineClassConfig) |
56
|
|
|
{ |
57
|
|
|
/* @var $item ClassManager */ |
58
|
|
|
/* @var $classConfig ClassConfig */ |
59
|
|
|
$classConfig = $classManager->getConfiguration(); |
60
|
|
|
$this->assertInstanceOf(ClassConfig::class, $classConfig); |
61
|
|
|
$this->assertEquals($expectedInlineClassConfig->isNoInterface(), $classConfig->isNoInterface()); |
62
|
|
|
$this->assertEquals($expectedInlineClassConfig->isNoPHPUnitClass(), $classConfig->isNoPHPUnitClass()); |
63
|
|
|
|
64
|
|
|
if ($expectedInlineClassConfig->isNoInterface()) { |
65
|
|
|
$this->assertNull($classManager->getInterface()); |
66
|
|
|
} else { |
67
|
|
|
$this->assertInstanceOf(InterfaceManager::class, $classManager->getInterface()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($expectedInlineClassConfig->isNoPHPUnitClass()) { |
71
|
|
|
$this->assertNull($classManager->getTestClass()); |
72
|
|
|
} else { |
73
|
|
|
$this->assertInstanceOf(TestClassManager::class, $classManager->getTestClass()); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function dataForTestClassManagerWithInlineConfiguration() |
78
|
|
|
{ |
79
|
|
|
$classManagersOne = $this->getStructureFromYaml(Helper::getStructureYamlForTestInlineClassConfuration()); |
80
|
|
|
$classManagersTwo = $this->getStructureFromYaml(Helper::getStructureYamlForTemplateChangeTest()); |
81
|
|
|
|
82
|
|
|
return [ |
83
|
|
|
[$classManagersOne[0], new ClassConfig(true, true)], |
84
|
|
|
[$classManagersTwo[0], new ClassConfig(false, false)] |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testInlineConfiguration() |
89
|
|
|
{ |
90
|
|
|
$classesManagers = $this->getStructureFromYaml(Helper::getStructureYamlForTestInlineClassConfuration()); |
91
|
|
|
$this->assertEquals(1, count($classesManagers)); |
92
|
|
|
$classManager = $classesManagers->first(); |
93
|
|
|
$this->assertInstanceOf(ClassManager::class, $classManager); |
94
|
|
|
$this->assertFalse($classManager->hasInterface()); |
95
|
|
|
$this->assertFalse($classManager->hasTestClass()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function getStructureFromYaml($yamlStructure) |
99
|
|
|
{ |
100
|
|
|
self::bootKernel(); |
101
|
|
|
$structureGenerator = self::$kernel->getContainer()->get('seg.structure_generator'); |
102
|
|
|
$resultArray = $structureGenerator->parseToArray($yamlStructure); |
103
|
|
|
return $structureGenerator->buildEntitiesClassStructure($resultArray); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function checkCommonClassManager(ClassManager $classManager) |
107
|
|
|
{ |
108
|
|
|
$this->assertInstanceOf("\HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\ClassManager", $classManager); |
109
|
|
|
$this->assertInstanceOf("\HelloWordPl\SimpleEntityGeneratorBundle\Lib\Interfaces\RenderableInterface", $classManager); |
110
|
|
|
$this->assertInstanceOf("\HelloWordPl\SimpleEntityGeneratorBundle\Lib\Interfaces\DumpableInterface", $classManager); |
111
|
|
|
$this->assertInstanceOf("\HelloWordPl\SimpleEntityGeneratorBundle\Lib\Interfaces\StructureWithMethodsInterface", $classManager); |
112
|
|
|
$this->assertTrue($classManager->hasInterface()); |
113
|
|
|
$this->assertInstanceOf("\HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\InterfaceManager", $classManager->getInterface()); |
114
|
|
|
$this->assertTrue($classManager->hasTestClass()); |
115
|
|
|
$this->assertInstanceOf("\HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\TestClassManager", $classManager->getTestClass()); |
116
|
|
|
$this->assertTrue($classManager->hasUniquePropertiesNames()); |
117
|
|
|
|
118
|
|
|
$this->assertInstanceOf("\Doctrine\Common\Collections\ArrayCollection", $classManager->getProperties()); |
119
|
|
|
$this->assertTrue($classManager->getProperties()->count() > 0); |
120
|
|
|
|
121
|
|
|
$this->assertInstanceOf("\Doctrine\Common\Collections\ArrayCollection", $classManager->getMethods()); |
122
|
|
|
$this->assertTrue($classManager->getMethods()->count() > 0); |
123
|
|
|
|
124
|
|
|
$this->checkAllProperties($classManager->getProperties()); |
125
|
|
|
$this->checkAllMethods($classManager->getMethods()); |
126
|
|
|
$this->checkAllMethods($classManager->getInterface()->getMethods()); |
127
|
|
|
$this->checkAllTestMethods($classManager->getTestClass()->getMethods()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function checkAllProperties(ArrayCollection $properties) |
131
|
|
|
{ |
132
|
|
|
foreach ($properties as $property) { |
133
|
|
|
$this->assertInstanceOf("\HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\PropertyManager", $property); |
134
|
|
|
$this->assertInstanceOf("\HelloWordPl\SimpleEntityGeneratorBundle\Lib\Interfaces\RenderableInterface", $property); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
View Code Duplication |
protected function checkAllMethods(ArrayCollection $methods) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
foreach ($methods as $method) { |
141
|
|
|
$this->assertInstanceOf("\HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\MethodManager", $method); |
142
|
|
|
$this->assertInstanceOf("\HelloWordPl\SimpleEntityGeneratorBundle\Lib\Interfaces\RenderableInterface", $method); |
143
|
|
|
$this->assertInstanceOf("\HelloWordPl\SimpleEntityGeneratorBundle\Lib\Interfaces\MethodInterface", $method); |
144
|
|
|
$this->assertInstanceOf("\HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\PropertyManager", $method->getProperty()); |
145
|
|
|
$this->assertInstanceOf("\HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\ClassManager", $method->getClassManager()); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
View Code Duplication |
protected function checkAllTestMethods(ArrayCollection $methods) |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
foreach ($methods as $method) { |
152
|
|
|
$this->assertInstanceOf("\HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\TestMethodManager", $method); |
153
|
|
|
$this->assertInstanceOf("\HelloWordPl\SimpleEntityGeneratorBundle\Lib\Interfaces\RenderableInterface", $method); |
154
|
|
|
$this->assertInstanceOf("\HelloWordPl\SimpleEntityGeneratorBundle\Lib\Interfaces\MethodInterface", $method); |
155
|
|
|
$this->assertInstanceOf("\HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\MethodManager", $method->getMethod()); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.