PhakeMockingStrategy   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A doBuild() 0 4 1
A doDecorateWithMethod() 0 11 2
A doGet() 0 4 1
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