ExactValueTokenSpec.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace spec\Prophecy\Argument\Token;
  3. use PhpSpec\ObjectBehavior;
  4. class ExactValueTokenSpec extends ObjectBehavior
  5. {
  6. function let()
  7. {
  8. $this->beConstructedWith(42);
  9. }
  10. function it_implements_TokenInterface()
  11. {
  12. $this->shouldBeAnInstanceOf('Prophecy\Argument\Token\TokenInterface');
  13. }
  14. function it_is_not_last()
  15. {
  16. $this->shouldNotBeLast();
  17. }
  18. function it_holds_value()
  19. {
  20. $this->getValue()->shouldReturn(42);
  21. }
  22. function it_scores_10_if_value_is_equal_to_argument()
  23. {
  24. $this->scoreArgument(42)->shouldReturn(10);
  25. $this->scoreArgument('42')->shouldReturn(10);
  26. }
  27. function it_scores_10_if_value_is_an_object_and_equal_to_argument()
  28. {
  29. $value = new \DateTime();
  30. $value2 = clone $value;
  31. $this->beConstructedWith($value);
  32. $this->scoreArgument($value2)->shouldReturn(10);
  33. }
  34. function it_does_not_scores_if_value_is_not_equal_to_argument()
  35. {
  36. $this->scoreArgument(50)->shouldReturn(false);
  37. $this->scoreArgument(new \stdClass())->shouldReturn(false);
  38. }
  39. function it_does_not_scores_if_value_an_object_and_is_not_equal_to_argument()
  40. {
  41. $value = new ExactValueTokenFixtureB('ABC');
  42. $value2 = new ExactValueTokenFixtureB('CBA');
  43. $this->beConstructedWith($value);
  44. $this->scoreArgument($value2)->shouldReturn(false);
  45. }
  46. function it_does_not_scores_if_value_type_and_is_not_equal_to_argument()
  47. {
  48. $this->beConstructedWith(false);
  49. $this->scoreArgument(0)->shouldReturn(false);
  50. }
  51. function it_generates_proper_string_representation_for_integer()
  52. {
  53. $this->beConstructedWith(42);
  54. $this->__toString()->shouldReturn('exact(42)');
  55. }
  56. function it_generates_proper_string_representation_for_string()
  57. {
  58. $this->beConstructedWith('some string');
  59. $this->__toString()->shouldReturn('exact("some string")');
  60. }
  61. function it_generates_single_line_representation_for_multiline_string()
  62. {
  63. $this->beConstructedWith("some\nstring");
  64. $this->__toString()->shouldReturn('exact("some\\nstring")');
  65. }
  66. function it_generates_proper_string_representation_for_double()
  67. {
  68. $this->beConstructedWith(42.3);
  69. $this->__toString()->shouldReturn('exact(42.3)');
  70. }
  71. function it_generates_proper_string_representation_for_boolean_true()
  72. {
  73. $this->beConstructedWith(true);
  74. $this->__toString()->shouldReturn('exact(true)');
  75. }
  76. function it_generates_proper_string_representation_for_boolean_false()
  77. {
  78. $this->beConstructedWith(false);
  79. $this->__toString()->shouldReturn('exact(false)');
  80. }
  81. function it_generates_proper_string_representation_for_null()
  82. {
  83. $this->beConstructedWith(null);
  84. $this->__toString()->shouldReturn('exact(null)');
  85. }
  86. function it_generates_proper_string_representation_for_empty_array()
  87. {
  88. $this->beConstructedWith(array());
  89. $this->__toString()->shouldReturn('exact([])');
  90. }
  91. function it_generates_proper_string_representation_for_array()
  92. {
  93. $this->beConstructedWith(array('zet', 42));
  94. $this->__toString()->shouldReturn('exact(["zet", 42])');
  95. }
  96. function it_generates_proper_string_representation_for_resource()
  97. {
  98. $resource = fopen(__FILE__, 'r');
  99. $this->beConstructedWith($resource);
  100. $this->__toString()->shouldReturn('exact(stream:'.$resource.')');
  101. }
  102. function it_generates_proper_string_representation_for_object(\stdClass $object)
  103. {
  104. $objHash = sprintf('%s:%s',
  105. get_class($object->getWrappedObject()),
  106. spl_object_hash($object->getWrappedObject())
  107. );
  108. $this->beConstructedWith($object);
  109. $this->__toString()->shouldReturn("exact($objHash Object (\n 'objectProphecy' => Prophecy\Prophecy\ObjectProphecy Object (*Prophecy*)\n))");
  110. }
  111. }
  112. class ExactValueTokenFixtureA
  113. {
  114. public $errors;
  115. }
  116. class ExactValueTokenFixtureB extends ExactValueTokenFixtureA
  117. {
  118. public $errors;
  119. public $value = null;
  120. public function __construct($value)
  121. {
  122. $this->value = $value;
  123. }
  124. }