|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ShouldPHP |
|
4
|
|
|
* |
|
5
|
|
|
* @author Gabriel Jacinto <[email protected]> |
|
6
|
|
|
* @status dev |
|
7
|
|
|
* @link https://github.com/GabrielJMJ/ShouldPHP |
|
8
|
|
|
* @license MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Gabrieljmj\Should\Assert\TheParameter; |
|
12
|
|
|
|
|
13
|
|
|
use Gabrieljmj\Should\Assert\AbstractAssert; |
|
14
|
|
|
use Gabrieljmj\Should\Exception\ShouldException; |
|
15
|
|
|
|
|
16
|
|
|
abstract class AbstractParameterAssert extends AbstractAssert |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string|object |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $class; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $method; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $parameter; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string|object $class |
|
35
|
|
|
* @param string $method |
|
36
|
|
|
* @param string $parameter |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct($class, $method, $parameter) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->validateData($class, $method, $parameter); |
|
41
|
|
|
|
|
42
|
|
|
$this->class = $class; |
|
43
|
|
|
$this->method = $method; |
|
44
|
|
|
$this->parameter = $parameter; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Returns the tested element |
|
49
|
|
|
* |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getTestedElement() |
|
53
|
|
|
{ |
|
54
|
|
|
$class = $this->classToStr($this->class); |
|
55
|
|
|
return $class . '::' . $this->method . '([$' . $this->parameter . '])'; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string|object $class |
|
60
|
|
|
* @param string $method |
|
61
|
|
|
* @param string $parameter |
|
62
|
|
|
*/ |
|
63
|
|
|
private function validateData($class, $method, $parameter) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->validateClass($class); |
|
66
|
|
|
|
|
67
|
|
|
if (!method_exists($class, $method)) { |
|
68
|
|
|
ShouldException::methodDoesNotExist($class, $method); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$ref = new \ReflectionMethod($class, $method); |
|
72
|
|
|
$paramsNames = array_map(function ($param) { |
|
73
|
|
|
return $param->name; |
|
74
|
|
|
}, $ref->getParameters()); |
|
75
|
|
|
|
|
76
|
|
|
if (!in_array($parameter, $paramsNames)) { |
|
77
|
|
|
ShouldException::parameterDoesNotExist($class, $method, $parameter); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |