ContextTest.php 3.7 KB

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