ContextTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 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. * @covers SebastianBergmann\RecursionContext\Context::add
  72. * @uses SebastianBergmann\RecursionContext\InvalidArgumentException
  73. * @dataProvider failsProvider
  74. */
  75. public function testAddFails($value)
  76. {
  77. $this->expectException(Exception::class);
  78. $this->expectExceptionMessage('Only arrays and objects are supported');
  79. $this->context->add($value);
  80. }
  81. /**
  82. * @covers SebastianBergmann\RecursionContext\Context::contains
  83. * @uses SebastianBergmann\RecursionContext\InvalidArgumentException
  84. * @dataProvider failsProvider
  85. */
  86. public function testContainsFails($value)
  87. {
  88. $this->expectException(Exception::class);
  89. $this->expectExceptionMessage('Only arrays and objects are supported');
  90. $this->context->contains($value);
  91. }
  92. /**
  93. * @covers SebastianBergmann\RecursionContext\Context::add
  94. * @dataProvider valuesProvider
  95. */
  96. public function testAdd($value, $key)
  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. /**
  103. * @covers SebastianBergmann\RecursionContext\Context::contains
  104. * @uses SebastianBergmann\RecursionContext\Context::add
  105. * @depends testAdd
  106. * @dataProvider valuesProvider
  107. */
  108. public function testContainsFound($value, $key)
  109. {
  110. $this->context->add($value);
  111. $this->assertEquals($key, $this->context->contains($value));
  112. // Test we get the same key on subsequent calls
  113. $this->assertEquals($key, $this->context->contains($value));
  114. }
  115. /**
  116. * @covers SebastianBergmann\RecursionContext\Context::contains
  117. * @dataProvider valuesProvider
  118. */
  119. public function testContainsNotFound($value)
  120. {
  121. $this->assertFalse($this->context->contains($value));
  122. }
  123. }