1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Moka\Tests; |
5
|
|
|
|
6
|
|
|
use Moka\Exception\InvalidArgumentException; |
7
|
|
|
use Moka\Strategy\MockingStrategyInterface; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Tests\AbstractTestClass; |
10
|
|
|
use Tests\BarTestClass; |
11
|
|
|
use Tests\FooTestClass; |
12
|
|
|
use Tests\TestInterface; |
13
|
|
|
|
14
|
|
|
abstract class MokaMockingStrategyTestCase extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var MockingStrategyInterface |
18
|
|
|
*/ |
19
|
|
|
protected $strategy; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var object |
23
|
|
|
*/ |
24
|
|
|
protected $mock; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $namesWithValues; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $className; |
35
|
|
|
|
36
|
|
|
protected function setUp() |
37
|
|
|
{ |
38
|
|
|
$this->className = [ |
39
|
|
|
FooTestClass::class, |
40
|
|
|
BarTestClass::class |
41
|
|
|
][random_int(0, 1)]; |
42
|
|
|
|
43
|
|
|
$this->namesWithValues = [ |
44
|
|
|
'$property' => mt_rand(), |
45
|
|
|
'$public' => mt_rand(), |
46
|
|
|
'$private' => mt_rand(), |
47
|
|
|
'isTrue' => (bool)random_int(0, 1), |
48
|
|
|
'getInt' => mt_rand(), |
49
|
|
|
'withArgument' => mt_rand(), |
50
|
|
|
'throwException' => new \Exception('' . mt_rand()) |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
$this->mock = $this->strategy->build($this->className); |
54
|
|
|
|
55
|
|
|
$this->strategy->decorate($this->mock, $this->namesWithValues); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
final public function testGetMockTypeSuccess() |
59
|
|
|
{ |
60
|
|
|
$this->assertInternalType('string', $this->strategy->getMockType()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
final public function testBuildClassSuccess() |
64
|
|
|
{ |
65
|
|
|
$this->assertInstanceOf($this->strategy->getMockType(), $this->mock); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
final public function testBuildInterfaceSuccess() |
69
|
|
|
{ |
70
|
|
|
$mock = $this->strategy->build(TestInterface::class); |
71
|
|
|
|
72
|
|
|
$this->assertInstanceOf($this->strategy->getMockType(), $mock); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
final public function testBuildAbstractClassSuccess() |
76
|
|
|
{ |
77
|
|
|
$mock = $this->strategy->build(AbstractTestClass::class); |
78
|
|
|
|
79
|
|
|
$this->assertInstanceOf($this->strategy->getMockType(), $mock); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
final public function testDecorateFailure() |
83
|
|
|
{ |
84
|
|
|
$this->expectException(InvalidArgumentException::class); |
85
|
|
|
|
86
|
|
|
$this->strategy->decorate(new \stdClass(), $this->namesWithValues); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
final public function testDecorateWrongTypeHintFailure() |
90
|
|
|
{ |
91
|
|
|
$this->strategy->decorate($this->mock, [ |
92
|
|
|
'getSelf' => mt_rand() |
93
|
|
|
]); |
94
|
|
|
|
95
|
|
|
$this->expectException(\TypeError::class); |
96
|
|
|
$this->strategy->get($this->mock)->getSelf(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
final public function testDecorateWithPropertyFailure() |
100
|
|
|
{ |
101
|
|
|
$this->assertEquals( |
102
|
|
|
$this->namesWithValues['$property'], |
103
|
|
|
$this->strategy->get($this->mock)->property |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
final public function testDecorateWithPublicPropertySuccess() |
108
|
|
|
{ |
109
|
|
|
$this->assertEquals( |
110
|
|
|
$this->namesWithValues['$public'], |
111
|
|
|
$this->strategy->get($this->mock)->public |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
final public function testDecorateWithProtectedPropertyFailure() |
116
|
|
|
{ |
117
|
|
|
$this->expectException(\Error::class); |
118
|
|
|
|
119
|
|
|
$this->strategy->decorate($this->mock, [ |
120
|
|
|
'$protected' => 1138 |
121
|
|
|
]); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
final public function testDecorateWithPrivatePropertySuccess() |
125
|
|
|
{ |
126
|
|
|
$this->assertEquals( |
127
|
|
|
$this->namesWithValues['$private'], |
128
|
|
|
$this->strategy->get($this->mock)->private |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
final public function testDecorateWithMethodSingleCallSuccess() |
133
|
|
|
{ |
134
|
|
|
$this->assertSame($this->namesWithValues['isTrue'], $this->strategy->get($this->mock)->isTrue()); |
135
|
|
|
|
136
|
|
|
$this->expectException(\Exception::class); |
137
|
|
|
$this->expectExceptionMessage($this->namesWithValues['throwException']->getMessage()); |
138
|
|
|
$this->strategy->get($this->mock)->throwException(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
final public function testDecorateWithMethodMultipleCallsSuccess() |
142
|
|
|
{ |
143
|
|
|
$this->assertSame($this->namesWithValues['getInt'], $this->strategy->get($this->mock)->getInt()); |
144
|
|
|
$this->assertSame($this->namesWithValues['getInt'], $this->strategy->get($this->mock)->getInt()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
final public function testDecorateWithMethodOverriddenCallsFailure() |
148
|
|
|
{ |
149
|
|
|
$this->strategy->decorate($this->mock, [ |
150
|
|
|
'getInt' => mt_rand(), |
151
|
|
|
'throwException' => mt_rand() |
152
|
|
|
]); |
153
|
|
|
|
154
|
|
|
$this->assertSame($this->namesWithValues['getInt'], $this->strategy->get($this->mock)->getInt()); |
155
|
|
|
$this->assertSame($this->namesWithValues['getInt'], $this->strategy->get($this->mock)->getInt()); |
156
|
|
|
|
157
|
|
|
$this->expectException(\Exception::class); |
158
|
|
|
$this->expectExceptionMessage($this->namesWithValues['throwException']->getMessage()); |
159
|
|
|
$this->strategy->get($this->mock)->throwException(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
final public function testDecorateWithMethodCallWithArgumentSuccess() |
163
|
|
|
{ |
164
|
|
|
$this->assertSame($this->namesWithValues['withArgument'], $this->strategy->get($this->mock)->withArgument(mt_rand())); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
final public function testDecorateWithMethodCallWithMissingArgumentFailure() |
168
|
|
|
{ |
169
|
|
|
$this->expectException(\Error::class); |
170
|
|
|
|
171
|
|
|
$this->strategy->get($this->mock)->withArgument(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
final public function testDecorateWithMethodCallWithWrongArgumentFailure() |
175
|
|
|
{ |
176
|
|
|
$this->expectException(\TypeError::class); |
177
|
|
|
|
178
|
|
|
$this->strategy->get($this->mock)->withArgument('string'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
final public function testGetSuccess() |
182
|
|
|
{ |
183
|
|
|
$this->assertInstanceOf($this->className, $this->strategy->get($this->mock)); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
final public function testGetFailure() |
187
|
|
|
{ |
188
|
|
|
$this->expectException(InvalidArgumentException::class); |
189
|
|
|
|
190
|
|
|
$this->strategy->get(new \stdClass()); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
final protected function setStrategy(MockingStrategyInterface $strategy) |
194
|
|
|
{ |
195
|
|
|
$this->strategy = $strategy; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
final protected function getRandomFQCN() |
199
|
|
|
{ |
200
|
|
|
return 'foo_' . mt_rand(); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|