@@ -15,41 +15,41 @@ |
||
15 | 15 | */ |
16 | 16 | class LessThan extends Constraint |
17 | 17 | { |
18 | - /** |
|
19 | - * @var int|float |
|
20 | - */ |
|
21 | - protected $value; |
|
18 | + /** |
|
19 | + * @var int|float |
|
20 | + */ |
|
21 | + protected $value; |
|
22 | 22 | |
23 | - /** |
|
24 | - * @param int|float $value |
|
25 | - */ |
|
26 | - public function __construct($value) |
|
27 | - { |
|
28 | - parent::__construct(); |
|
23 | + /** |
|
24 | + * @param int|float $value |
|
25 | + */ |
|
26 | + public function __construct($value) |
|
27 | + { |
|
28 | + parent::__construct(); |
|
29 | 29 | |
30 | - $this->value = $value; |
|
31 | - } |
|
30 | + $this->value = $value; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Evaluates the constraint for parameter $other. Returns true if the |
|
35 | - * constraint is met, false otherwise. |
|
36 | - * |
|
37 | - * @param mixed $other Value or object to evaluate. |
|
38 | - * |
|
39 | - * @return bool |
|
40 | - */ |
|
41 | - protected function matches($other) |
|
42 | - { |
|
43 | - return $this->value > $other; |
|
44 | - } |
|
33 | + /** |
|
34 | + * Evaluates the constraint for parameter $other. Returns true if the |
|
35 | + * constraint is met, false otherwise. |
|
36 | + * |
|
37 | + * @param mixed $other Value or object to evaluate. |
|
38 | + * |
|
39 | + * @return bool |
|
40 | + */ |
|
41 | + protected function matches($other) |
|
42 | + { |
|
43 | + return $this->value > $other; |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Returns a string representation of the constraint. |
|
48 | - * |
|
49 | - * @return string |
|
50 | - */ |
|
51 | - public function toString() |
|
52 | - { |
|
53 | - return 'is less than ' . $this->exporter->export($this->value); |
|
54 | - } |
|
46 | + /** |
|
47 | + * Returns a string representation of the constraint. |
|
48 | + * |
|
49 | + * @return string |
|
50 | + */ |
|
51 | + public function toString() |
|
52 | + { |
|
53 | + return 'is less than ' . $this->exporter->export($this->value); |
|
54 | + } |
|
55 | 55 | } |
@@ -14,76 +14,76 @@ |
||
14 | 14 | |
15 | 15 | class Exception extends Constraint |
16 | 16 | { |
17 | - /** |
|
18 | - * @var string |
|
19 | - */ |
|
20 | - protected $className; |
|
17 | + /** |
|
18 | + * @var string |
|
19 | + */ |
|
20 | + protected $className; |
|
21 | 21 | |
22 | - /** |
|
23 | - * @param string $className |
|
24 | - */ |
|
25 | - public function __construct($className) |
|
26 | - { |
|
27 | - parent::__construct(); |
|
28 | - $this->className = $className; |
|
29 | - } |
|
22 | + /** |
|
23 | + * @param string $className |
|
24 | + */ |
|
25 | + public function __construct($className) |
|
26 | + { |
|
27 | + parent::__construct(); |
|
28 | + $this->className = $className; |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * Evaluates the constraint for parameter $other. Returns true if the |
|
33 | - * constraint is met, false otherwise. |
|
34 | - * |
|
35 | - * @param mixed $other Value or object to evaluate. |
|
36 | - * |
|
37 | - * @return bool |
|
38 | - */ |
|
39 | - protected function matches($other) |
|
40 | - { |
|
41 | - return $other instanceof $this->className; |
|
42 | - } |
|
31 | + /** |
|
32 | + * Evaluates the constraint for parameter $other. Returns true if the |
|
33 | + * constraint is met, false otherwise. |
|
34 | + * |
|
35 | + * @param mixed $other Value or object to evaluate. |
|
36 | + * |
|
37 | + * @return bool |
|
38 | + */ |
|
39 | + protected function matches($other) |
|
40 | + { |
|
41 | + return $other instanceof $this->className; |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Returns the description of the failure |
|
46 | - * |
|
47 | - * The beginning of failure messages is "Failed asserting that" in most |
|
48 | - * cases. This method should return the second part of that sentence. |
|
49 | - * |
|
50 | - * @param mixed $other Evaluated value or object. |
|
51 | - * |
|
52 | - * @return string |
|
53 | - */ |
|
54 | - protected function failureDescription($other) |
|
55 | - { |
|
56 | - if ($other !== null) { |
|
57 | - $message = ''; |
|
58 | - if ($other instanceof Throwable) { |
|
59 | - $message = '. Message was: "' . $other->getMessage() . '" at' |
|
60 | - . "\n" . Filter::getFilteredStacktrace($other); |
|
61 | - } |
|
44 | + /** |
|
45 | + * Returns the description of the failure |
|
46 | + * |
|
47 | + * The beginning of failure messages is "Failed asserting that" in most |
|
48 | + * cases. This method should return the second part of that sentence. |
|
49 | + * |
|
50 | + * @param mixed $other Evaluated value or object. |
|
51 | + * |
|
52 | + * @return string |
|
53 | + */ |
|
54 | + protected function failureDescription($other) |
|
55 | + { |
|
56 | + if ($other !== null) { |
|
57 | + $message = ''; |
|
58 | + if ($other instanceof Throwable) { |
|
59 | + $message = '. Message was: "' . $other->getMessage() . '" at' |
|
60 | + . "\n" . Filter::getFilteredStacktrace($other); |
|
61 | + } |
|
62 | 62 | |
63 | - return \sprintf( |
|
64 | - 'exception of type "%s" matches expected exception "%s"%s', |
|
65 | - \get_class($other), |
|
66 | - $this->className, |
|
67 | - $message |
|
68 | - ); |
|
69 | - } |
|
63 | + return \sprintf( |
|
64 | + 'exception of type "%s" matches expected exception "%s"%s', |
|
65 | + \get_class($other), |
|
66 | + $this->className, |
|
67 | + $message |
|
68 | + ); |
|
69 | + } |
|
70 | 70 | |
71 | - return \sprintf( |
|
72 | - 'exception of type "%s" is thrown', |
|
73 | - $this->className |
|
74 | - ); |
|
75 | - } |
|
71 | + return \sprintf( |
|
72 | + 'exception of type "%s" is thrown', |
|
73 | + $this->className |
|
74 | + ); |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * Returns a string representation of the constraint. |
|
79 | - * |
|
80 | - * @return string |
|
81 | - */ |
|
82 | - public function toString() |
|
83 | - { |
|
84 | - return \sprintf( |
|
85 | - 'exception of type "%s"', |
|
86 | - $this->className |
|
87 | - ); |
|
88 | - } |
|
77 | + /** |
|
78 | + * Returns a string representation of the constraint. |
|
79 | + * |
|
80 | + * @return string |
|
81 | + */ |
|
82 | + public function toString() |
|
83 | + { |
|
84 | + return \sprintf( |
|
85 | + 'exception of type "%s"', |
|
86 | + $this->className |
|
87 | + ); |
|
88 | + } |
|
89 | 89 | } |
@@ -15,40 +15,40 @@ |
||
15 | 15 | */ |
16 | 16 | class StringStartsWith extends Constraint |
17 | 17 | { |
18 | - /** |
|
19 | - * @var string |
|
20 | - */ |
|
21 | - protected $prefix; |
|
18 | + /** |
|
19 | + * @var string |
|
20 | + */ |
|
21 | + protected $prefix; |
|
22 | 22 | |
23 | - /** |
|
24 | - * @param string $prefix |
|
25 | - */ |
|
26 | - public function __construct($prefix) |
|
27 | - { |
|
28 | - parent::__construct(); |
|
29 | - $this->prefix = $prefix; |
|
30 | - } |
|
23 | + /** |
|
24 | + * @param string $prefix |
|
25 | + */ |
|
26 | + public function __construct($prefix) |
|
27 | + { |
|
28 | + parent::__construct(); |
|
29 | + $this->prefix = $prefix; |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * Evaluates the constraint for parameter $other. Returns true if the |
|
34 | - * constraint is met, false otherwise. |
|
35 | - * |
|
36 | - * @param mixed $other Value or object to evaluate. |
|
37 | - * |
|
38 | - * @return bool |
|
39 | - */ |
|
40 | - protected function matches($other) |
|
41 | - { |
|
42 | - return \strpos($other, $this->prefix) === 0; |
|
43 | - } |
|
32 | + /** |
|
33 | + * Evaluates the constraint for parameter $other. Returns true if the |
|
34 | + * constraint is met, false otherwise. |
|
35 | + * |
|
36 | + * @param mixed $other Value or object to evaluate. |
|
37 | + * |
|
38 | + * @return bool |
|
39 | + */ |
|
40 | + protected function matches($other) |
|
41 | + { |
|
42 | + return \strpos($other, $this->prefix) === 0; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * Returns a string representation of the constraint. |
|
47 | - * |
|
48 | - * @return string |
|
49 | - */ |
|
50 | - public function toString() |
|
51 | - { |
|
52 | - return 'starts with "' . $this->prefix . '"'; |
|
53 | - } |
|
45 | + /** |
|
46 | + * Returns a string representation of the constraint. |
|
47 | + * |
|
48 | + * @return string |
|
49 | + */ |
|
50 | + public function toString() |
|
51 | + { |
|
52 | + return 'starts with "' . $this->prefix . '"'; |
|
53 | + } |
|
54 | 54 | } |
@@ -20,43 +20,43 @@ |
||
20 | 20 | */ |
21 | 21 | class RegularExpression extends Constraint |
22 | 22 | { |
23 | - /** |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - protected $pattern; |
|
23 | + /** |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + protected $pattern; |
|
27 | 27 | |
28 | - /** |
|
29 | - * @param string $pattern |
|
30 | - */ |
|
31 | - public function __construct($pattern) |
|
32 | - { |
|
33 | - parent::__construct(); |
|
34 | - $this->pattern = $pattern; |
|
35 | - } |
|
28 | + /** |
|
29 | + * @param string $pattern |
|
30 | + */ |
|
31 | + public function __construct($pattern) |
|
32 | + { |
|
33 | + parent::__construct(); |
|
34 | + $this->pattern = $pattern; |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * Evaluates the constraint for parameter $other. Returns true if the |
|
39 | - * constraint is met, false otherwise. |
|
40 | - * |
|
41 | - * @param mixed $other Value or object to evaluate. |
|
42 | - * |
|
43 | - * @return bool |
|
44 | - */ |
|
45 | - protected function matches($other) |
|
46 | - { |
|
47 | - return \preg_match($this->pattern, $other) > 0; |
|
48 | - } |
|
37 | + /** |
|
38 | + * Evaluates the constraint for parameter $other. Returns true if the |
|
39 | + * constraint is met, false otherwise. |
|
40 | + * |
|
41 | + * @param mixed $other Value or object to evaluate. |
|
42 | + * |
|
43 | + * @return bool |
|
44 | + */ |
|
45 | + protected function matches($other) |
|
46 | + { |
|
47 | + return \preg_match($this->pattern, $other) > 0; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Returns a string representation of the constraint. |
|
52 | - * |
|
53 | - * @return string |
|
54 | - */ |
|
55 | - public function toString() |
|
56 | - { |
|
57 | - return \sprintf( |
|
58 | - 'matches PCRE pattern "%s"', |
|
59 | - $this->pattern |
|
60 | - ); |
|
61 | - } |
|
50 | + /** |
|
51 | + * Returns a string representation of the constraint. |
|
52 | + * |
|
53 | + * @return string |
|
54 | + */ |
|
55 | + public function toString() |
|
56 | + { |
|
57 | + return \sprintf( |
|
58 | + 'matches PCRE pattern "%s"', |
|
59 | + $this->pattern |
|
60 | + ); |
|
61 | + } |
|
62 | 62 | } |
@@ -15,58 +15,58 @@ |
||
15 | 15 | */ |
16 | 16 | class JsonMatchesErrorMessageProvider |
17 | 17 | { |
18 | - /** |
|
19 | - * Translates JSON error to a human readable string. |
|
20 | - * |
|
21 | - * @param string $error |
|
22 | - * @param string $prefix |
|
23 | - * |
|
24 | - * @return string |
|
25 | - */ |
|
26 | - public static function determineJsonError($error, $prefix = '') |
|
27 | - { |
|
28 | - switch ($error) { |
|
29 | - case JSON_ERROR_NONE: |
|
30 | - return; |
|
31 | - case JSON_ERROR_DEPTH: |
|
32 | - return $prefix . 'Maximum stack depth exceeded'; |
|
33 | - case JSON_ERROR_STATE_MISMATCH: |
|
34 | - return $prefix . 'Underflow or the modes mismatch'; |
|
35 | - case JSON_ERROR_CTRL_CHAR: |
|
36 | - return $prefix . 'Unexpected control character found'; |
|
37 | - case JSON_ERROR_SYNTAX: |
|
38 | - return $prefix . 'Syntax error, malformed JSON'; |
|
39 | - case JSON_ERROR_UTF8: |
|
40 | - return $prefix . 'Malformed UTF-8 characters, possibly incorrectly encoded'; |
|
41 | - default: |
|
42 | - return $prefix . 'Unknown error'; |
|
43 | - } |
|
44 | - } |
|
18 | + /** |
|
19 | + * Translates JSON error to a human readable string. |
|
20 | + * |
|
21 | + * @param string $error |
|
22 | + * @param string $prefix |
|
23 | + * |
|
24 | + * @return string |
|
25 | + */ |
|
26 | + public static function determineJsonError($error, $prefix = '') |
|
27 | + { |
|
28 | + switch ($error) { |
|
29 | + case JSON_ERROR_NONE: |
|
30 | + return; |
|
31 | + case JSON_ERROR_DEPTH: |
|
32 | + return $prefix . 'Maximum stack depth exceeded'; |
|
33 | + case JSON_ERROR_STATE_MISMATCH: |
|
34 | + return $prefix . 'Underflow or the modes mismatch'; |
|
35 | + case JSON_ERROR_CTRL_CHAR: |
|
36 | + return $prefix . 'Unexpected control character found'; |
|
37 | + case JSON_ERROR_SYNTAX: |
|
38 | + return $prefix . 'Syntax error, malformed JSON'; |
|
39 | + case JSON_ERROR_UTF8: |
|
40 | + return $prefix . 'Malformed UTF-8 characters, possibly incorrectly encoded'; |
|
41 | + default: |
|
42 | + return $prefix . 'Unknown error'; |
|
43 | + } |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Translates a given type to a human readable message prefix. |
|
48 | - * |
|
49 | - * @param string $type |
|
50 | - * |
|
51 | - * @return string |
|
52 | - */ |
|
53 | - public static function translateTypeToPrefix($type) |
|
54 | - { |
|
55 | - switch (\strtolower($type)) { |
|
56 | - case 'expected': |
|
57 | - $prefix = 'Expected value JSON decode error - '; |
|
46 | + /** |
|
47 | + * Translates a given type to a human readable message prefix. |
|
48 | + * |
|
49 | + * @param string $type |
|
50 | + * |
|
51 | + * @return string |
|
52 | + */ |
|
53 | + public static function translateTypeToPrefix($type) |
|
54 | + { |
|
55 | + switch (\strtolower($type)) { |
|
56 | + case 'expected': |
|
57 | + $prefix = 'Expected value JSON decode error - '; |
|
58 | 58 | |
59 | - break; |
|
60 | - case 'actual': |
|
61 | - $prefix = 'Actual value JSON decode error - '; |
|
59 | + break; |
|
60 | + case 'actual': |
|
61 | + $prefix = 'Actual value JSON decode error - '; |
|
62 | 62 | |
63 | - break; |
|
64 | - default: |
|
65 | - $prefix = ''; |
|
63 | + break; |
|
64 | + default: |
|
65 | + $prefix = ''; |
|
66 | 66 | |
67 | - break; |
|
68 | - } |
|
67 | + break; |
|
68 | + } |
|
69 | 69 | |
70 | - return $prefix; |
|
71 | - } |
|
70 | + return $prefix; |
|
71 | + } |
|
72 | 72 | } |
@@ -14,26 +14,26 @@ |
||
14 | 14 | */ |
15 | 15 | class IsFalse extends Constraint |
16 | 16 | { |
17 | - /** |
|
18 | - * Evaluates the constraint for parameter $other. Returns true if the |
|
19 | - * constraint is met, false otherwise. |
|
20 | - * |
|
21 | - * @param mixed $other Value or object to evaluate. |
|
22 | - * |
|
23 | - * @return bool |
|
24 | - */ |
|
25 | - protected function matches($other) |
|
26 | - { |
|
27 | - return $other === false; |
|
28 | - } |
|
17 | + /** |
|
18 | + * Evaluates the constraint for parameter $other. Returns true if the |
|
19 | + * constraint is met, false otherwise. |
|
20 | + * |
|
21 | + * @param mixed $other Value or object to evaluate. |
|
22 | + * |
|
23 | + * @return bool |
|
24 | + */ |
|
25 | + protected function matches($other) |
|
26 | + { |
|
27 | + return $other === false; |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * Returns a string representation of the constraint. |
|
32 | - * |
|
33 | - * @return string |
|
34 | - */ |
|
35 | - public function toString() |
|
36 | - { |
|
37 | - return 'is false'; |
|
38 | - } |
|
30 | + /** |
|
31 | + * Returns a string representation of the constraint. |
|
32 | + * |
|
33 | + * @return string |
|
34 | + */ |
|
35 | + public function toString() |
|
36 | + { |
|
37 | + return 'is false'; |
|
38 | + } |
|
39 | 39 | } |
@@ -16,166 +16,166 @@ |
||
16 | 16 | */ |
17 | 17 | class LogicalNot extends Constraint |
18 | 18 | { |
19 | - /** |
|
20 | - * @var Constraint |
|
21 | - */ |
|
22 | - protected $constraint; |
|
23 | - |
|
24 | - /** |
|
25 | - * @param Constraint $constraint |
|
26 | - */ |
|
27 | - public function __construct($constraint) |
|
28 | - { |
|
29 | - parent::__construct(); |
|
30 | - |
|
31 | - if (!($constraint instanceof Constraint)) { |
|
32 | - $constraint = new IsEqual($constraint); |
|
33 | - } |
|
34 | - |
|
35 | - $this->constraint = $constraint; |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * @param string $string |
|
40 | - * |
|
41 | - * @return string |
|
42 | - */ |
|
43 | - public static function negate($string) |
|
44 | - { |
|
45 | - $positives = [ |
|
46 | - 'contains ', |
|
47 | - 'exists', |
|
48 | - 'has ', |
|
49 | - 'is ', |
|
50 | - 'are ', |
|
51 | - 'matches ', |
|
52 | - 'starts with ', |
|
53 | - 'ends with ', |
|
54 | - 'reference ', |
|
55 | - 'not not ' |
|
56 | - ]; |
|
57 | - |
|
58 | - $negatives = [ |
|
59 | - 'does not contain ', |
|
60 | - 'does not exist', |
|
61 | - 'does not have ', |
|
62 | - 'is not ', |
|
63 | - 'are not ', |
|
64 | - 'does not match ', |
|
65 | - 'starts not with ', |
|
66 | - 'ends not with ', |
|
67 | - 'don\'t reference ', |
|
68 | - 'not ' |
|
69 | - ]; |
|
70 | - |
|
71 | - \preg_match('/(\'[\w\W]*\')([\w\W]*)("[\w\W]*")/i', $string, $matches); |
|
72 | - |
|
73 | - if (\count($matches) > 0) { |
|
74 | - $nonInput = $matches[2]; |
|
75 | - |
|
76 | - $negatedString = \str_replace( |
|
77 | - $nonInput, |
|
78 | - \str_replace( |
|
79 | - $positives, |
|
80 | - $negatives, |
|
81 | - $nonInput |
|
82 | - ), |
|
83 | - $string |
|
84 | - ); |
|
85 | - } else { |
|
86 | - $negatedString = \str_replace( |
|
87 | - $positives, |
|
88 | - $negatives, |
|
89 | - $string |
|
90 | - ); |
|
91 | - } |
|
92 | - |
|
93 | - return $negatedString; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Evaluates the constraint for parameter $other |
|
98 | - * |
|
99 | - * If $returnResult is set to false (the default), an exception is thrown |
|
100 | - * in case of a failure. null is returned otherwise. |
|
101 | - * |
|
102 | - * If $returnResult is true, the result of the evaluation is returned as |
|
103 | - * a boolean value instead: true in case of success, false in case of a |
|
104 | - * failure. |
|
105 | - * |
|
106 | - * @param mixed $other Value or object to evaluate. |
|
107 | - * @param string $description Additional information about the test |
|
108 | - * @param bool $returnResult Whether to return a result or throw an exception |
|
109 | - * |
|
110 | - * @return mixed |
|
111 | - * |
|
112 | - * @throws ExpectationFailedException |
|
113 | - */ |
|
114 | - public function evaluate($other, $description = '', $returnResult = false) |
|
115 | - { |
|
116 | - $success = !$this->constraint->evaluate($other, $description, true); |
|
117 | - |
|
118 | - if ($returnResult) { |
|
119 | - return $success; |
|
120 | - } |
|
121 | - |
|
122 | - if (!$success) { |
|
123 | - $this->fail($other, $description); |
|
124 | - } |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Returns the description of the failure |
|
129 | - * |
|
130 | - * The beginning of failure messages is "Failed asserting that" in most |
|
131 | - * cases. This method should return the second part of that sentence. |
|
132 | - * |
|
133 | - * @param mixed $other Evaluated value or object. |
|
134 | - * |
|
135 | - * @return string |
|
136 | - */ |
|
137 | - protected function failureDescription($other) |
|
138 | - { |
|
139 | - switch (\get_class($this->constraint)) { |
|
140 | - case LogicalAnd::class: |
|
141 | - case self::class: |
|
142 | - case LogicalOr::class: |
|
143 | - return 'not( ' . $this->constraint->failureDescription($other) . ' )'; |
|
144 | - |
|
145 | - default: |
|
146 | - return self::negate( |
|
147 | - $this->constraint->failureDescription($other) |
|
148 | - ); |
|
149 | - } |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Returns a string representation of the constraint. |
|
154 | - * |
|
155 | - * @return string |
|
156 | - */ |
|
157 | - public function toString() |
|
158 | - { |
|
159 | - switch (\get_class($this->constraint)) { |
|
160 | - case LogicalAnd::class: |
|
161 | - case self::class: |
|
162 | - case LogicalOr::class: |
|
163 | - return 'not( ' . $this->constraint->toString() . ' )'; |
|
164 | - |
|
165 | - default: |
|
166 | - return self::negate( |
|
167 | - $this->constraint->toString() |
|
168 | - ); |
|
169 | - } |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Counts the number of constraint elements. |
|
174 | - * |
|
175 | - * @return int |
|
176 | - */ |
|
177 | - public function count() |
|
178 | - { |
|
179 | - return \count($this->constraint); |
|
180 | - } |
|
19 | + /** |
|
20 | + * @var Constraint |
|
21 | + */ |
|
22 | + protected $constraint; |
|
23 | + |
|
24 | + /** |
|
25 | + * @param Constraint $constraint |
|
26 | + */ |
|
27 | + public function __construct($constraint) |
|
28 | + { |
|
29 | + parent::__construct(); |
|
30 | + |
|
31 | + if (!($constraint instanceof Constraint)) { |
|
32 | + $constraint = new IsEqual($constraint); |
|
33 | + } |
|
34 | + |
|
35 | + $this->constraint = $constraint; |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * @param string $string |
|
40 | + * |
|
41 | + * @return string |
|
42 | + */ |
|
43 | + public static function negate($string) |
|
44 | + { |
|
45 | + $positives = [ |
|
46 | + 'contains ', |
|
47 | + 'exists', |
|
48 | + 'has ', |
|
49 | + 'is ', |
|
50 | + 'are ', |
|
51 | + 'matches ', |
|
52 | + 'starts with ', |
|
53 | + 'ends with ', |
|
54 | + 'reference ', |
|
55 | + 'not not ' |
|
56 | + ]; |
|
57 | + |
|
58 | + $negatives = [ |
|
59 | + 'does not contain ', |
|
60 | + 'does not exist', |
|
61 | + 'does not have ', |
|
62 | + 'is not ', |
|
63 | + 'are not ', |
|
64 | + 'does not match ', |
|
65 | + 'starts not with ', |
|
66 | + 'ends not with ', |
|
67 | + 'don\'t reference ', |
|
68 | + 'not ' |
|
69 | + ]; |
|
70 | + |
|
71 | + \preg_match('/(\'[\w\W]*\')([\w\W]*)("[\w\W]*")/i', $string, $matches); |
|
72 | + |
|
73 | + if (\count($matches) > 0) { |
|
74 | + $nonInput = $matches[2]; |
|
75 | + |
|
76 | + $negatedString = \str_replace( |
|
77 | + $nonInput, |
|
78 | + \str_replace( |
|
79 | + $positives, |
|
80 | + $negatives, |
|
81 | + $nonInput |
|
82 | + ), |
|
83 | + $string |
|
84 | + ); |
|
85 | + } else { |
|
86 | + $negatedString = \str_replace( |
|
87 | + $positives, |
|
88 | + $negatives, |
|
89 | + $string |
|
90 | + ); |
|
91 | + } |
|
92 | + |
|
93 | + return $negatedString; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Evaluates the constraint for parameter $other |
|
98 | + * |
|
99 | + * If $returnResult is set to false (the default), an exception is thrown |
|
100 | + * in case of a failure. null is returned otherwise. |
|
101 | + * |
|
102 | + * If $returnResult is true, the result of the evaluation is returned as |
|
103 | + * a boolean value instead: true in case of success, false in case of a |
|
104 | + * failure. |
|
105 | + * |
|
106 | + * @param mixed $other Value or object to evaluate. |
|
107 | + * @param string $description Additional information about the test |
|
108 | + * @param bool $returnResult Whether to return a result or throw an exception |
|
109 | + * |
|
110 | + * @return mixed |
|
111 | + * |
|
112 | + * @throws ExpectationFailedException |
|
113 | + */ |
|
114 | + public function evaluate($other, $description = '', $returnResult = false) |
|
115 | + { |
|
116 | + $success = !$this->constraint->evaluate($other, $description, true); |
|
117 | + |
|
118 | + if ($returnResult) { |
|
119 | + return $success; |
|
120 | + } |
|
121 | + |
|
122 | + if (!$success) { |
|
123 | + $this->fail($other, $description); |
|
124 | + } |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Returns the description of the failure |
|
129 | + * |
|
130 | + * The beginning of failure messages is "Failed asserting that" in most |
|
131 | + * cases. This method should return the second part of that sentence. |
|
132 | + * |
|
133 | + * @param mixed $other Evaluated value or object. |
|
134 | + * |
|
135 | + * @return string |
|
136 | + */ |
|
137 | + protected function failureDescription($other) |
|
138 | + { |
|
139 | + switch (\get_class($this->constraint)) { |
|
140 | + case LogicalAnd::class: |
|
141 | + case self::class: |
|
142 | + case LogicalOr::class: |
|
143 | + return 'not( ' . $this->constraint->failureDescription($other) . ' )'; |
|
144 | + |
|
145 | + default: |
|
146 | + return self::negate( |
|
147 | + $this->constraint->failureDescription($other) |
|
148 | + ); |
|
149 | + } |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Returns a string representation of the constraint. |
|
154 | + * |
|
155 | + * @return string |
|
156 | + */ |
|
157 | + public function toString() |
|
158 | + { |
|
159 | + switch (\get_class($this->constraint)) { |
|
160 | + case LogicalAnd::class: |
|
161 | + case self::class: |
|
162 | + case LogicalOr::class: |
|
163 | + return 'not( ' . $this->constraint->toString() . ' )'; |
|
164 | + |
|
165 | + default: |
|
166 | + return self::negate( |
|
167 | + $this->constraint->toString() |
|
168 | + ); |
|
169 | + } |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Counts the number of constraint elements. |
|
174 | + * |
|
175 | + * @return int |
|
176 | + */ |
|
177 | + public function count() |
|
178 | + { |
|
179 | + return \count($this->constraint); |
|
180 | + } |
|
181 | 181 | } |
@@ -16,110 +16,110 @@ |
||
16 | 16 | */ |
17 | 17 | class LogicalAnd extends Constraint |
18 | 18 | { |
19 | - /** |
|
20 | - * @var Constraint[] |
|
21 | - */ |
|
22 | - protected $constraints = []; |
|
23 | - |
|
24 | - /** |
|
25 | - * @var Constraint |
|
26 | - */ |
|
27 | - protected $lastConstraint; |
|
28 | - |
|
29 | - /** |
|
30 | - * @param Constraint[] $constraints |
|
31 | - * |
|
32 | - * @throws \PHPUnit\Framework\Exception |
|
33 | - */ |
|
34 | - public function setConstraints(array $constraints) |
|
35 | - { |
|
36 | - $this->constraints = []; |
|
37 | - |
|
38 | - foreach ($constraints as $constraint) { |
|
39 | - if (!($constraint instanceof Constraint)) { |
|
40 | - throw new \PHPUnit\Framework\Exception( |
|
41 | - 'All parameters to ' . __CLASS__ . |
|
42 | - ' must be a constraint object.' |
|
43 | - ); |
|
44 | - } |
|
45 | - |
|
46 | - $this->constraints[] = $constraint; |
|
47 | - } |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * Evaluates the constraint for parameter $other |
|
52 | - * |
|
53 | - * If $returnResult is set to false (the default), an exception is thrown |
|
54 | - * in case of a failure. null is returned otherwise. |
|
55 | - * |
|
56 | - * If $returnResult is true, the result of the evaluation is returned as |
|
57 | - * a boolean value instead: true in case of success, false in case of a |
|
58 | - * failure. |
|
59 | - * |
|
60 | - * @param mixed $other Value or object to evaluate. |
|
61 | - * @param string $description Additional information about the test |
|
62 | - * @param bool $returnResult Whether to return a result or throw an exception |
|
63 | - * |
|
64 | - * @return mixed |
|
65 | - * |
|
66 | - * @throws ExpectationFailedException |
|
67 | - */ |
|
68 | - public function evaluate($other, $description = '', $returnResult = false) |
|
69 | - { |
|
70 | - $success = true; |
|
71 | - $constraint = null; |
|
72 | - |
|
73 | - foreach ($this->constraints as $constraint) { |
|
74 | - if (!$constraint->evaluate($other, $description, true)) { |
|
75 | - $success = false; |
|
76 | - |
|
77 | - break; |
|
78 | - } |
|
79 | - } |
|
80 | - |
|
81 | - if ($returnResult) { |
|
82 | - return $success; |
|
83 | - } |
|
84 | - |
|
85 | - if (!$success) { |
|
86 | - $this->fail($other, $description); |
|
87 | - } |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Returns a string representation of the constraint. |
|
92 | - * |
|
93 | - * @return string |
|
94 | - */ |
|
95 | - public function toString() |
|
96 | - { |
|
97 | - $text = ''; |
|
98 | - |
|
99 | - foreach ($this->constraints as $key => $constraint) { |
|
100 | - if ($key > 0) { |
|
101 | - $text .= ' and '; |
|
102 | - } |
|
103 | - |
|
104 | - $text .= $constraint->toString(); |
|
105 | - } |
|
106 | - |
|
107 | - return $text; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Counts the number of constraint elements. |
|
112 | - * |
|
113 | - * @return int |
|
114 | - */ |
|
115 | - public function count() |
|
116 | - { |
|
117 | - $count = 0; |
|
118 | - |
|
119 | - foreach ($this->constraints as $constraint) { |
|
120 | - $count += \count($constraint); |
|
121 | - } |
|
122 | - |
|
123 | - return $count; |
|
124 | - } |
|
19 | + /** |
|
20 | + * @var Constraint[] |
|
21 | + */ |
|
22 | + protected $constraints = []; |
|
23 | + |
|
24 | + /** |
|
25 | + * @var Constraint |
|
26 | + */ |
|
27 | + protected $lastConstraint; |
|
28 | + |
|
29 | + /** |
|
30 | + * @param Constraint[] $constraints |
|
31 | + * |
|
32 | + * @throws \PHPUnit\Framework\Exception |
|
33 | + */ |
|
34 | + public function setConstraints(array $constraints) |
|
35 | + { |
|
36 | + $this->constraints = []; |
|
37 | + |
|
38 | + foreach ($constraints as $constraint) { |
|
39 | + if (!($constraint instanceof Constraint)) { |
|
40 | + throw new \PHPUnit\Framework\Exception( |
|
41 | + 'All parameters to ' . __CLASS__ . |
|
42 | + ' must be a constraint object.' |
|
43 | + ); |
|
44 | + } |
|
45 | + |
|
46 | + $this->constraints[] = $constraint; |
|
47 | + } |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * Evaluates the constraint for parameter $other |
|
52 | + * |
|
53 | + * If $returnResult is set to false (the default), an exception is thrown |
|
54 | + * in case of a failure. null is returned otherwise. |
|
55 | + * |
|
56 | + * If $returnResult is true, the result of the evaluation is returned as |
|
57 | + * a boolean value instead: true in case of success, false in case of a |
|
58 | + * failure. |
|
59 | + * |
|
60 | + * @param mixed $other Value or object to evaluate. |
|
61 | + * @param string $description Additional information about the test |
|
62 | + * @param bool $returnResult Whether to return a result or throw an exception |
|
63 | + * |
|
64 | + * @return mixed |
|
65 | + * |
|
66 | + * @throws ExpectationFailedException |
|
67 | + */ |
|
68 | + public function evaluate($other, $description = '', $returnResult = false) |
|
69 | + { |
|
70 | + $success = true; |
|
71 | + $constraint = null; |
|
72 | + |
|
73 | + foreach ($this->constraints as $constraint) { |
|
74 | + if (!$constraint->evaluate($other, $description, true)) { |
|
75 | + $success = false; |
|
76 | + |
|
77 | + break; |
|
78 | + } |
|
79 | + } |
|
80 | + |
|
81 | + if ($returnResult) { |
|
82 | + return $success; |
|
83 | + } |
|
84 | + |
|
85 | + if (!$success) { |
|
86 | + $this->fail($other, $description); |
|
87 | + } |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Returns a string representation of the constraint. |
|
92 | + * |
|
93 | + * @return string |
|
94 | + */ |
|
95 | + public function toString() |
|
96 | + { |
|
97 | + $text = ''; |
|
98 | + |
|
99 | + foreach ($this->constraints as $key => $constraint) { |
|
100 | + if ($key > 0) { |
|
101 | + $text .= ' and '; |
|
102 | + } |
|
103 | + |
|
104 | + $text .= $constraint->toString(); |
|
105 | + } |
|
106 | + |
|
107 | + return $text; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Counts the number of constraint elements. |
|
112 | + * |
|
113 | + * @return int |
|
114 | + */ |
|
115 | + public function count() |
|
116 | + { |
|
117 | + $count = 0; |
|
118 | + |
|
119 | + foreach ($this->constraints as $constraint) { |
|
120 | + $count += \count($constraint); |
|
121 | + } |
|
122 | + |
|
123 | + return $count; |
|
124 | + } |
|
125 | 125 | } |
@@ -24,159 +24,159 @@ |
||
24 | 24 | */ |
25 | 25 | class IsEqual extends Constraint |
26 | 26 | { |
27 | - /** |
|
28 | - * @var mixed |
|
29 | - */ |
|
30 | - protected $value; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var float |
|
34 | - */ |
|
35 | - protected $delta = 0.0; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var int |
|
39 | - */ |
|
40 | - protected $maxDepth = 10; |
|
41 | - |
|
42 | - /** |
|
43 | - * @var bool |
|
44 | - */ |
|
45 | - protected $canonicalize = false; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var bool |
|
49 | - */ |
|
50 | - protected $ignoreCase = false; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var SebastianBergmann\Comparator\ComparisonFailure |
|
54 | - */ |
|
55 | - protected $lastFailure; |
|
56 | - |
|
57 | - /** |
|
58 | - * @param mixed $value |
|
59 | - * @param float $delta |
|
60 | - * @param int $maxDepth |
|
61 | - * @param bool $canonicalize |
|
62 | - * @param bool $ignoreCase |
|
63 | - * |
|
64 | - * @throws \PHPUnit\Framework\Exception |
|
65 | - */ |
|
66 | - public function __construct($value, $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false) |
|
67 | - { |
|
68 | - parent::__construct(); |
|
69 | - |
|
70 | - if (!\is_numeric($delta)) { |
|
71 | - throw InvalidArgumentHelper::factory(2, 'numeric'); |
|
72 | - } |
|
73 | - |
|
74 | - if (!\is_int($maxDepth)) { |
|
75 | - throw InvalidArgumentHelper::factory(3, 'integer'); |
|
76 | - } |
|
77 | - |
|
78 | - if (!\is_bool($canonicalize)) { |
|
79 | - throw InvalidArgumentHelper::factory(4, 'boolean'); |
|
80 | - } |
|
81 | - |
|
82 | - if (!\is_bool($ignoreCase)) { |
|
83 | - throw InvalidArgumentHelper::factory(5, 'boolean'); |
|
84 | - } |
|
85 | - |
|
86 | - $this->value = $value; |
|
87 | - $this->delta = $delta; |
|
88 | - $this->maxDepth = $maxDepth; |
|
89 | - $this->canonicalize = $canonicalize; |
|
90 | - $this->ignoreCase = $ignoreCase; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Evaluates the constraint for parameter $other |
|
95 | - * |
|
96 | - * If $returnResult is set to false (the default), an exception is thrown |
|
97 | - * in case of a failure. null is returned otherwise. |
|
98 | - * |
|
99 | - * If $returnResult is true, the result of the evaluation is returned as |
|
100 | - * a boolean value instead: true in case of success, false in case of a |
|
101 | - * failure. |
|
102 | - * |
|
103 | - * @param mixed $other Value or object to evaluate. |
|
104 | - * @param string $description Additional information about the test |
|
105 | - * @param bool $returnResult Whether to return a result or throw an exception |
|
106 | - * |
|
107 | - * @return mixed |
|
108 | - * |
|
109 | - * @throws ExpectationFailedException |
|
110 | - */ |
|
111 | - public function evaluate($other, $description = '', $returnResult = false) |
|
112 | - { |
|
113 | - // If $this->value and $other are identical, they are also equal. |
|
114 | - // This is the most common path and will allow us to skip |
|
115 | - // initialization of all the comparators. |
|
116 | - if ($this->value === $other) { |
|
117 | - return true; |
|
118 | - } |
|
119 | - |
|
120 | - $comparatorFactory = SebastianBergmann\Comparator\Factory::getInstance(); |
|
121 | - |
|
122 | - try { |
|
123 | - $comparator = $comparatorFactory->getComparatorFor( |
|
124 | - $this->value, |
|
125 | - $other |
|
126 | - ); |
|
127 | - |
|
128 | - $comparator->assertEquals( |
|
129 | - $this->value, |
|
130 | - $other, |
|
131 | - $this->delta, |
|
132 | - $this->canonicalize, |
|
133 | - $this->ignoreCase |
|
134 | - ); |
|
135 | - } catch (SebastianBergmann\Comparator\ComparisonFailure $f) { |
|
136 | - if ($returnResult) { |
|
137 | - return false; |
|
138 | - } |
|
139 | - |
|
140 | - throw new ExpectationFailedException( |
|
141 | - \trim($description . "\n" . $f->getMessage()), |
|
142 | - $f |
|
143 | - ); |
|
144 | - } |
|
145 | - |
|
146 | - return true; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Returns a string representation of the constraint. |
|
151 | - * |
|
152 | - * @return string |
|
153 | - */ |
|
154 | - public function toString() |
|
155 | - { |
|
156 | - $delta = ''; |
|
157 | - |
|
158 | - if (\is_string($this->value)) { |
|
159 | - if (\strpos($this->value, "\n") !== false) { |
|
160 | - return 'is equal to <text>'; |
|
161 | - } |
|
162 | - |
|
163 | - return \sprintf( |
|
164 | - 'is equal to "%s"', |
|
165 | - $this->value |
|
166 | - ); |
|
167 | - } |
|
168 | - |
|
169 | - if ($this->delta != 0) { |
|
170 | - $delta = \sprintf( |
|
171 | - ' with delta <%F>', |
|
172 | - $this->delta |
|
173 | - ); |
|
174 | - } |
|
175 | - |
|
176 | - return \sprintf( |
|
177 | - 'is equal to %s%s', |
|
178 | - $this->exporter->export($this->value), |
|
179 | - $delta |
|
180 | - ); |
|
181 | - } |
|
27 | + /** |
|
28 | + * @var mixed |
|
29 | + */ |
|
30 | + protected $value; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var float |
|
34 | + */ |
|
35 | + protected $delta = 0.0; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var int |
|
39 | + */ |
|
40 | + protected $maxDepth = 10; |
|
41 | + |
|
42 | + /** |
|
43 | + * @var bool |
|
44 | + */ |
|
45 | + protected $canonicalize = false; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var bool |
|
49 | + */ |
|
50 | + protected $ignoreCase = false; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var SebastianBergmann\Comparator\ComparisonFailure |
|
54 | + */ |
|
55 | + protected $lastFailure; |
|
56 | + |
|
57 | + /** |
|
58 | + * @param mixed $value |
|
59 | + * @param float $delta |
|
60 | + * @param int $maxDepth |
|
61 | + * @param bool $canonicalize |
|
62 | + * @param bool $ignoreCase |
|
63 | + * |
|
64 | + * @throws \PHPUnit\Framework\Exception |
|
65 | + */ |
|
66 | + public function __construct($value, $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false) |
|
67 | + { |
|
68 | + parent::__construct(); |
|
69 | + |
|
70 | + if (!\is_numeric($delta)) { |
|
71 | + throw InvalidArgumentHelper::factory(2, 'numeric'); |
|
72 | + } |
|
73 | + |
|
74 | + if (!\is_int($maxDepth)) { |
|
75 | + throw InvalidArgumentHelper::factory(3, 'integer'); |
|
76 | + } |
|
77 | + |
|
78 | + if (!\is_bool($canonicalize)) { |
|
79 | + throw InvalidArgumentHelper::factory(4, 'boolean'); |
|
80 | + } |
|
81 | + |
|
82 | + if (!\is_bool($ignoreCase)) { |
|
83 | + throw InvalidArgumentHelper::factory(5, 'boolean'); |
|
84 | + } |
|
85 | + |
|
86 | + $this->value = $value; |
|
87 | + $this->delta = $delta; |
|
88 | + $this->maxDepth = $maxDepth; |
|
89 | + $this->canonicalize = $canonicalize; |
|
90 | + $this->ignoreCase = $ignoreCase; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Evaluates the constraint for parameter $other |
|
95 | + * |
|
96 | + * If $returnResult is set to false (the default), an exception is thrown |
|
97 | + * in case of a failure. null is returned otherwise. |
|
98 | + * |
|
99 | + * If $returnResult is true, the result of the evaluation is returned as |
|
100 | + * a boolean value instead: true in case of success, false in case of a |
|
101 | + * failure. |
|
102 | + * |
|
103 | + * @param mixed $other Value or object to evaluate. |
|
104 | + * @param string $description Additional information about the test |
|
105 | + * @param bool $returnResult Whether to return a result or throw an exception |
|
106 | + * |
|
107 | + * @return mixed |
|
108 | + * |
|
109 | + * @throws ExpectationFailedException |
|
110 | + */ |
|
111 | + public function evaluate($other, $description = '', $returnResult = false) |
|
112 | + { |
|
113 | + // If $this->value and $other are identical, they are also equal. |
|
114 | + // This is the most common path and will allow us to skip |
|
115 | + // initialization of all the comparators. |
|
116 | + if ($this->value === $other) { |
|
117 | + return true; |
|
118 | + } |
|
119 | + |
|
120 | + $comparatorFactory = SebastianBergmann\Comparator\Factory::getInstance(); |
|
121 | + |
|
122 | + try { |
|
123 | + $comparator = $comparatorFactory->getComparatorFor( |
|
124 | + $this->value, |
|
125 | + $other |
|
126 | + ); |
|
127 | + |
|
128 | + $comparator->assertEquals( |
|
129 | + $this->value, |
|
130 | + $other, |
|
131 | + $this->delta, |
|
132 | + $this->canonicalize, |
|
133 | + $this->ignoreCase |
|
134 | + ); |
|
135 | + } catch (SebastianBergmann\Comparator\ComparisonFailure $f) { |
|
136 | + if ($returnResult) { |
|
137 | + return false; |
|
138 | + } |
|
139 | + |
|
140 | + throw new ExpectationFailedException( |
|
141 | + \trim($description . "\n" . $f->getMessage()), |
|
142 | + $f |
|
143 | + ); |
|
144 | + } |
|
145 | + |
|
146 | + return true; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Returns a string representation of the constraint. |
|
151 | + * |
|
152 | + * @return string |
|
153 | + */ |
|
154 | + public function toString() |
|
155 | + { |
|
156 | + $delta = ''; |
|
157 | + |
|
158 | + if (\is_string($this->value)) { |
|
159 | + if (\strpos($this->value, "\n") !== false) { |
|
160 | + return 'is equal to <text>'; |
|
161 | + } |
|
162 | + |
|
163 | + return \sprintf( |
|
164 | + 'is equal to "%s"', |
|
165 | + $this->value |
|
166 | + ); |
|
167 | + } |
|
168 | + |
|
169 | + if ($this->delta != 0) { |
|
170 | + $delta = \sprintf( |
|
171 | + ' with delta <%F>', |
|
172 | + $this->delta |
|
173 | + ); |
|
174 | + } |
|
175 | + |
|
176 | + return \sprintf( |
|
177 | + 'is equal to %s%s', |
|
178 | + $this->exporter->export($this->value), |
|
179 | + $delta |
|
180 | + ); |
|
181 | + } |
|
182 | 182 | } |