ContextTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /*
  3. * This file is part of the Recursion Context package.
  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. namespace SebastianBergmann\RecursionContext;
  11. use PHPUnit_Framework_TestCase;
  12. /**
  13. * @covers SebastianBergmann\RecursionContext\Context
  14. */
  15. class ContextTest extends PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @var \SebastianBergmann\RecursionContext\Context
  19. */
  20. private $context;
  21. protected function setUp()
  22. {
  23. $this->context = new Context;
  24. }
  25. public function failsProvider()
  26. {
  27. return array(
  28. array(true),
  29. array(false),
  30. array(null),
  31. array('string'),
  32. array(1),
  33. array(1.5),
  34. array(fopen('php://memory', 'r'))
  35. );
  36. }
  37. public function valuesProvider()
  38. {
  39. $obj2 = new \stdClass;
  40. $obj2->foo = 'bar';
  41. $obj3 = (object) array(1,2,"Test\r\n",4,5,6,7,8);
  42. $obj = new \stdClass;
  43. //@codingStandardsIgnoreStart
  44. $obj->null = null;
  45. //@codingStandardsIgnoreEnd
  46. $obj->boolean = true;
  47. $obj->integer = 1;
  48. $obj->double = 1.2;
  49. $obj->string = '1';
  50. $obj->text = "this\nis\na\nvery\nvery\nvery\nvery\nvery\nvery\rlong\n\rtext";
  51. $obj->object = $obj2;
  52. $obj->objectagain = $obj2;
  53. $obj->array = array('foo' => 'bar');
  54. $obj->array2 = array(1,2,3,4,5,6);
  55. $obj->array3 = array($obj, $obj2, $obj3);
  56. $obj->self = $obj;
  57. $storage = new \SplObjectStorage;
  58. $storage->attach($obj2);
  59. $storage->foo = $obj2;
  60. return array(
  61. array($obj, spl_object_hash($obj)),
  62. array($obj2, spl_object_hash($obj2)),
  63. array($obj3, spl_object_hash($obj3)),
  64. array($storage, spl_object_hash($storage)),
  65. array($obj->array, 0),
  66. array($obj->array2, 0),
  67. array($obj->array3, 0)
  68. );
  69. }
  70. /**
  71. * @dataProvider failsProvider
  72. */
  73. public function testAddFails($value)
  74. {
  75. $this->setExpectedException(
  76. 'SebastianBergmann\\RecursionContext\\Exception',
  77. 'Only arrays and objects are supported'
  78. );
  79. $this->context->add($value);
  80. }
  81. /**
  82. * @dataProvider failsProvider
  83. */
  84. public function testContainsFails($value)
  85. {
  86. $this->setExpectedException(
  87. 'SebastianBergmann\\RecursionContext\\Exception',
  88. 'Only arrays and objects are supported'
  89. );
  90. $this->context->contains($value);
  91. }
  92. /**
  93. * @dataProvider valuesProvider
  94. */
  95. public function testAdd($value, $key)
  96. {
  97. $this->assertEquals($key, $this->context->add($value));
  98. // Test we get the same key on subsequent adds
  99. $this->assertEquals($key, $this->context->add($value));
  100. }
  101. /**
  102. * @requires PHP 7
  103. */
  104. public function testAdd2()
  105. {
  106. $a = array(PHP_INT_MAX => 'foo');
  107. $this->context->add($a);
  108. $this->assertInternalType('int', $this->context->contains($a));
  109. }
  110. /**
  111. * @depends testAdd
  112. * @dataProvider valuesProvider
  113. */
  114. public function testContainsFound($value, $key)
  115. {
  116. $this->context->add($value);
  117. $this->assertEquals($key, $this->context->contains($value));
  118. // Test we get the same key on subsequent calls
  119. $this->assertEquals($key, $this->context->contains($value));
  120. }
  121. /**
  122. * @dataProvider valuesProvider
  123. */
  124. public function testContainsNotFound($value)
  125. {
  126. $this->assertFalse($this->context->contains($value));
  127. }
  128. }