Passed
Push — master ( e09c3e...ddc6ca )
by Dan
03:00
created

MockWithExpectationsTrait::setInvocationFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
trait MockWithExpectationsTrait
10
{
11
    /** @var MockWithExpectations */
12
    private $mockWithExpectations;
13
14
    /** @var InvocationFactory */
15
    private $invocationFactory;
16
17
    /** @var ExpectationsFactory */
18
    private $expectationsFactory;
19
20
    /** @var MockFactory */
21
    private $mockFactory;
22
23
    /**
24
     * This trait expects to be used in a sub-class of PHPUnit\Framework\TestCase
25
     * which provides implementations of these functions:
26
     */
27
    abstract public function getMockBuilder($className);
28
    abstract public function registerMockObject(PHPUnit_Framework_MockObject_MockObject $mockObject);
29
30
    public function mockWithExpectations(
31
        $className,
32
        array $expectations = [],
33
        array $constructorArgs = null): PHPUnit_Framework_MockObject_MockObject
34
    {
35
        return $this->getMockWithExpectations()->createMockWithExpectations(
36
            $className,
37
            $expectations,
38
            $constructorArgs
39
        );
40
    }
41
42
    public function addExpectation(
43
        PHPUnit_Framework_MockObject_MockObject $mock,
44
        array $expectation) : void
45
    {
46
        $this->addExpectations($mock, [$expectation]);
47
    }
48
49
    public function addExpectations(
50
        PHPUnit_Framework_MockObject_MockObject $mock,
51
        array $expectations) : void
52
    {
53
        $this->getMockWithExpectations()->addExpectations($mock, $expectations);
54
    }
55
56
    protected function getMockWithExpectations() : MockWithExpectations
57
    {
58
        if ($this->mockWithExpectations === null) {
59
            $expectationsFactory = $this->getExpectationsFactory();
60
            $mockFactory = $this->getMockFactory();
61
            $this->mockWithExpectations = new MockWithExpectations($expectationsFactory, $mockFactory);
62
        }
63
64
        return $this->mockWithExpectations;
65
    }
66
67
    protected function setInvocationFactory(InvocationFactory $invocationFactory) : void
68
    {
69
        $this->invocationFactory = $invocationFactory;
70
    }
71
72
    protected function getInvocationFactory() : InvocationFactory
73
    {
74
        if (!$this->invocationFactory) {
75
            $this->invocationFactory = new InvocationFactory();
76
        }
77
78
        return $this->invocationFactory;
79
    }
80
81
    protected function setExpectationsFactory(ExpectationsFactory $expectationsFactory) : void
82
    {
83
        $this->expectationsFactory = $expectationsFactory;
84
    }
85
86
    protected function getExpectationsFactory() : ExpectationsFactory
87
    {
88
        if (!$this->expectationsFactory) {
89
            $invocationFactory = $this->getInvocationFactory();
90
            $this->expectationsFactory = new ExpectationsFactory($invocationFactory);
91
        }
92
93
        return $this->expectationsFactory;
94
    }
95
96
    protected function setMockFactory(MockFactory $mockFactory) : void
97
    {
98
        $this->mockFactory = $mockFactory;
99
    }
100
101
    protected function getMockFactory() : MockFactory
102
    {
103
        if (!$this->mockFactory) {
104
            if ($this instanceof TestCase) {
105
                $this->mockFactory = new MockFactory($this);
106
            } else {
107
                throw new TestException(\get_class($this).' is not an instance of '.TestCase::class);
108
            }
109
        }
110
111
        return $this->mockFactory;
112
    }
113
}
114