PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpunit/phpunit/PHPUnit/Framework/Comparator/Scalar.php

https://bitbucket.org/sklyarov_ivan/trap
PHP | 136 lines | 48 code | 7 blank | 81 comment | 13 complexity | 39da9bc7e9bb89dc326572cf76ca4f41 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2001-2012, Sebastian Bergmann <sebastian@phpunit.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @package PHPUnit
  38. * @subpackage Framework
  39. * @author Bernhard Schussek <bschussek@2bepublished.at>
  40. * @copyright 2001-2012 Sebastian Bergmann <sebastian@phpunit.de>
  41. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  42. * @link http://www.phpunit.de/
  43. * @since File available since Release 3.6.0
  44. */
  45. /**
  46. * Compares scalar or NULL values for equality.
  47. *
  48. * @package PHPUnit
  49. * @subpackage Framework_Comparator
  50. * @author Bernhard Schussek <bschussek@2bepublished.at>
  51. * @copyright 2001-2012 Sebastian Bergmann <sebastian@phpunit.de>
  52. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  53. * @link http://www.phpunit.de/
  54. * @since Class available since Release 3.6.0
  55. */
  56. class PHPUnit_Framework_Comparator_Scalar extends PHPUnit_Framework_Comparator
  57. {
  58. /**
  59. * Returns whether the comparator can compare two values.
  60. *
  61. * @param mixed $expected The first value to compare
  62. * @param mixed $actual The second value to compare
  63. * @return boolean
  64. * @since Method available since Release 3.6.0
  65. */
  66. public function accepts($expected, $actual)
  67. {
  68. return ((is_scalar($expected) XOR NULL === $expected) &&
  69. (is_scalar($actual) XOR NULL === $actual))
  70. // allow comparison between strings and objects featuring __toString()
  71. || (is_string($expected) && is_object($actual) && method_exists($actual, '__toString'))
  72. || (is_object($expected) && method_exists($expected, '__toString') && is_string($actual));
  73. }
  74. /**
  75. * Asserts that two values are equal.
  76. *
  77. * @param mixed $expected The first value to compare
  78. * @param mixed $actual The second value to compare
  79. * @param float $delta The allowed numerical distance between two values to
  80. * consider them equal
  81. * @param bool $canonicalize If set to TRUE, arrays are sorted before
  82. * comparison
  83. * @param bool $ignoreCase If set to TRUE, upper- and lowercasing is
  84. * ignored when comparing string values
  85. * @throws PHPUnit_Framework_ComparisonFailure Thrown when the comparison
  86. * fails. Contains information about the
  87. * specific errors that lead to the failure.
  88. */
  89. public function assertEquals($expected, $actual, $delta = 0, $canonicalize = FALSE, $ignoreCase = FALSE)
  90. {
  91. $expectedToCompare = $expected;
  92. $actualToCompare = $actual;
  93. // always compare as strings to avoid strange behaviour
  94. // otherwise 0 == 'Foobar'
  95. if (is_string($expected) || is_string($actual)) {
  96. $expectedToCompare = (string)$expectedToCompare;
  97. $actualToCompare = (string)$actualToCompare;
  98. if ($ignoreCase) {
  99. $expectedToCompare = strtolower($expectedToCompare);
  100. $actualToCompare = strtolower($actualToCompare);
  101. }
  102. }
  103. if ($expectedToCompare != $actualToCompare) {
  104. if (is_string($expected) && is_string($actual)) {
  105. throw new PHPUnit_Framework_ComparisonFailure(
  106. $expected,
  107. $actual,
  108. PHPUnit_Util_Type::export($expected),
  109. PHPUnit_Util_Type::export($actual),
  110. FALSE,
  111. 'Failed asserting that two strings are equal.'
  112. );
  113. }
  114. throw new PHPUnit_Framework_ComparisonFailure(
  115. $expected,
  116. $actual,
  117. // no diff is required
  118. '',
  119. '',
  120. FALSE,
  121. sprintf(
  122. 'Failed asserting that %s matches expected %s.',
  123. PHPUnit_Util_Type::export($actual),
  124. PHPUnit_Util_Type::export($expected)
  125. )
  126. );
  127. }
  128. }
  129. }