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

PhakeMockingStrategy   A

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 doGet() 0 4 1
A doDecorateWithMethod() 0 11 2
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