Completed
Pull Request — master (#6)
by Angelo
02:47
created

PHPUnitMockingStrategy::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Strategy;
5
6
use Moka\Exception\InvalidArgumentException;
7
use Moka\Exception\MockNotCreatedException;
8
use Moka\Stub\Stub;
9
use Moka\Stub\StubSet;
10
use PHPUnit_Framework_MockObject_Generator as MockGenerator;
11
use PHPUnit_Framework_MockObject_MockObject as MockObject;
12
13
/**
14
 * Class PHPUnitMockingStrategy
15
 * @package Moka\Strategy
16
 */
17
class PHPUnitMockingStrategy extends AbstractMockingStrategy
18
{
19
    /**
20
     * @var MockGenerator
21
     */
22
    private $generator;
23
24
    /**
25
     * PHPUnitMockingStrategy constructor.
26
     */
27 12
    public function __construct()
28
    {
29 12
        $this->generator = new MockGenerator();
30 12
        $this->setMockType(MockObject::class);
31
    }
32
33
    /**
34
     * @param string $fqcn
35
     * @return MockObject
36
     *
37
     * @throws MockNotCreatedException
38
     */
39 15
    public function build(string $fqcn)
40
    {
41
        try {
42 15
            return $this->generator->getMock(
43 15
                $fqcn,
44 15
                $methods = [],
45 15
                $arguments = [],
46 15
                $mockClassName = '',
47 15
                $callOriginalConstructor = false
48
            );
49 2
        } catch (\Throwable $exception) {
50
            // Use \Throwable to catch \ParseError too.
51 2
            throw new MockNotCreatedException(
52 2
                sprintf(
53 2
                    'Unable to create mock object for FQCN %s: %s',
54 2
                    $fqcn,
55 2
                    $exception->getMessage()
56
                )
57
            );
58
        }
59
    }
60
61
    /**
62
     * @param MockObject $mock
63
     * @param StubSet $stubs
64
     * @return void
65
     *
66
     * @throws InvalidArgumentException
67
     */
68 12 View Code Duplication
    public function decorate($mock, StubSet $stubs)
69
    {
70 12
        $this->checkMockType($mock);
71
72
        /** @var Stub $stub */
73 11
        foreach ($stubs as $stub) {
74 5
            $methodValue = $stub->getMethodValue();
75
76 5
            $partial = $mock->method($stub->getMethodName());
77 5
            $methodValue instanceof \Throwable
78 2
                ? $partial->willThrowException($methodValue)
79 5
                : $partial->willReturn($methodValue);
80
        }
81
    }
82
83
    /**
84
     * @param MockObject $mock
85
     * @return MockObject
86
     *
87
     * @throws InvalidArgumentException
88
     */
89 13
    public function get($mock)
90
    {
91 13
        $this->checkMockType($mock);
92
93 12
        return $mock;
94
    }
95
}
96