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

MockWithExpectationsTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A mockWithExpectations() 0 9 1
A addExpectation() 0 5 1
A getMockWithExpectations() 0 14 3
A addExpectations() 0 5 1
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