1
|
|
|
<?php namespace Chekote\Phake; |
2
|
|
|
|
3
|
|
|
use Phake_IMock; |
4
|
|
|
use Phake_Proxies_StubberProxy; |
5
|
|
|
use Phake_Proxies_VerifierProxy; |
6
|
|
|
use RuntimeException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Combines a Phake Stubber and Verifier to create an expectation. |
10
|
|
|
* |
11
|
|
|
* This class is used to accomplish something similar to Prophecy predictions, but without the opinionated restrictions |
12
|
|
|
* that come with that mocking framework. |
13
|
|
|
*/ |
14
|
|
|
class Expectation |
15
|
|
|
{ |
16
|
|
|
/** @var Phake_IMock the mock object */ |
17
|
|
|
protected $mock; |
18
|
|
|
|
19
|
|
|
/** @var string the method expected to be called */ |
20
|
|
|
protected $method; |
21
|
|
|
|
22
|
|
|
/** @var array the arguments expected to be passed to the method */ |
23
|
|
|
protected $args; |
24
|
|
|
|
25
|
|
|
/** @var int the number of times the method is expected to be called */ |
26
|
|
|
protected $count; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Expectation constructor. |
30
|
|
|
* |
31
|
|
|
* @param Phake_IMock $mock the mock object |
32
|
|
|
* @param int $count number of times the method is expected to be called |
33
|
|
|
*/ |
34
|
|
|
public function __construct(Phake_IMock $mock, int $count) |
35
|
|
|
{ |
36
|
|
|
$this->mock = $mock; |
37
|
|
|
$this->count = $count; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Verifies that the expected method was called. |
42
|
|
|
* |
43
|
|
|
* @throws RuntimeException if a method has not been set for the expectation. |
44
|
|
|
* @throws RuntimeException if args have not been set for the expectation. |
45
|
|
|
* @return Phake_Proxies_VerifierProxy |
46
|
|
|
*/ |
47
|
|
|
public function verify() |
48
|
|
|
{ |
49
|
|
|
if (!isset($this->method)) { |
50
|
|
|
throw new RuntimeException('Expectation method was not set'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (!isset($this->args)) { |
54
|
|
|
throw new RuntimeException('Expectation args were not set'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$method = $this->method; |
58
|
|
|
|
59
|
|
|
/** @var Phake_Proxies_VerifierProxy $verifier */ |
60
|
|
|
$verifier = Phake::verify($this->mock, Phake::times($this->count))->$method(...$this->args); |
61
|
|
|
|
62
|
|
|
return $verifier; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Sets the expected method to be called. |
67
|
|
|
* |
68
|
|
|
* @param string $method the method that is expected to be called. |
69
|
|
|
* @param array $args the args that are expected to be passed to the method. |
70
|
|
|
* @return Phake_Proxies_StubberProxy |
71
|
|
|
*/ |
72
|
|
|
public function __call(string $method, array $args) |
73
|
|
|
{ |
74
|
|
|
// record the method and args for verification later |
75
|
|
|
$this->method = $method; |
76
|
|
|
$this->args = $args; |
77
|
|
|
|
78
|
|
|
return Phake::when($this->mock)->$method(...$args); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|