99
1010class DefinitionHasArgumentConstraint extends Constraint
1111{
12+
13+ /**
14+ * @var int|string
15+ */
1216 private $ argumentIndex ;
1317 private $ expectedValue ;
1418 private $ checkExpectedValue ;
@@ -17,13 +21,36 @@ public function __construct($argumentIndex, $expectedValue, $checkExpectedValue
1721 {
1822 parent ::__construct ();
1923
20- $ this ->argumentIndex = (integer )$ argumentIndex ;
24+ if (!(is_string ($ argumentIndex ) || (is_int ($ argumentIndex ) && $ argumentIndex >= 0 ))) {
25+ throw new \InvalidArgumentException ('Expected either a string or a positive integer for $argumentIndex. ' );
26+ }
27+
28+ if (is_string ($ argumentIndex )) {
29+ if ('' === $ argumentIndex ) {
30+ throw new \InvalidArgumentException ('A named argument must begin with a "$". ' );
31+ }
32+
33+ if ('$ ' !== $ argumentIndex [0 ]) {
34+ throw new \InvalidArgumentException (
35+ sprintf ('Unknown argument "%s". Did you mean "$%s"? ' , $ argumentIndex , $ argumentIndex )
36+ );
37+ }
38+ }
39+
40+ $ this ->argumentIndex = $ argumentIndex ;
2141 $ this ->expectedValue = $ expectedValue ;
2242 $ this ->checkExpectedValue = $ checkExpectedValue ;
2343 }
2444
2545 public function toString ()
2646 {
47+ if (is_string ($ this ->argumentIndex )) {
48+ return sprintf (
49+ 'has an argument named "%s" with the given value ' ,
50+ $ this ->argumentIndex
51+ );
52+ }
53+
2754 return sprintf (
2855 'has an argument with index %d with the given value ' ,
2956 $ this ->argumentIndex
@@ -65,13 +92,13 @@ private function evaluateArgumentIndex(Definition $definition, $returnResult)
6592 return false ;
6693 }
6794
68- $ this ->fail (
69- $ definition,
70- sprintf (
71- 'The definition has no argument with index %d ' ,
72- $ this -> argumentIndex
73- )
74- );
95+ if ( is_string ( $ this ->argumentIndex )) {
96+ $ message = sprintf ( ' The definition has no argument named "%s" ' , $ this -> argumentIndex );
97+ } else {
98+ $ message = sprintf ( 'The definition has no argument with index %d ' , $ this -> argumentIndex );
99+ }
100+
101+ $ this -> fail ( $ definition , $ message );
75102 }
76103
77104 return true ;
@@ -88,15 +115,23 @@ private function evaluateArgumentValue(Definition $definition, $returnResult)
88115 return false ;
89116 }
90117
91- $ this ->fail (
92- $ definition ,
93- sprintf (
118+ if (is_string ($ this ->argumentIndex )) {
119+ $ message = sprintf (
120+ 'The value of argument named "%s" (%s) is not equal to the expected value (%s) ' ,
121+ $ this ->argumentIndex ,
122+ $ this ->exporter ->export ($ actualValue ),
123+ $ this ->exporter ->export ($ this ->expectedValue )
124+ );
125+ } else {
126+ $ message = sprintf (
94127 'The value of argument with index %d (%s) is not equal to the expected value (%s) ' ,
95128 $ this ->argumentIndex ,
96129 $ this ->exporter ->export ($ actualValue ),
97130 $ this ->exporter ->export ($ this ->expectedValue )
98- )
99- );
131+ );
132+ }
133+
134+ $ this ->fail ($ definition , $ message );
100135 }
101136
102137 return true ;
0 commit comments