These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
|
0 ignored issues
–
show
|
|||
| 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 |
||
| 15 | 1 | if (class_exists('\PHPUnit_Framework_Constraint')) { |
|
| 16 | 1 | class_alias('\PHPUnit_Framework_Constraint', '\PHPUnit\Framework\Constraint\Constraint'); |
|
| 17 | 1 | } |
|
| 18 | 1 | if (class_exists('\PHPUnit_Framework_ExpectationFailedException')) { |
|
| 19 | 1 | class_alias('\PHPUnit_Framework_ExpectationFailedException', '\PHPUnit\Framework\ExpectationFailedException'); |
|
| 20 | 1 | } |
|
| 21 | |||
| 22 | use PHPUnit\Framework\Constraint\Constraint; |
||
| 23 | use PHPUnit\Framework\ExpectationFailedException; |
||
| 24 | use Symfony\Component\Validator\ConstraintViolationList; |
||
| 25 | |||
| 26 | class ValidationErrorsConstraint extends Constraint |
||
| 27 | { |
||
| 28 | private $expect; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * ValidationErrorsConstraint constructor. |
||
| 32 | * |
||
| 33 | * @param $expect |
||
| 34 | */ |
||
| 35 | 2 | public function __construct(array $expect) |
|
| 36 | { |
||
| 37 | 2 | $this->expect = $expect; |
|
| 38 | 2 | sort($this->expect); |
|
| 39 | 2 | parent::__construct(); |
|
| 40 | 2 | } |
|
| 41 | |||
| 42 | /** |
||
| 43 | * @param ConstraintViolationList $other |
||
| 44 | * @param string $description |
||
| 45 | * @param bool $returnResult |
||
| 46 | * |
||
| 47 | * @return mixed |
||
| 48 | */ |
||
| 49 | 2 | public function evaluate($other, $description = '', $returnResult = false) |
|
| 50 | { |
||
| 51 | 2 | $actual = array(); |
|
| 52 | |||
| 53 | 2 | foreach ($other as $error) { |
|
| 54 | 2 | $actual[$error->getPropertyPath()][] = $error->getMessage(); |
|
| 55 | 2 | } |
|
| 56 | |||
| 57 | 2 | ksort($actual); |
|
| 58 | |||
| 59 | 2 | if (array_keys($actual) == $this->expect) { |
|
| 60 | 1 | return true; |
|
| 61 | } |
||
| 62 | |||
| 63 | 1 | if ($returnResult) { |
|
| 64 | return false; |
||
| 65 | } |
||
| 66 | |||
| 67 | // Generate failure message |
||
| 68 | 1 | $mismatchedKeys = array_merge( |
|
| 69 | 1 | array_diff(array_keys($actual), $this->expect), |
|
| 70 | 1 | array_diff($this->expect, array_keys($actual)) |
|
| 71 | 1 | ); |
|
| 72 | 1 | sort($mismatchedKeys); |
|
| 73 | |||
| 74 | 1 | $lines = array(); |
|
| 75 | |||
| 76 | 1 | foreach ($mismatchedKeys as $key) { |
|
| 77 | 1 | if (isset($actual[$key])) { |
|
| 78 | 1 | foreach ($actual[$key] as $unexpectedErrorMessage) { |
|
| 79 | 1 | $lines[] = '+ '.$key.' ('.$unexpectedErrorMessage.')'; |
|
| 80 | 1 | } |
|
| 81 | 1 | } else { |
|
| 82 | 1 | $lines[] = '- '.$key; |
|
| 83 | } |
||
| 84 | 1 | } |
|
| 85 | |||
| 86 | 1 | throw new ExpectationFailedException( |
|
| 87 | 1 | $description."\n".implode("\n", $lines) |
|
| 88 | 1 | ); |
|
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Returns a string representation of the object. |
||
| 93 | * |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public function toString() |
||
| 97 | { |
||
| 98 | return 'validation errors match'; |
||
| 99 | } |
||
| 100 | } |
||
| 101 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.