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
|
|
|
protected 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
|
|
|
protected function addExpectation( |
29
|
|
|
PHPUnit_Framework_MockObject_MockObject $mock, |
30
|
|
|
array $expectation) : void |
31
|
|
|
{ |
32
|
|
|
$this->addExpectations($mock, [$expectation]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected 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
|
|
|
if ($this instanceof TestCase) { |
46
|
|
|
$invocationFactory = new InvocationFactory(); |
47
|
|
|
$expectationsFactory = new ExpectationsFactory($invocationFactory); |
48
|
|
|
$mockFactory = new MockFactory($this); |
49
|
|
|
$this->mockWithExpectations = new MockWithExpectations($expectationsFactory, $mockFactory); |
50
|
|
|
} else { |
51
|
|
|
throw new TestException(\get_class($this).' is not an instance of '.TestCase::class); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->mockWithExpectations; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|