1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
declare(strict_types=1); |
20
|
|
|
|
21
|
|
|
namespace GeneratedHydratorTest\ClassGenerator; |
22
|
|
|
|
23
|
|
|
use CodeGenerationUtils\Visitor\ClassRenamerVisitor; |
24
|
|
|
use GeneratedHydrator\ClassGenerator\HydratorGenerator; |
25
|
|
|
use CodeGenerationUtils\Inflector\Util\UniqueIdentifierGenerator; |
26
|
|
|
use CodeGenerationUtils\GeneratorStrategy\EvaluatingGeneratorStrategy; |
27
|
|
|
use GeneratedHydratorTestAsset\BaseClass; |
28
|
|
|
use GeneratedHydratorTestAsset\ClassWithByRefMagicMethods; |
29
|
|
|
use GeneratedHydratorTestAsset\ClassWithMagicMethods; |
30
|
|
|
use GeneratedHydratorTestAsset\ClassWithMixedProperties; |
31
|
|
|
use PhpParser\NodeTraverser; |
32
|
|
|
use PHPUnit_Framework_TestCase; |
33
|
|
|
use ReflectionClass; |
34
|
|
|
use Zend\Hydrator\HydratorInterface; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Tests for {@see \GeneratedHydrator\ClassGenerator\HydratorGenerator} |
38
|
|
|
* |
39
|
|
|
* @author Marco Pivetta <[email protected]> |
40
|
|
|
* @license MIT |
41
|
|
|
* |
42
|
|
|
* @covers \GeneratedHydrator\ClassGenerator\HydratorGenerator |
43
|
|
|
*/ |
44
|
|
|
class HydratorGeneratorTest extends PHPUnit_Framework_TestCase |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* @dataProvider getTestedImplementations |
48
|
|
|
* |
49
|
|
|
* Verifies that generated code is valid and implements expected interfaces |
50
|
|
|
* |
51
|
|
|
* @param string $className |
52
|
|
|
*/ |
53
|
|
|
public function testGeneratesValidCode(string $className) |
54
|
|
|
{ |
55
|
|
|
$generator = new HydratorGenerator(); |
56
|
|
|
$generatedClassName = UniqueIdentifierGenerator::getIdentifier('HydratorGeneratorTest'); |
57
|
|
|
$originalClass = new ReflectionClass($className); |
58
|
|
|
$generatorStrategy = new EvaluatingGeneratorStrategy(); |
59
|
|
|
$traverser = new NodeTraverser(); |
60
|
|
|
|
61
|
|
|
$traverser->addVisitor(new ClassRenamerVisitor($originalClass, $generatedClassName)); |
62
|
|
|
$generatorStrategy->generate($traverser->traverse($generator->generate($originalClass))); |
63
|
|
|
|
64
|
|
|
$generatedReflection = new ReflectionClass($generatedClassName); |
65
|
|
|
|
66
|
|
|
self::assertSame($generatedClassName, $generatedReflection->getName()); |
|
|
|
|
67
|
|
|
|
68
|
|
|
foreach ($this->getExpectedImplementedInterfaces() as $interface) { |
69
|
|
|
self::assertTrue($generatedReflection->implementsInterface($interface)); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function getTestedImplementations() : array |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
|
|
[BaseClass::class], |
80
|
|
|
[ClassWithMagicMethods::class], |
81
|
|
|
[ClassWithByRefMagicMethods::class], |
82
|
|
|
[ClassWithMixedProperties::class], |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritDoc} |
88
|
|
|
*/ |
89
|
|
|
protected function getExpectedImplementedInterfaces() : array |
90
|
|
|
{ |
91
|
|
|
return [HydratorInterface::class]; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|