ArrayHasSameValuesConstraint.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. /**
  11. * Constraint that accepts arrays with the same elements but different order.
  12. */
  13. class ArrayHasSameValuesConstraint extends PHPUnit_Framework_Constraint
  14. {
  15. protected $array;
  16. /**
  17. * @param array $array
  18. */
  19. public function __construct($array)
  20. {
  21. $this->array = $array;
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function evaluate($other, $description = '', $returnResult = FALSE)
  27. {
  28. $description = $description ?: 'Failed asserting that two arrays have the same elements.';
  29. if (count($this->array) !== count($other)) {
  30. throw new PHPUnit_Framework_ExpectationFailedException($description);
  31. }
  32. if (array_diff($this->array, $other)) {
  33. throw new PHPUnit_Framework_ExpectationFailedException($description);
  34. }
  35. return true;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function toString()
  41. {
  42. return 'two arrays have the same elements.';
  43. }
  44. }