ArrayHasSameValuesConstraint.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. use \PHPUnit_Framework_Constraint;
  11. use \PHPUnit_Framework_ExpectationFailedException;
  12. /**
  13. * Constraint that accepts arrays with the same elements but different order.
  14. */
  15. class ArrayHasSameValuesConstraint extends PHPUnit_Framework_Constraint
  16. {
  17. protected $array;
  18. /**
  19. * @param array $array
  20. */
  21. public function __construct($array)
  22. {
  23. $this->array = $array;
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function evaluate($other, $description = '', $returnResult = FALSE)
  29. {
  30. $description = $description ?: 'Failed asserting that two arrays have the same elements.';
  31. if (count($this->array) !== count($other)) {
  32. throw new PHPUnit_Framework_ExpectationFailedException($description);
  33. }
  34. if (array_diff($this->array, $other)) {
  35. throw new PHPUnit_Framework_ExpectationFailedException($description);
  36. }
  37. return true;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function toString()
  43. {
  44. return 'two arrays have the same elements.';
  45. }
  46. }