Passed
Push — master ( a7f03b...b76a97 )
by Dan
01:56
created

MockWithExpectationsTrait::addExpectation()   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
    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