Completed
Push — master ( b76a97...9e17cb )
by Dan
01:36
created

InvocationFactory::prepareInvocationString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Nopolabs\Test;
5
6
7
use PHPUnit\Framework\TestCase;
8
use PHPUnit_Framework_MockObject_Matcher_Invocation;
9
10
class InvocationFactory
11
{
12
    public function prepareInvocation($invoked) : PHPUnit_Framework_MockObject_Matcher_Invocation
13
    {
14
        if ($invoked === null) {
15
            return TestCase::any();
16
        }
17
18
        if (\is_object($invoked)) {
19
            return $invoked;
20
        }
21
22
        if (\is_numeric($invoked)) {
23
            return $this->prepareInvocationNumeric((int)$invoked);
24
        }
25
26
        if (\is_string($invoked)) {
27
            return $this->prepareInvocationString($invoked);
28
        }
29
30
        throw new TestException("prepareInvocation cannot prepare '$invoked'");
31
    }
32
33
    private function prepareInvocationNumeric(int $times): PHPUnit_Framework_MockObject_Matcher_Invocation
34
    {
35
        switch ($times) {
36
            case 0:
37
                return TestCase::never();
38
            case 1:
39
                return TestCase::once();
40
            default:
41
                return TestCase::exactly($times);
42
        }
43
    }
44
45
    private function prepareInvocationString(string $invoked): PHPUnit_Framework_MockObject_Matcher_Invocation
46
    {
47
        $parsed = $this->parseInvoked($invoked);
48
49
        if (isset($parsed['method'], $parsed['count'])) {
50
            return $this->prepareInvocationMethodCount($parsed['method'], (int)$parsed['count']);
51
        }
52
53
        if (isset($parsed['method'])) {
54
            return $this->prepareInvocationMethod($parsed['method']);
55
        }
56
57
        throw new TestException("prepareInvocationString cannot handle '$invoked'");
58
    }
59
60
    private function parseInvoked(string $invoked) : array
61
    {
62
        if (preg_match("/^(?'method'\w+)(?:\s+(?'count'\d+))?$/", $invoked, $matches)) {
63
            return $matches;
64
        }
65
66
        return [];
67
    }
68
69
    private function prepareInvocationMethodCount(string $method, int $count) : PHPUnit_Framework_MockObject_Matcher_Invocation
70
    {
71
        switch ($method) {
72
            case 'atLeast':
73
                return TestCase::atLeast($count);
74
            case 'exactly':
75
                return TestCase::exactly($count);
76
            case 'atMost':
77
                return TestCase::atMost($count);
78
        }
79
80
        throw new TestException("prepareInvocationMethodCount cannot handle '$method $count'");
81
    }
82
83
    private function prepareInvocationMethod($method) : PHPUnit_Framework_MockObject_Matcher_Invocation
84
    {
85
        switch ($method) {
86
            case 'once':
87
                return TestCase::once();
88
            case 'any':
89
                return TestCase::any();
90
            case 'never':
91
                return TestCase::never();
92
            case 'atLeastOnce':
93
                return TestCase::atLeastOnce();
94
        }
95
96
        throw new TestException("prepareInvocationMethod cannot handle '$method'");
97
    }
98
}
99