|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Nopolabs\Test; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* This trait expects to be used in a sub-class of PHPUnit\Framework\TestCase |
|
11
|
|
|
*/ |
|
12
|
|
|
trait MockWithExpectationsTrait |
|
13
|
|
|
{ |
|
14
|
|
|
private $mockWithExpectations; |
|
15
|
|
|
|
|
16
|
|
|
public function mockWithExpectations( |
|
17
|
|
|
$className, |
|
18
|
|
|
array $expectations = [], |
|
19
|
|
|
array $constructorArgs = null): PHPUnit_Framework_MockObject_MockObject |
|
20
|
|
|
{ |
|
21
|
|
|
return $this->getMockWithExpectations()->createMockWithExpectations( |
|
22
|
|
|
$className, |
|
23
|
|
|
$expectations, |
|
24
|
|
|
$constructorArgs |
|
25
|
|
|
); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function addExpectation( |
|
29
|
|
|
PHPUnit_Framework_MockObject_MockObject $mock, |
|
30
|
|
|
array $expectation) : void |
|
31
|
|
|
{ |
|
32
|
|
|
$this->addExpectations($mock, [$expectation]); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function addExpectations( |
|
36
|
|
|
PHPUnit_Framework_MockObject_MockObject $mock, |
|
37
|
|
|
array $expectations) : void |
|
38
|
|
|
{ |
|
39
|
|
|
$this->getMockWithExpectations()->addExpectations($mock, $expectations); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function getMockWithExpectations() : MockWithExpectations |
|
43
|
|
|
{ |
|
44
|
|
|
if ($this->mockWithExpectations === null) { |
|
45
|
|
|
$expectationsFactory = $this->getExpectationsFactory(); |
|
46
|
|
|
$mockFactory = $this->getMockFactory(); |
|
47
|
|
|
$this->mockWithExpectations = new MockWithExpectations($expectationsFactory, $mockFactory); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $this->mockWithExpectations; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function getInvocationFactory() : InvocationFactory |
|
54
|
|
|
{ |
|
55
|
|
|
return new InvocationFactory(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function getExpectationsFactory() : ExpectationsFactory |
|
59
|
|
|
{ |
|
60
|
|
|
$invocationFactory = $this->getInvocationFactory(); |
|
61
|
|
|
|
|
62
|
|
|
return new ExpectationsFactory($invocationFactory); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function getMockFactory() : MockFactory |
|
66
|
|
|
{ |
|
67
|
|
|
if ($this instanceof TestCase) { |
|
68
|
|
|
return new MockFactory($this); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
throw new TestException(\get_class($this).' is not an instance of '.TestCase::class); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|