Expectation::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 12
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Nopolabs\Test;
5
6
use Closure;
7
use Exception;
8
use PHPUnit\Framework\TestCase;
9
use PHPUnit_Framework_MockObject_Builder_InvocationMocker;
10
use PHPUnit_Framework_MockObject_Matcher_Invocation;
11
use PHPUnit_Framework_MockObject_MockObject;
12
13
class Expectation
14
{
15
    private $method;
16
    private $params;
17
    private $result;
18
    private $throws;
19
    private $invocation;
20
21
    public function __construct(
22
        string $method,
23
        array $params = [],
24
        $result = null,
25
        $throws = null,
26
        PHPUnit_Framework_MockObject_Matcher_Invocation $invocation = null)
27
    {
28
        $this->method = $method;
29
        $this->params = $params;
30
        $this->result = $result;
31
        $this->throws = $throws;
32
        $this->invocation = $invocation ?? TestCase::any();
33
    }
34
35
    public function getMethod() : string
36
    {
37
        return $this->method;
38
    }
39
40
    public function set(PHPUnit_Framework_MockObject_MockObject $mock) : void
41
    {
42
        $builder = $this->mockExpects($mock, $this->invocation);
43
44
        $this->mockMethod($builder, $this->method);
45
46
        if (!empty($this->params)) {
47
            $this->mockParams($builder, $this->params);
48
        }
49
50
        if ($this->result !== null) {
51
            $this->mockResult($builder, $this->result);
52
        }
53
54
        if ($this->throws !== null) {
55
            $this->mockThrows($builder, $this->throws);
56
        }
57
    }
58
59
    protected function mockExpects(
60
        PHPUnit_Framework_MockObject_MockObject $mock,
61
        PHPUnit_Framework_MockObject_Matcher_Invocation $invocation) : PHPUnit_Framework_MockObject_Builder_InvocationMocker
62
    {
63
        return $mock->expects($invocation);
64
    }
65
66
    protected function mockMethod(
67
        PHPUnit_Framework_MockObject_Builder_InvocationMocker $builder,
68
        string $method) : PHPUnit_Framework_MockObject_Builder_InvocationMocker
69
    {
70
        return $builder->method($method);
71
    }
72
73
    protected function mockParams(
74
        PHPUnit_Framework_MockObject_Builder_InvocationMocker $builder,
75
        array $params) : PHPUnit_Framework_MockObject_Builder_InvocationMocker
76
    {
77
        return \call_user_func_array([$builder, 'with'], $params);
78
    }
79
80
    protected function mockResult(
81
        PHPUnit_Framework_MockObject_Builder_InvocationMocker $builder,
82
        $result) : PHPUnit_Framework_MockObject_Builder_InvocationMocker
83
    {
84
        if ($result instanceof Closure) {
85
            return $builder->willReturnCallback($result);
86
        }
87
88
        return $builder->willReturn($result);
89
    }
90
91
    protected function mockThrows(
92
        PHPUnit_Framework_MockObject_Builder_InvocationMocker $builder,
93
        $throws) : PHPUnit_Framework_MockObject_Builder_InvocationMocker
94
    {
95
        if (\is_string($throws)) {
96
            $throws = new Exception($throws);
97
        }
98
99
        return $builder->willThrowException($throws);
100
    }
101
}
102