|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GeneratedHydratorTest\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use CodeGenerationUtils\Autoloader\AutoloaderInterface; |
|
8
|
|
|
use CodeGenerationUtils\GeneratorStrategy\GeneratorStrategyInterface; |
|
9
|
|
|
use CodeGenerationUtils\Inflector\ClassNameInflectorInterface; |
|
10
|
|
|
use CodeGenerationUtils\Inflector\Util\UniqueIdentifierGenerator; |
|
11
|
|
|
use GeneratedHydrator\ClassGenerator\DefaultHydratorGenerator; |
|
12
|
|
|
use GeneratedHydrator\Configuration; |
|
13
|
|
|
use GeneratedHydrator\Factory\HydratorFactory; |
|
14
|
|
|
use GeneratedHydratorTestAsset\LazyLoadingMock; |
|
15
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Tests for {@see \GeneratedHydrator\Factory\HydratorFactory} |
|
20
|
|
|
*/ |
|
21
|
|
|
class HydratorFactoryTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var MockObject */ |
|
24
|
|
|
protected $inflector; |
|
25
|
|
|
|
|
26
|
|
|
/** @var Configuration|MockObject */ |
|
27
|
|
|
protected $config; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritDoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
public function setUp() : void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->inflector = $this->createMock(ClassNameInflectorInterface::class); |
|
35
|
|
|
$this->config = $this |
|
36
|
|
|
->getMockBuilder('GeneratedHydrator\Configuration') |
|
37
|
|
|
->disableOriginalConstructor() |
|
38
|
|
|
->getMock(); |
|
39
|
|
|
|
|
40
|
|
|
$this |
|
41
|
|
|
->config |
|
42
|
|
|
->expects(self::any()) |
|
43
|
|
|
->method('getClassNameInflector') |
|
44
|
|
|
->will(self::returnValue($this->inflector)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritDoc} |
|
49
|
|
|
* |
|
50
|
|
|
* @covers \GeneratedHydrator\Factory\HydratorFactory::__construct |
|
51
|
|
|
* @covers \GeneratedHydrator\Factory\HydratorFactory::getHydratorClass |
|
52
|
|
|
*/ |
|
53
|
|
|
public function testWillSkipAutoGeneration() |
|
54
|
|
|
{ |
|
55
|
|
|
$className = UniqueIdentifierGenerator::getIdentifier('foo'); |
|
56
|
|
|
|
|
57
|
|
|
$this->config->expects(self::any())->method('getHydratedClassName')->will(self::returnValue($className)); |
|
|
|
|
|
|
58
|
|
|
$this->config->expects(self::any())->method('doesAutoGenerateProxies')->will(self::returnValue(false)); |
|
59
|
|
|
$this |
|
60
|
|
|
->inflector |
|
61
|
|
|
->expects(self::any()) |
|
62
|
|
|
->method('getUserClassName') |
|
63
|
|
|
->with($className) |
|
64
|
|
|
->will(self::returnValue('GeneratedHydratorTestAsset\BaseClass')); |
|
65
|
|
|
|
|
66
|
|
|
$this |
|
67
|
|
|
->inflector |
|
68
|
|
|
->expects(self::once()) |
|
69
|
|
|
->method('getGeneratedClassName') |
|
70
|
|
|
->with('GeneratedHydratorTestAsset\BaseClass') |
|
71
|
|
|
->will(self::returnValue('GeneratedHydratorTestAsset\EmptyClass')); |
|
72
|
|
|
|
|
73
|
|
|
$factory = new HydratorFactory($this->config); |
|
|
|
|
|
|
74
|
|
|
$generatedClass = $factory->getHydratorClass(); |
|
75
|
|
|
|
|
76
|
|
|
self::assertInstanceOf('GeneratedHydratorTestAsset\EmptyClass', new $generatedClass()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritDoc} |
|
81
|
|
|
* |
|
82
|
|
|
* @covers \GeneratedHydrator\Factory\HydratorFactory::__construct |
|
83
|
|
|
* @covers \GeneratedHydrator\Factory\HydratorFactory::getHydratorClass |
|
84
|
|
|
* |
|
85
|
|
|
* NOTE: serious mocking going on in here (a class is generated on-the-fly) - careful |
|
86
|
|
|
*/ |
|
87
|
|
|
public function testWillTryAutoGeneration() |
|
88
|
|
|
{ |
|
89
|
|
|
$className = UniqueIdentifierGenerator::getIdentifier('foo'); |
|
90
|
|
|
$generatedClassName = UniqueIdentifierGenerator::getIdentifier('bar'); |
|
91
|
|
|
$generator = $this->createMock(GeneratorStrategyInterface::class); |
|
92
|
|
|
$autoloader = $this->createMock(AutoloaderInterface::class); |
|
93
|
|
|
|
|
94
|
|
|
$this->config->expects(self::any())->method('getHydratedClassName')->will(self::returnValue($className)); |
|
|
|
|
|
|
95
|
|
|
$this->config->expects(self::any())->method('doesAutoGenerateProxies')->will(self::returnValue(true)); |
|
96
|
|
|
$this->config->expects(self::any())->method('getGeneratorStrategy')->will(self::returnValue($generator)); |
|
97
|
|
|
$this->config->expects(self::any())->method('getHydratorGenerator')->willReturn(new DefaultHydratorGenerator()); |
|
98
|
|
|
$this |
|
99
|
|
|
->config |
|
100
|
|
|
->expects(self::any()) |
|
101
|
|
|
->method('getGeneratedClassAutoloader') |
|
102
|
|
|
->will(self::returnValue($autoloader)); |
|
103
|
|
|
|
|
104
|
|
|
$generator |
|
105
|
|
|
->expects(self::once()) |
|
106
|
|
|
->method('generate') |
|
107
|
|
|
->with(self::isType('array')); |
|
108
|
|
|
|
|
109
|
|
|
// simulate autoloading |
|
110
|
|
|
$autoloader |
|
111
|
|
|
->expects(self::once()) |
|
112
|
|
|
->method('__invoke') |
|
113
|
|
|
->with($generatedClassName) |
|
114
|
|
|
->willReturnCallback(static function () use ($generatedClassName) : bool { |
|
115
|
|
|
eval('class ' . $generatedClassName . ' {}'); |
|
116
|
|
|
|
|
117
|
|
|
return true; |
|
118
|
|
|
}); |
|
119
|
|
|
|
|
120
|
|
|
$this |
|
121
|
|
|
->inflector |
|
122
|
|
|
->expects(self::once()) |
|
123
|
|
|
->method('getGeneratedClassName') |
|
124
|
|
|
->with('GeneratedHydratorTestAsset\BaseClass') |
|
125
|
|
|
->will(self::returnValue($generatedClassName)); |
|
126
|
|
|
|
|
127
|
|
|
$this |
|
128
|
|
|
->inflector |
|
129
|
|
|
->expects(self::once()) |
|
130
|
|
|
->method('getUserClassName') |
|
131
|
|
|
->with($className) |
|
132
|
|
|
->will(self::returnValue('GeneratedHydratorTestAsset\BaseClass')); |
|
133
|
|
|
|
|
134
|
|
|
$factory = new HydratorFactory($this->config); |
|
|
|
|
|
|
135
|
|
|
/** @var LazyLoadingMock $generatedClass */ |
|
136
|
|
|
$generatedClass = $factory->getHydratorClass(); |
|
137
|
|
|
|
|
138
|
|
|
self::assertInstanceOf($generatedClassName, new $generatedClass()); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: