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

PhakeMockingStrategy::doDecorateWithMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 9.4285
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
    const CLASS_NAME = Phake::class;
21
    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 23
    protected function doBuild(string $fqcn)
40
    {
41 23
        return Phake::mock($fqcn);
42
    }
43
44
    /**
45
     * @param PhakeMock $mock
46
     * @param MethodStub $stub
47
     * @return void
48
     */
49 22
    protected function doDecorateWithMethod($mock, MethodStub $stub)
50
    {
51 22
        $methodName = $stub->getName();
52 22
        $methodValue = $stub->getValue();
53
54
        /** @var AnswerBinderProxy $partial */
55 22
        $partial = Phake::when($mock)->$methodName(new FirstStubMatcher($mock, $methodName));
56 22
        $methodValue instanceof \Throwable
57 22
            ? $partial->thenThrow($methodValue)
58 22
            : $partial->thenReturn($methodValue);
59
    }
60
61
    /**
62
     * @param PhakeMock $mock
63
     * @return PhakeMock
64
     */
65 12
    protected function doGet($mock)
66
    {
67 12
        return $mock;
68
    }
69
}
70