LimeTesterScalar.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /*
  3. * This file is part of the Lime framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. class LimeTesterScalar extends LimeTester
  12. {
  13. protected
  14. $type = 'scalar';
  15. public function __construct($value)
  16. {
  17. $this->type = gettype($value);
  18. parent::__construct($value);
  19. }
  20. public function __toString()
  21. {
  22. return var_export($this->value, true);
  23. }
  24. private function equals(LimeTesterInterface $other)
  25. {
  26. $exp1 = $this->value;
  27. $exp2 = $other->value;
  28. if (is_scalar($exp2) || is_null($exp2))
  29. {
  30. // always compare as strings to avoid strange behaviour
  31. // otherwise 0 == 'Foobar'
  32. if (is_string($exp1) || is_string($exp2))
  33. {
  34. $exp1 = (string)$exp1;
  35. $exp2 = (string)$exp2;
  36. }
  37. return $exp1 == $exp2;
  38. }
  39. else
  40. {
  41. return false;
  42. }
  43. }
  44. public function is(LimeTesterInterface $expected)
  45. {
  46. if (!$this->equals($expected))
  47. {
  48. throw new LimeAssertionFailedException($this, $expected);
  49. }
  50. }
  51. public function same(LimeTesterInterface $expected)
  52. {
  53. if ($this->value !== $expected->value)
  54. {
  55. throw new LimeAssertionFailedException($this, $expected);
  56. }
  57. }
  58. public function isnt(LimeTesterInterface $expected)
  59. {
  60. if ($this->equals($expected))
  61. {
  62. throw new LimeAssertionFailedException($this, $expected);
  63. }
  64. }
  65. public function isntSame(LimeTesterInterface $expected)
  66. {
  67. if ($this->value === $expected->value)
  68. {
  69. throw new LimeAssertionFailedException($this, $expected);
  70. }
  71. }
  72. public function greaterThan(LimeTesterInterface $expected)
  73. {
  74. if ($this->value <= $expected->value)
  75. {
  76. throw new LimeAssertionFailedException($this, $expected);
  77. }
  78. }
  79. public function greaterThanEqual(LimeTesterInterface $expected)
  80. {
  81. if ($this->value < $expected->value)
  82. {
  83. throw new LimeAssertionFailedException($this, $expected);
  84. }
  85. }
  86. public function lessThanEqual(LimeTesterInterface $expected)
  87. {
  88. if ($this->value > $expected->value)
  89. {
  90. throw new LimeAssertionFailedException($this, $expected);
  91. }
  92. }
  93. public function lessThan(LimeTesterInterface $expected)
  94. {
  95. if ($this->value >= $expected->value)
  96. {
  97. throw new LimeAssertionFailedException($this, $expected);
  98. }
  99. }
  100. }