|
1
|
|
|
<?php |
|
2
|
|
|
namespace Agavi\Testing\PHPUnit\Constraint; |
|
3
|
|
|
use Agavi\Controller\Controller; |
|
4
|
|
|
use Agavi\Testing\BaseConstraintBecausePhpunitSucksAtBackwardsCompatibility; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Constraint that checks if an Controller handles an expected request method. |
|
10
|
|
|
* |
|
11
|
|
|
* The Controller instance is passed to the constructor. |
|
12
|
|
|
* |
|
13
|
|
|
* @package agavi |
|
14
|
|
|
* @subpackage testing |
|
15
|
|
|
* |
|
16
|
|
|
* @author Felix Gilcher <[email protected]> |
|
17
|
|
|
* @copyright The Agavi Project |
|
18
|
|
|
* |
|
19
|
|
|
* @since 1.0.0 |
|
20
|
|
|
* |
|
21
|
|
|
* @version $Id$ |
|
22
|
|
|
*/ |
|
23
|
|
|
class ConstraintControllerHandlesMethod extends BaseConstraintBecausePhpunitSucksAtBackwardsCompatibility |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var Controller The Controller instance. |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $controllerInstance; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var bool Whether generic 'execute' methods should be accepted. |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $acceptGeneric; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Class constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param Controller $controllerInstance Instance of the Controller to test. |
|
39
|
|
|
* @param bool $acceptGeneric Whether generic execute methods should be accepted. |
|
40
|
|
|
* |
|
41
|
|
|
* @author Felix Gilcher <[email protected]> |
|
42
|
|
|
* @since 1.0.0 |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(Controller $controllerInstance, $acceptGeneric = true) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->controllerInstance = $controllerInstance; |
|
47
|
|
|
$this->acceptGeneric = $acceptGeneric; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Evaluates the constraint for parameter $other. Returns TRUE if the |
|
52
|
|
|
* constraint is met, FALSE otherwise. |
|
53
|
|
|
* |
|
54
|
|
|
* @param mixed $other Value or object to evaluate. |
|
55
|
|
|
* |
|
56
|
|
|
* @return bool The result of the evaluation. |
|
57
|
|
|
* |
|
58
|
|
|
* @author Felix Gilcher <[email protected]> |
|
59
|
|
|
* @since 1.0.7 |
|
60
|
|
|
*/ |
|
61
|
|
View Code Duplication |
public function matches($other) |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
$executeMethod = 'execute' . $other; |
|
64
|
|
|
if(is_callable(array($this->controllerInstance, $executeMethod)) || ($this->acceptGeneric && is_callable(array($this->controllerInstance, 'execute')))) { |
|
65
|
|
|
return true; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns a string representation of the constraint. |
|
73
|
|
|
* |
|
74
|
|
|
* @return string The string representation. |
|
75
|
|
|
* |
|
76
|
|
|
* @author Felix Gilcher <[email protected]> |
|
77
|
|
|
* @since 1.0.0 |
|
78
|
|
|
*/ |
|
79
|
|
|
public function toString() |
|
80
|
|
|
{ |
|
81
|
|
|
return sprintf( |
|
82
|
|
|
'%1$s handles method', |
|
83
|
|
|
get_class($this->controllerInstance) |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Returns a custom error description. |
|
89
|
|
|
* |
|
90
|
|
|
* @param mixed $other Value or object to evaluate. |
|
91
|
|
|
* @param string $description The original description. |
|
92
|
|
|
* @param bool $not true if the constraint was negated. |
|
93
|
|
|
* |
|
94
|
|
|
* @return string The error description. |
|
95
|
|
|
* |
|
96
|
|
|
* @author Felix Gilcher <[email protected]> |
|
97
|
|
|
* @since 1.0.0 |
|
98
|
|
|
*/ |
|
99
|
|
View Code Duplication |
protected function customFailureDescription($other, $description, $not) |
|
|
|
|
|
|
100
|
|
|
{ |
|
101
|
|
|
if($not) { |
|
102
|
|
|
return sprintf( |
|
103
|
|
|
'Failed asserting that %1$s does not handle method "%2$s".', |
|
104
|
|
|
get_class($this->controllerInstance), |
|
105
|
|
|
$other |
|
106
|
|
|
); |
|
107
|
|
|
} else { |
|
108
|
|
|
return sprintf( |
|
109
|
|
|
'Failed asserting that %1$s handles method "%2$s".', |
|
110
|
|
|
get_class($this->controllerInstance), |
|
111
|
|
|
$other |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
?> |
|
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.