Completed
Push — master ( dbd889...95c92d )
by Alberto
02:45
created

ProphecyMockingStrategy::doDecorate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Strategy;
5
6
use Moka\Exception\MockNotCreatedException;
7
use Moka\Strategy\Prophecy\NoPriorityToken;
8
use Moka\Stub\Stub;
9
use Moka\Stub\StubSet;
10
use Prophecy\Exception\Prophecy\ObjectProphecyException;
11
use Prophecy\Prophecy\ObjectProphecy;
12
use Prophecy\Prophet;
13
14
/**
15
 * Class ProphecyMockingStrategy
16
 * @package Moka\Strategy
17
 */
18
class ProphecyMockingStrategy extends AbstractMockingStrategy
19
{
20
    /**
21
     * @var Prophet
22
     */
23
    private $prophet;
24
25
    /**
26
     * PHPUnitMockingStrategy constructor.
27
     */
28 2
    public function __construct()
29
    {
30 2
        $this->prophet = new Prophet();
31 2
        $this->setMockType(ObjectProphecy::class);
32
    }
33
34
    /**
35
     * @param string $fqcn
36
     * @return ObjectProphecy
37
     */
38 4
    protected function doBuild(string $fqcn)
39
    {
40 4
        return $this->prophet->prophesize($fqcn);
41
    }
42
43
    /**
44
     * @param ObjectProphecy $mock
45
     * @param StubSet $stubs
46
     * @return void
47
     */
48 1 View Code Duplication
    protected function doDecorate($mock, StubSet $stubs)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
        /** @var Stub $stub */
51 1
        foreach ($stubs as $stub) {
52 1
            $methodValue = $stub->getMethodValue();
53
54 1
            $partial = $mock->{$stub->getMethodName()}(new NoPriorityToken());
55 1
            $methodValue instanceof \Throwable
56 1
                ? $partial->willThrow($methodValue)
57 1
                : $partial->willReturn($methodValue);
58
        }
59
    }
60
61
    /**
62
     * @param object $mock
63
     * @return mixed
64
     *
65
     * @throws MockNotCreatedException
66
     */
67 4
    protected function doGet($mock)
68
    {
69
        try {
70 4
            return $mock->reveal();
71 1
        } catch (ObjectProphecyException $exception) {
72 1
            throw new MockNotCreatedException(
73 1
                sprintf(
74 1
                    'Cannot create mock object: %s',
75 1
                    $exception->getMessage()
76
                )
77
            );
78
        }
79
    }
80
}
81