alexislefebvre /
LiipFunctionalTestBundle
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the Liip/FunctionalTestBundle |
||
| 5 | * |
||
| 6 | * (c) Lukas Kahwe Smith <[email protected]> |
||
| 7 | * |
||
| 8 | * This source file is subject to the MIT license that is bundled |
||
| 9 | * with this source code in the file LICENSE. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace Liip\FunctionalTestBundle\Test; |
||
| 13 | |||
| 14 | // BC |
||
|
0 ignored issues
–
show
|
|||
| 15 | 1 | //class_alias('\PHPUnit_Framework_Constraint', '\PHPUnit\Framework\Constraint\Constraint'); |
|
| 16 | |||
| 17 | use PHPUnit\Framework\Constraint\Constraint; |
||
| 18 | use PHPUnit\Framework\ExpectationFailedException; |
||
| 19 | use Symfony\Component\Validator\ConstraintViolationList; |
||
| 20 | |||
| 21 | class ValidationErrorsConstraint extends Constraint |
||
| 22 | { |
||
| 23 | private $expect; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * ValidationErrorsConstraint constructor. |
||
| 27 | * |
||
| 28 | * @param $expect |
||
| 29 | 2 | */ |
|
| 30 | public function __construct(array $expect) |
||
| 31 | 2 | { |
|
| 32 | 2 | $this->expect = $expect; |
|
| 33 | 2 | sort($this->expect); |
|
| 34 | 2 | parent::__construct(); |
|
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param ConstraintViolationList $other |
||
| 39 | * @param string $description |
||
| 40 | * @param bool $returnResult |
||
| 41 | * |
||
| 42 | * @return mixed |
||
| 43 | 2 | */ |
|
| 44 | public function evaluate($other, $description = '', $returnResult = false) |
||
| 45 | 2 | { |
|
| 46 | $actual = array(); |
||
| 47 | 2 | ||
| 48 | 2 | foreach ($other as $error) { |
|
| 49 | 2 | $actual[$error->getPropertyPath()][] = $error->getMessage(); |
|
| 50 | } |
||
| 51 | 2 | ||
| 52 | ksort($actual); |
||
| 53 | 2 | ||
| 54 | 1 | if (array_keys($actual) == $this->expect) { |
|
| 55 | return true; |
||
| 56 | } |
||
| 57 | 1 | ||
| 58 | if ($returnResult) { |
||
| 59 | return false; |
||
| 60 | } |
||
| 61 | |||
| 62 | 1 | // Generate failure message |
|
| 63 | 1 | $mismatchedKeys = array_merge( |
|
| 64 | 1 | array_diff(array_keys($actual), $this->expect), |
|
| 65 | 1 | array_diff($this->expect, array_keys($actual)) |
|
| 66 | 1 | ); |
|
| 67 | sort($mismatchedKeys); |
||
| 68 | 1 | ||
| 69 | $lines = array(); |
||
| 70 | 1 | ||
| 71 | 1 | foreach ($mismatchedKeys as $key) { |
|
| 72 | 1 | if (isset($actual[$key])) { |
|
| 73 | 1 | foreach ($actual[$key] as $unexpectedErrorMessage) { |
|
| 74 | 1 | $lines[] = '+ '.$key.' ('.$unexpectedErrorMessage.')'; |
|
| 75 | 1 | } |
|
| 76 | 1 | } else { |
|
| 77 | $lines[] = '- '.$key; |
||
| 78 | 1 | } |
|
| 79 | } |
||
| 80 | 1 | ||
| 81 | 1 | throw new ExpectationFailedException( |
|
| 82 | 1 | $description."\n".implode("\n", $lines) |
|
| 83 | ); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Returns a string representation of the object. |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | public function toString() |
||
| 92 | { |
||
| 93 | return 'validation errors match'; |
||
| 94 | } |
||
| 95 | } |
||
| 96 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.