Completed
Push — master ( 7ee214...32fd8d )
by Alberto
19s
created

Moka/Plugin/Prophecy/ProphecyMockingStrategy.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Plugin\Prophecy;
5
6
use Moka\Exception\MissingDependencyException;
7
use Moka\Exception\MockNotCreatedException;
8
use Moka\Plugin\Prophecy\Token\MaxPriorityToken;
9
use Moka\Strategy\AbstractMockingStrategy;
10
use Moka\Stub\MethodStub;
11
use Prophecy\Exception\Prophecy\ObjectProphecyException;
12
use Prophecy\Prophecy\MethodProphecy;
13
use Prophecy\Prophecy\ObjectProphecy;
14
use Prophecy\Prophet;
15
16
/**
17
 * Class ProphecyMockingStrategy
18
 * @package Moka\Strategy
19
 */
20
class ProphecyMockingStrategy extends AbstractMockingStrategy
21
{
22
    const CLASS_NAME = Prophet::class;
23
    const PACKAGE_NAME = 'phpspec/prophecy';
24
25
    /**
26
     * @var Prophet
27
     */
28
    private $prophet;
29
30
    /**
31
     * PHPUnitMockingStrategy constructor.
32
     *
33
     * @throws MissingDependencyException
34
     */
35 2
    public function __construct()
36
    {
37 2
        self::checkDependencies(self::CLASS_NAME, self::PACKAGE_NAME);
38
39 2
        $this->prophet = new Prophet();
40 2
        $this->setMockType(ObjectProphecy::class);
41
    }
42
43
    /**
44
     * @param string $fqcn
45
     * @return ObjectProphecy
46
     */
47 27
    protected function doBuild(string $fqcn)
48
    {
49 27
        return $this->prophet->prophesize($fqcn);
50
    }
51
52
    /**
53
     * @param ObjectProphecy $mock
54
     * @param MethodStub $stub
55
     * @return void
56
     */
57 25 View Code Duplication
    protected function doDecorateWithMethod($mock, MethodStub $stub)
1 ignored issue
show
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...
58
    {
59 25
        $methodName = $stub->getName();
60 25
        $methodValue = $stub->getValue();
61
62
        /** @var MethodProphecy $partial */
63 25
        $partial = $mock->$methodName(new MaxPriorityToken());
64 25
        $methodValue instanceof \Throwable
65 25
            ? $partial->willThrow($methodValue)
66 25
            : $partial->willReturn($methodValue);
67
    }
68
69
    /**
70
     * @param ObjectProphecy $mock
71
     * @return mixed
72
     *
73
     * @throws MockNotCreatedException
74
     */
75 16
    protected function doGet($mock)
76
    {
77
        try {
78 16
            return $mock->reveal();
79 1
        } catch (ObjectProphecyException $exception) {
80 1
            throw new MockNotCreatedException(
81 1
                sprintf(
82 1
                    'Cannot create mock object: %s',
83 1
                    $exception->getMessage()
84
                )
85
            );
86
        }
87
    }
88
89
    /**
90
     * @param ObjectProphecy $mock
91
     * @param string $methodName
92
     * @return object
93
     */
94 2
    protected function doCall($mock, string $methodName)
95
    {
96 2
        return new MethodProphecyHelper($mock, $methodName);
97
    }
98
}
99