|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ekvedaras\LaravelTestHelpers\Helpers; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class TestHelpersMock. |
|
9
|
|
|
* @method PHPUnit\Framework\MockObject\Builder\InvocationMocker method($constraint) |
|
10
|
|
|
*/ |
|
11
|
|
|
class TestHelpersMock implements \PHPUnit_Framework_MockObject_MockObject |
|
12
|
|
|
{ |
|
13
|
|
|
use \Ekvedaras\LaravelTestHelpers\Traits\PortToMock; |
|
14
|
|
|
|
|
15
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
|
16
|
|
|
private $mock; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject |
|
20
|
|
|
*/ |
|
21
|
|
|
public function getMock(): \PHPUnit_Framework_MockObject_MockObject |
|
22
|
|
|
{ |
|
23
|
|
|
return $this->mock; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $method |
|
28
|
|
|
* @param mixed ...$vars |
|
29
|
|
|
* @return \PHPUnit\Framework\MockObject\Builder\InvocationMocker |
|
30
|
|
|
*/ |
|
31
|
|
|
public function once(string $method, ...$vars): \PHPUnit\Framework\MockObject\Builder\InvocationMocker |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->times(1, $method, ...$vars); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $method |
|
38
|
|
|
* @param mixed ...$vars |
|
39
|
|
|
* @return \PHPUnit\Framework\MockObject\Builder\InvocationMocker |
|
40
|
|
|
*/ |
|
41
|
|
|
public function twice(string $method, ...$vars): \PHPUnit\Framework\MockObject\Builder\InvocationMocker |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->times(2, $method, ...$vars); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param int $times |
|
48
|
|
|
* @param string $method |
|
49
|
|
|
* @param mixed ...$vars |
|
50
|
|
|
* @return \PHPUnit\Framework\MockObject\Builder\InvocationMocker |
|
51
|
|
|
*/ |
|
52
|
|
|
public function times(int $times, string $method, ...$vars): \PHPUnit\Framework\MockObject\Builder\InvocationMocker |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->getMock() |
|
55
|
|
|
->expects(\PHPUnit\Framework\TestCase::exactly($times)) |
|
56
|
|
|
->method($method) |
|
57
|
|
|
->with(...$vars); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $method |
|
62
|
|
|
* @param mixed ...$vars |
|
63
|
|
|
* @return \PHPUnit\Framework\MockObject\Builder\InvocationMocker |
|
64
|
|
|
*/ |
|
65
|
|
|
public function any(string $method, ...$vars): \PHPUnit\Framework\MockObject\Builder\InvocationMocker |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->getMock() |
|
68
|
|
|
->expects(\PHPUnit\Framework\TestCase::any()) |
|
69
|
|
|
->method($method) |
|
70
|
|
|
->with(...$vars); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $method |
|
75
|
|
|
* @param mixed ...$vars |
|
76
|
|
|
* @return \PHPUnit\Framework\MockObject\Builder\InvocationMocker |
|
77
|
|
|
*/ |
|
78
|
|
|
public function consecutiveTwice(string $method, ...$vars): \PHPUnit\Framework\MockObject\Builder\InvocationMocker |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->consecutive(2, $method, ...$vars); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param int $times |
|
85
|
|
|
* @param string $method |
|
86
|
|
|
* @param mixed ...$vars |
|
87
|
|
|
* @return \PHPUnit\Framework\MockObject\Builder\InvocationMocker |
|
88
|
|
|
*/ |
|
89
|
|
|
public function consecutive( |
|
90
|
|
|
int $times, |
|
91
|
|
|
string $method, |
|
92
|
|
|
...$vars |
|
93
|
|
|
): \PHPUnit\Framework\MockObject\Builder\InvocationMocker { |
|
94
|
|
|
return $this->getMock() |
|
95
|
|
|
->expects(\PHPUnit\Framework\TestCase::exactly($times)) |
|
96
|
|
|
->method($method) |
|
97
|
|
|
->withConsecutive(...$vars); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $method |
|
102
|
|
|
* @return \PHPUnit\Framework\MockObject\Builder\InvocationMocker |
|
103
|
|
|
*/ |
|
104
|
|
|
public function never(string $method): \PHPUnit\Framework\MockObject\Builder\InvocationMocker |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->getMock()->expects(\PHPUnit\Framework\TestCase::never())->method($method); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param string $method |
|
111
|
|
|
* @param \Exception $exception |
|
112
|
|
|
* @param mixed ...$vars |
|
113
|
|
|
* @return \PHPUnit\Framework\MockObject\Builder\InvocationMocker |
|
114
|
|
|
*/ |
|
115
|
|
|
public function fail( |
|
116
|
|
|
string $method, |
|
117
|
|
|
\Exception $exception, |
|
118
|
|
|
...$vars |
|
119
|
|
|
): \PHPUnit\Framework\MockObject\Builder\InvocationMocker { |
|
120
|
|
|
return $this->any($method, ...$vars)->willThrowException($exception); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|