Completed
Push — master ( 4848aa...64d9da )
by Dmitry
03:44
created

Mock::handler()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 9
cp 0.7778
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
crap 3.0987
1
<?php
2
3
namespace Basis\Test;
4
5
use Basis\Converter;
6
use Basis\Test;
7
use Exception;
8
9
class Mock
10
{
11
    private $converter;
12
    private $test;
13
14
    public $params;
15
    public $result;
16
    public $calls = 0;
17
18 4
    public function __construct(Converter $converter, Test $test)
19
    {
20 4
        $this->converter = $converter;
21 4
        $this->test = $test;
22 4
    }
23
24
    public function withParams($params)
25
    {
26
        $this->params = $params;
27
        return $this;
28
    }
29
30 4
    public function handler($result)
31
    {
32 4
        $this->result = $result;
33 4
        if (is_string($result)) {
34
            if (!method_exists($this->test, $result)) {
35
                throw new Exception("Invalid method ".get_class($this->test)."::$result");
36
            }
37 1
            $this->result = function($params) use ($result) {
38 1
                $params = $this->converter->toObject($params);
39 1
                return $this->test->$result($params);
40
            };
41
        }
42 4
        return $this;
43
    }
44
45 1
    public function willDo($result)
46
    {
47 1
        return $this->handler($result);
48
    }
49
50 4
    public function willReturn($result)
51
    {
52 4
        return $this->handler($result);
53
    }
54
}
55