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

https://gitlab.com/Japang-Jawara/jawara-penilaian · PHP · 123 lines · 60 code · 12 blank · 51 comment · 16 complexity · 0c15d54237a653dc14251b01f57cdf44 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. * Constraint that asserts that the Traversable it is applied to contains
  12. * a given value.
  13. */
  14. class PHPUnit_Framework_Constraint_TraversableContains extends PHPUnit_Framework_Constraint
  15. {
  16. /**
  17. * @var bool
  18. */
  19. protected $checkForObjectIdentity;
  20. /**
  21. * @var bool
  22. */
  23. protected $checkForNonObjectIdentity;
  24. /**
  25. * @var mixed
  26. */
  27. protected $value;
  28. /**
  29. * @param mixed $value
  30. * @param bool $checkForObjectIdentity
  31. * @param bool $checkForNonObjectIdentity
  32. *
  33. * @throws PHPUnit_Framework_Exception
  34. */
  35. public function __construct($value, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false)
  36. {
  37. parent::__construct();
  38. if (!is_bool($checkForObjectIdentity)) {
  39. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'boolean');
  40. }
  41. if (!is_bool($checkForNonObjectIdentity)) {
  42. throw PHPUnit_Util_InvalidArgumentHelper::factory(3, 'boolean');
  43. }
  44. $this->checkForObjectIdentity = $checkForObjectIdentity;
  45. $this->checkForNonObjectIdentity = $checkForNonObjectIdentity;
  46. $this->value = $value;
  47. }
  48. /**
  49. * Evaluates the constraint for parameter $other. Returns true if the
  50. * constraint is met, false otherwise.
  51. *
  52. * @param mixed $other Value or object to evaluate.
  53. *
  54. * @return bool
  55. */
  56. protected function matches($other)
  57. {
  58. if ($other instanceof SplObjectStorage) {
  59. return $other->contains($this->value);
  60. }
  61. if (is_object($this->value)) {
  62. foreach ($other as $element) {
  63. if ($this->checkForObjectIdentity && $element === $this->value) {
  64. return true;
  65. } elseif (!$this->checkForObjectIdentity && $element == $this->value) {
  66. return true;
  67. }
  68. }
  69. } else {
  70. foreach ($other as $element) {
  71. if ($this->checkForNonObjectIdentity && $element === $this->value) {
  72. return true;
  73. } elseif (!$this->checkForNonObjectIdentity && $element == $this->value) {
  74. return true;
  75. }
  76. }
  77. }
  78. return false;
  79. }
  80. /**
  81. * Returns a string representation of the constraint.
  82. *
  83. * @return string
  84. */
  85. public function toString()
  86. {
  87. if (is_string($this->value) && strpos($this->value, "\n") !== false) {
  88. return 'contains "' . $this->value . '"';
  89. } else {
  90. return 'contains ' . $this->exporter->export($this->value);
  91. }
  92. }
  93. /**
  94. * Returns the description of the failure
  95. *
  96. * The beginning of failure messages is "Failed asserting that" in most
  97. * cases. This method should return the second part of that sentence.
  98. *
  99. * @param mixed $other Evaluated value or object.
  100. *
  101. * @return string
  102. */
  103. protected function failureDescription($other)
  104. {
  105. return sprintf(
  106. '%s %s',
  107. is_array($other) ? 'an array' : 'a traversable',
  108. $this->toString()
  109. );
  110. }
  111. }