ProphecyMockingStrategy   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 79
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A doBuild() 0 4 1
A doDecorateWithMethod() 0 11 2
A doGet() 0 13 2
A doCall() 0 4 1
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
    private const CLASS_NAME = Prophet::class;
23
    private 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 22
    protected function doBuild(string $fqcn): ObjectProphecy
48
    {
49 22
        return $this->prophet->prophesize($fqcn);
50
    }
51
52
    /**
53
     * @param ObjectProphecy $mock
54
     * @param MethodStub $stub
55
     * @return void
56
     */
57 20
    protected function doDecorateWithMethod($mock, MethodStub $stub): void
58
    {
59 20
        $methodName = $stub->getName();
60 20
        $methodValue = $stub->getValue();
61
62
        /** @var MethodProphecy $partial */
63 20
        $partial = $mock->$methodName(new MaxPriorityToken());
64 20
        $methodValue instanceof \Throwable
65 20
            ? $partial->willThrow($methodValue)
66 20
            : $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