Completed
Push — master ( 9e17cb...0c8038 )
by Dan
04:15
created

MockWithExpectationsTrait::addExpectations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
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