1
|
|
|
<?php namespace Unit\Chekote\Phake; |
2
|
|
|
|
3
|
|
|
use Phake as BasePhake; |
4
|
|
|
use Phake\CallRecorder\Recorder; |
5
|
|
|
use Phake\ClassGenerator\MockClass; |
6
|
|
|
use Phake\IMock; |
7
|
|
|
use Phake\Stubber\AnswerCollection; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Unit\Chekote\Phake\Proxies\VisibilityProxy; |
10
|
|
|
use Unit\Chekote\Phake\Stubber\Answers\UnMockedResponseExceptionAnswer; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Extends Phake to add strict mock functionality. |
14
|
|
|
* |
15
|
|
|
* A strict mock will throw an Exception stating the class and method name of any method that is called without |
16
|
|
|
* its response being mocked. This is to assist in strict London style TDD, whereby only explicitly mocked out |
17
|
|
|
* methods are allowed to be invoked. |
18
|
|
|
*/ |
19
|
|
|
abstract class Phake extends BasePhake |
20
|
|
|
{ |
21
|
|
|
/** @var Expectation[] */ |
22
|
|
|
protected static array $expectations; |
23
|
|
|
|
24
|
|
|
public static function clearExpectations(): void |
25
|
|
|
{ |
26
|
|
|
self::$expectations = []; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Declare an expectation for a mock. |
31
|
|
|
* |
32
|
|
|
* This method combines the behavior of when() and verify() to create an expectation. The returned expectation |
33
|
|
|
* should be utilized in the same way that the Stubber from when() would be used. The expectations will be |
34
|
|
|
* recorded and can be verified via verifyExpectations(), which should typically be invoked in tearDown(). |
35
|
|
|
* |
36
|
|
|
* @param IMock $mock the mock. |
37
|
|
|
* @param int $count the expected call count. |
38
|
|
|
* @return Expectation the expectation. |
39
|
|
|
*/ |
40
|
|
|
public static function expect(IMock $mock, int $count): Expectation |
41
|
|
|
{ |
42
|
|
|
$expectation = new Expectation($mock, $count); |
43
|
|
|
self::$expectations[] = $expectation; |
44
|
|
|
|
45
|
|
|
return $expectation; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Verifies all expectations. |
50
|
|
|
* |
51
|
|
|
* @throws RuntimeException if a method has not been set for the expectation. |
52
|
|
|
* @throws RuntimeException if args have not been set for the expectation. |
53
|
|
|
*/ |
54
|
|
|
public static function verifyExpectations(): void |
55
|
|
|
{ |
56
|
|
|
foreach (self::$expectations as $expectation) { |
57
|
|
|
$expectation->verify(); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Overrides the base method to use our custom VisibilityProxy. |
63
|
|
|
* |
64
|
|
|
* @param IMock $mock |
65
|
|
|
* @return VisibilityProxy $mock |
66
|
|
|
*/ |
67
|
|
|
public static function makeVisible(IMock $mock) |
68
|
|
|
{ |
69
|
|
|
return new VisibilityProxy($mock); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Creates a strict mock. |
74
|
|
|
* |
75
|
|
|
* @param class-string $className the name of the class to mock. |
|
|
|
|
76
|
|
|
* @return IMock the mocked class instance. |
77
|
|
|
*/ |
78
|
|
|
public static function strictMock(string $className): IMock |
79
|
|
|
{ |
80
|
|
|
return self::mock($className, new AnswerCollection(new UnMockedResponseExceptionAnswer())); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Creates a strict mock and calls the real classes constructor. |
85
|
|
|
* |
86
|
|
|
* This method creates a mock that is somewhere between what Phake::mock and Phake::partialMock would create. The |
87
|
|
|
* returned mock behaves exactly the same as what Phake::mock would return, except that it calls the constructor |
88
|
|
|
* of the mocked class (as Phake::partialMock does). However, the returned mock will NOT thenCallParent() for every |
89
|
|
|
* mocked method in the way that a partial mock would. |
90
|
|
|
* |
91
|
|
|
* @param class-string $className the name of the class to mock. |
|
|
|
|
92
|
|
|
* @param mixed ...$args arguments for the classes constructor. |
93
|
|
|
* @return IMock the mocked class instance. |
94
|
|
|
*/ |
95
|
|
|
public static function strictMockWithConstructor(string $className, ...$args): IMock |
96
|
|
|
{ |
97
|
|
|
return self::getPhake()->mock( |
98
|
|
|
$className, |
99
|
|
|
new MockClass(self::getMockLoader()), |
100
|
|
|
new Recorder(), |
101
|
|
|
new UnMockedResponseExceptionAnswer(), |
102
|
|
|
$args |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|