/vendor/phpunit/phpunit/src/Framework/Constraint/Xor.php

https://bitbucket.org/alan_cordova/api-sb-map · PHP · 118 lines · 56 code · 17 blank · 45 comment · 5 complexity · c38ae0be3e48b3b60ef97ac2fac91f2f MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of PHPUnit.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  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. * Logical XOR.
  12. */
  13. class PHPUnit_Framework_Constraint_Xor extends PHPUnit_Framework_Constraint
  14. {
  15. /**
  16. * @var PHPUnit_Framework_Constraint[]
  17. */
  18. protected $constraints = [];
  19. /**
  20. * @param PHPUnit_Framework_Constraint[] $constraints
  21. */
  22. public function setConstraints(array $constraints)
  23. {
  24. $this->constraints = [];
  25. foreach ($constraints as $constraint) {
  26. if (!($constraint instanceof PHPUnit_Framework_Constraint)) {
  27. $constraint = new PHPUnit_Framework_Constraint_IsEqual(
  28. $constraint
  29. );
  30. }
  31. $this->constraints[] = $constraint;
  32. }
  33. }
  34. /**
  35. * Evaluates the constraint for parameter $other
  36. *
  37. * If $returnResult is set to false (the default), an exception is thrown
  38. * in case of a failure. null is returned otherwise.
  39. *
  40. * If $returnResult is true, the result of the evaluation is returned as
  41. * a boolean value instead: true in case of success, false in case of a
  42. * failure.
  43. *
  44. * @param mixed $other Value or object to evaluate.
  45. * @param string $description Additional information about the test
  46. * @param bool $returnResult Whether to return a result or throw an exception
  47. *
  48. * @return mixed
  49. *
  50. * @throws PHPUnit_Framework_ExpectationFailedException
  51. */
  52. public function evaluate($other, $description = '', $returnResult = false)
  53. {
  54. $success = true;
  55. $lastResult = null;
  56. $constraint = null;
  57. foreach ($this->constraints as $constraint) {
  58. $result = $constraint->evaluate($other, $description, true);
  59. if ($result === $lastResult) {
  60. $success = false;
  61. break;
  62. }
  63. $lastResult = $result;
  64. }
  65. if ($returnResult) {
  66. return $success;
  67. }
  68. if (!$success) {
  69. $this->fail($other, $description);
  70. }
  71. }
  72. /**
  73. * Returns a string representation of the constraint.
  74. *
  75. * @return string
  76. */
  77. public function toString()
  78. {
  79. $text = '';
  80. foreach ($this->constraints as $key => $constraint) {
  81. if ($key > 0) {
  82. $text .= ' xor ';
  83. }
  84. $text .= $constraint->toString();
  85. }
  86. return $text;
  87. }
  88. /**
  89. * Counts the number of constraint elements.
  90. *
  91. * @return int
  92. */
  93. public function count()
  94. {
  95. $count = 0;
  96. foreach ($this->constraints as $constraint) {
  97. $count += count($constraint);
  98. }
  99. return $count;
  100. }
  101. }