|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Stefano Torresi (http://stefanotorresi.it) |
|
4
|
|
|
* @license See the file LICENSE.txt for copying permission. |
|
5
|
|
|
* ************************************************ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Thorr\Persistence\Test\Validator; |
|
9
|
|
|
|
|
10
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
|
11
|
|
|
use Thorr\Persistence\DataMapper\EntityFinderInterface; |
|
12
|
|
|
use Thorr\Persistence\DataMapper\Manager\DataMapperManagerInterface; |
|
13
|
|
|
use Thorr\Persistence\Validator\AbstractEntityValidator; |
|
14
|
|
|
use Zend\Validator\Exception; |
|
15
|
|
|
|
|
16
|
|
|
class AbstractEntityValidatorTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param array $arguments |
|
20
|
|
|
* |
|
21
|
|
|
* @dataProvider constructorValidArgumentsProvider |
|
22
|
|
|
*/ |
|
23
|
|
|
public function testConstructorWithValidArguments($arguments) |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var AbstractEntityValidator $validator */ |
|
26
|
|
|
$validator = $this->getMockForAbstractClass(AbstractEntityValidator::class, $arguments); |
|
27
|
|
|
|
|
28
|
|
|
foreach ($arguments[0] as $property => $value) { |
|
29
|
|
|
$property = str_replace('_', '', $property); |
|
30
|
|
|
$method = 'get' . $property; |
|
31
|
|
|
if (! method_exists($validator, $method)) { |
|
32
|
|
|
continue; |
|
33
|
|
|
} |
|
34
|
|
|
$this->assertSame($value, $validator->$method()); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param array $arguments |
|
40
|
|
|
* @param string $expectedExceptionClass |
|
41
|
|
|
* @param string $expectedExceptionMessage |
|
42
|
|
|
* |
|
43
|
|
|
* @dataProvider constructorInvalidArgumentsProvider |
|
44
|
|
|
*/ |
|
45
|
|
|
public function testConstructorWithInvalidArguments($arguments, $expectedExceptionClass, $expectedExceptionMessage) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->setExpectedException($expectedExceptionClass, $expectedExceptionMessage); |
|
48
|
|
|
|
|
49
|
|
|
/* @var AbstractEntityValidator $validator */ |
|
50
|
|
|
$this->getMockForAbstractClass(AbstractEntityValidator::class, $arguments); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function constructorValidArgumentsProvider() |
|
54
|
|
|
{ |
|
55
|
|
|
$dmm = $this->getMock(DataMapperManagerInterface::class); |
|
56
|
|
|
$dmm->expects($this->any()) |
|
57
|
|
|
->method('getDataMapperForEntity') |
|
58
|
|
|
->willReturn($this->getMock(EntityFinderInterface::class)); |
|
59
|
|
|
|
|
60
|
|
|
return [ |
|
61
|
|
|
[ |
|
62
|
|
|
[ |
|
63
|
|
|
[ |
|
64
|
|
|
'finder' => function () { |
|
65
|
|
|
}, |
|
66
|
|
|
], |
|
67
|
|
|
], |
|
68
|
|
|
], |
|
69
|
|
|
[ |
|
70
|
|
|
[ |
|
71
|
|
|
[ |
|
72
|
|
|
'finder' => $this->getMock(\stdClass::class, [ 'findBySomeProperty' ]), |
|
73
|
|
|
'find_method' => 'findBySomeProperty', |
|
74
|
|
|
], |
|
75
|
|
|
], |
|
76
|
|
|
], |
|
77
|
|
|
[ |
|
78
|
|
|
[ |
|
79
|
|
|
[ |
|
80
|
|
|
'entity_class' => 'FooBar', |
|
81
|
|
|
], |
|
82
|
|
|
$dmm, |
|
83
|
|
|
], |
|
84
|
|
|
], |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function constructorInvalidArgumentsProvider() |
|
89
|
|
|
{ |
|
90
|
|
|
return [ |
|
91
|
|
|
[ |
|
92
|
|
|
[ ], |
|
93
|
|
|
Exception\InvalidArgumentException::class, |
|
94
|
|
|
'No finder nor entity class provided', |
|
95
|
|
|
], |
|
96
|
|
|
[ |
|
97
|
|
|
[ [ 'finder' => 'foo' ] ], |
|
98
|
|
|
Exception\InvalidArgumentException::class, |
|
99
|
|
|
'Finder must be an object or a callable', |
|
100
|
|
|
], |
|
101
|
|
|
[ |
|
102
|
|
|
[ [ 'finder' => new \stdClass() ] ], |
|
103
|
|
|
Exception\InvalidArgumentException::class, |
|
104
|
|
|
"'findByUuid' method not found in 'stdClass'", |
|
105
|
|
|
], |
|
106
|
|
|
[ |
|
107
|
|
|
[ |
|
108
|
|
|
[ |
|
109
|
|
|
'finder' => $this->getMock(EntityFinderInterface::class), |
|
110
|
|
|
'find_method' => 'findBySomeThing', |
|
111
|
|
|
], |
|
112
|
|
|
], |
|
113
|
|
|
Exception\InvalidArgumentException::class, |
|
114
|
|
|
"'findBySomeThing' method not found", |
|
115
|
|
|
], |
|
116
|
|
|
]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function testExcludedSetter() |
|
120
|
|
|
{ |
|
121
|
|
|
$options = [ |
|
122
|
|
|
'finder' => function () { |
|
123
|
|
|
}, |
|
124
|
|
|
'excluded' => 'foo', |
|
125
|
|
|
]; |
|
126
|
|
|
|
|
127
|
|
|
/** @var AbstractEntityValidator $validator */ |
|
128
|
|
|
$validator = $this->getMockForAbstractClass(AbstractEntityValidator::class, [$options]); |
|
129
|
|
|
|
|
130
|
|
|
$this->assertSame([$options['excluded']], $validator->getExcluded()); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function testNonArrayFinderResultIsReturnedInArray() |
|
134
|
|
|
{ |
|
135
|
|
|
$finder = function ($val) { |
|
136
|
|
|
return $val; |
|
137
|
|
|
}; |
|
138
|
|
|
/** @var AbstractEntityValidator $validator */ |
|
139
|
|
|
$validator = $this->getMockForAbstractClass(AbstractEntityValidator::class, [['finder' => $finder]]); |
|
140
|
|
|
|
|
141
|
|
|
$method = new \ReflectionMethod($validator, 'findEntity'); |
|
142
|
|
|
$method->setAccessible(true); |
|
143
|
|
|
$this->assertEquals(['foo'], $method->invoke($validator, 'foo')); |
|
144
|
|
|
$this->assertEquals(['foo'], $method->invoke($validator, ['foo'])); |
|
145
|
|
|
$this->assertEquals([ false ], $method->invoke($validator, false)); // false is indeed a value |
|
146
|
|
|
$this->assertEquals([], $method->invoke($validator, null)); |
|
147
|
|
|
$this->assertEquals([], $method->invoke($validator, [])); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|