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