PhakeMockingStrategy::doDecorateWithMethod()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 9.9
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Plugin\Phake;
5
6
use Moka\Exception\MissingDependencyException;
7
use Moka\Plugin\Phake\Matcher\FirstStubMatcher;
8
use Moka\Strategy\AbstractMockingStrategy;
9
use Moka\Stub\MethodStub;
10
use Phake;
11
use Phake_IMock as PhakeMock;
12
use Phake_Proxies_AnswerBinderProxy as AnswerBinderProxy;
13
14
/**
15
 * Class PhakeMockingStrategy
16
 * @package Moka\Plugin\Phake
17
 */
18
class PhakeMockingStrategy extends AbstractMockingStrategy
19
{
20
    private const CLASS_NAME = Phake::class;
21
    private const PACKAGE_NAME = 'phake/phake';
22
23
    /**
24
     * PhakeMockingStrategy constructor.
25
     *
26
     * @throws MissingDependencyException
27
     */
28 2
    public function __construct()
29
    {
30 2
        self::checkDependencies(self::CLASS_NAME, self::PACKAGE_NAME);
31
32 2
        $this->setMockType(PhakeMock::class);
33
    }
34
35
    /**
36
     * @param string $fqcn
37
     * @return PhakeMock
38
     */
39 18
    protected function doBuild(string $fqcn): PhakeMock
40
    {
41 18
        return Phake::mock($fqcn);
42
    }
43
44
    /**
45
     * @param PhakeMock $mock
46
     * @param MethodStub $stub
47
     * @return void
48
     */
49 17
    protected function doDecorateWithMethod($mock, MethodStub $stub): void
50
    {
51 17
        $methodName = $stub->getName();
52 17
        $methodValue = $stub->getValue();
53
54
        /** @var AnswerBinderProxy $partial */
55 17
        $partial = Phake::when($mock)->$methodName(new FirstStubMatcher($mock, $methodName));
56 17
        $methodValue instanceof \Throwable
57 17
            ? $partial->thenThrow($methodValue)
58 17
            : $partial->thenReturn($methodValue);
59
    }
60
61
    /**
62
     * @param PhakeMock $mock
63
     * @return PhakeMock
64
     */
65 14
    protected function doGet($mock): PhakeMock
66
    {
67 14
        return $mock;
68
    }
69
}
70