1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GeneratedHydratorTest\CodeGenerator\Visitor; |
6
|
|
|
|
7
|
|
|
use GeneratedHydrator\CodeGenerator\Visitor\HydratorMethodsVisitor; |
8
|
|
|
use PhpParser\Node; |
9
|
|
|
use PhpParser\Node\Expr\Assign; |
10
|
|
|
use PhpParser\Node\Expr\PropertyFetch; |
11
|
|
|
use PhpParser\Node\Stmt\Class_; |
12
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
13
|
|
|
use PhpParser\ParserFactory; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use CodeGenerationUtils\Inflector\Util\UniqueIdentifierGenerator; |
16
|
|
|
use ReflectionClass; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Tests for {@see \GeneratedHydrator\CodeGenerator\Visitor\HydratorMethodsVisitor} |
20
|
|
|
* |
21
|
|
|
* @author Marco Pivetta <[email protected]> |
22
|
|
|
* @license MIT |
23
|
|
|
* |
24
|
|
|
* @covers \GeneratedHydrator\CodeGenerator\Visitor\HydratorMethodsVisitor |
25
|
|
|
*/ |
26
|
|
|
class HydratorMethodsVisitorTest extends TestCase |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @dataProvider classAstProvider |
30
|
|
|
* |
31
|
|
|
* @param string $className |
32
|
|
|
* @param Class_ $classNode |
33
|
|
|
* @param string[] $properties |
34
|
|
|
*/ |
35
|
|
|
public function testBasicCodeGeneration(string $className, Class_ $classNode, array $properties) |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$visitor = new HydratorMethodsVisitor(new ReflectionClass($className)); |
38
|
|
|
|
39
|
|
|
/* @var $modifiedAst Class_ */ |
40
|
|
|
$modifiedNode = $visitor->leaveNode($classNode); |
41
|
|
|
|
42
|
|
|
self::assertMethodExistence('hydrate', $modifiedNode); |
|
|
|
|
43
|
|
|
self::assertMethodExistence('extract', $modifiedNode); |
|
|
|
|
44
|
|
|
self::assertMethodExistence('__construct', $modifiedNode); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Verifies that a method was correctly added to by the visitor |
49
|
|
|
* |
50
|
|
|
* @param string $methodName |
51
|
|
|
* @param Class_ $class |
52
|
|
|
*/ |
53
|
|
|
private function assertMethodExistence(string $methodName, Class_ $class) |
54
|
|
|
{ |
55
|
|
|
$members = $class->stmts; |
56
|
|
|
|
57
|
|
|
self::assertCount( |
58
|
|
|
1, |
59
|
|
|
array_filter( |
60
|
|
|
$members, |
61
|
|
|
function (Node $node) use ($methodName) : bool { |
62
|
|
|
return $node instanceof ClassMethod |
63
|
|
|
&& $methodName === $node->name; |
64
|
|
|
} |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return \PhpParser\Node[][] |
71
|
|
|
*/ |
72
|
|
|
public function classAstProvider() : array |
73
|
|
|
{ |
74
|
|
|
$parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7); |
75
|
|
|
|
76
|
|
|
$className = UniqueIdentifierGenerator::getIdentifier('Foo'); |
77
|
|
|
$classCode = 'class ' . $className . ' { private $bar; private $baz; protected $tab; ' |
78
|
|
|
. 'protected $tar; public $taw; public $tam; }'; |
79
|
|
|
|
80
|
|
|
eval($classCode); |
81
|
|
|
|
82
|
|
|
$staticClassName = UniqueIdentifierGenerator::getIdentifier('Foo'); |
83
|
|
|
$staticClassCode = 'class ' . $staticClassName . ' { private static $bar; ' |
84
|
|
|
. 'protected static $baz; public static $tab; private $taz; }'; |
85
|
|
|
|
86
|
|
|
eval($staticClassCode); |
87
|
|
|
|
88
|
|
|
return [ |
89
|
|
|
[$className, $parser->parse('<?php ' . $classCode)[0], ['bar', 'baz', 'tab', 'tar', 'taw', 'tam']], |
90
|
|
|
[$staticClassName, $parser->parse('<?php ' . $staticClassCode)[0], ['taz']], |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.