ContextTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. * @covers SebastianBergmann\RecursionContext\Context::add
  72. * @uses SebastianBergmann\RecursionContext\InvalidArgumentException
  73. * @dataProvider failsProvider
  74. */
  75. public function testAddFails($value)
  76. {
  77. $this->setExpectedException(
  78. 'SebastianBergmann\\RecursionContext\\Exception',
  79. 'Only arrays and objects are supported'
  80. );
  81. $this->context->add($value);
  82. }
  83. /**
  84. * @covers SebastianBergmann\RecursionContext\Context::contains
  85. * @uses SebastianBergmann\RecursionContext\InvalidArgumentException
  86. * @dataProvider failsProvider
  87. */
  88. public function testContainsFails($value)
  89. {
  90. $this->setExpectedException(
  91. 'SebastianBergmann\\RecursionContext\\Exception',
  92. 'Only arrays and objects are supported'
  93. );
  94. $this->context->contains($value);
  95. }
  96. /**
  97. * @covers SebastianBergmann\RecursionContext\Context::add
  98. * @dataProvider valuesProvider
  99. */
  100. public function testAdd($value, $key)
  101. {
  102. $this->assertEquals($key, $this->context->add($value));
  103. // Test we get the same key on subsequent adds
  104. $this->assertEquals($key, $this->context->add($value));
  105. }
  106. /**
  107. * @covers SebastianBergmann\RecursionContext\Context::contains
  108. * @uses SebastianBergmann\RecursionContext\Context::add
  109. * @depends testAdd
  110. * @dataProvider valuesProvider
  111. */
  112. public function testContainsFound($value, $key)
  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. * @covers SebastianBergmann\RecursionContext\Context::contains
  121. * @dataProvider valuesProvider
  122. */
  123. public function testContainsNotFound($value)
  124. {
  125. $this->assertFalse($this->context->contains($value));
  126. }
  127. }