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