/vendor/klein/klein/tests/Klein/Tests/DataCollection/DataCollectionTest.php

https://bitbucket.org/jncormier/edit.or · PHP · 356 lines · 182 code · 65 blank · 109 comment · 2 complexity · 1dd2d68f0b9a0f1434300c91582e5b18 MD5 · raw file

  1. <?php
  2. /**
  3. * Klein (klein.php) - A lightning fast router for PHP
  4. *
  5. * @author Chris O'Hara <cohara87@gmail.com>
  6. * @author Trevor Suarez (Rican7) (contributor and v2 refactorer)
  7. * @copyright (c) Chris O'Hara
  8. * @link https://github.com/chriso/klein.php
  9. * @license MIT
  10. */
  11. namespace Klein\Tests\DataCollection;
  12. use \stdClass;
  13. use \Klein\Tests\AbstractKleinTest;
  14. use \Klein\DataCollection\DataCollection;
  15. /**
  16. * DataCollectionTest
  17. *
  18. * @uses AbstractKleinTest
  19. * @package Klein\Tests\DataCollection
  20. */
  21. class DataCollectionTest extends AbstractKleinTest
  22. {
  23. /**
  24. * Non existent key in the sample data
  25. *
  26. * @static
  27. * @var string
  28. * @access protected
  29. */
  30. protected static $nonexistent_key = 'key-name-doesnt-exist';
  31. /*
  32. * Data Providers and Methods
  33. */
  34. /**
  35. * Quickly makes sure that no sample data arrays
  36. * have any keys that match the "nonexistent_key"
  37. *
  38. * @param array $sample_data
  39. * @access protected
  40. * @return void
  41. */
  42. protected function prepareSampleData(&$sample_data)
  43. {
  44. if (isset($sample_data[static::$nonexistent_key])) {
  45. unset($sample_data[static::$nonexistent_key]);
  46. }
  47. foreach ($sample_data as &$data) {
  48. if (is_array($data)) {
  49. $this->prepareSampleData($data);
  50. }
  51. }
  52. }
  53. /**
  54. * Sample data provider
  55. *
  56. * @access public
  57. * @return array
  58. */
  59. public function sampleDataProvider()
  60. {
  61. // Populate our sample data
  62. $sample_data = array(
  63. 'id' => 1337,
  64. 'name' => array(
  65. 'first' => 'Trevor',
  66. 'last' => 'Suarez',
  67. ),
  68. 'float' => 13.37,
  69. 'thing' => new stdClass(),
  70. );
  71. $this->prepareSampleData($sample_data);
  72. $data_collection = new DataCollection($sample_data);
  73. return array(
  74. array($sample_data, $data_collection),
  75. );
  76. }
  77. /**
  78. * Totally different sample data provider
  79. *
  80. * @access public
  81. * @return array
  82. */
  83. public function totallyDifferentSampleDataProvider()
  84. {
  85. // Populate our sample data
  86. $totally_different_sample_data = array(
  87. '_why' => 'the lucky stiff',
  88. 'php' => 'has become beautiful',
  89. 'yay' => 'life is very good. :)',
  90. );
  91. $this->prepareSampleData($totally_different_sample_data);
  92. return array(
  93. array($totally_different_sample_data),
  94. );
  95. }
  96. /*
  97. * Tests
  98. */
  99. /**
  100. * @dataProvider sampleDataProvider
  101. */
  102. public function testAll($sample_data, $data_collection)
  103. {
  104. // Test basic data similarity
  105. $this->assertSame($sample_data, $data_collection->all());
  106. // Create mask
  107. $mask = array('float', static::$nonexistent_key);
  108. $this->assertArrayHasKey($mask[0], $data_collection->all($mask));
  109. $this->assertArrayHasKey($mask[1], $data_collection->all($mask));
  110. $this->assertArrayNotHasKey(key($sample_data), $data_collection->all($mask));
  111. // Test more "magical" way of inputting mask
  112. $this->assertArrayHasKey($mask[0], $data_collection->all($mask[0], $mask[1]));
  113. $this->assertArrayHasKey($mask[1], $data_collection->all($mask[0], $mask[1]));
  114. $this->assertArrayNotHasKey(key($sample_data), $data_collection->all($mask[0], $mask[1]));
  115. }
  116. /**
  117. * @dataProvider sampleDataProvider
  118. */
  119. public function testGet($sample_data, $data_collection)
  120. {
  121. $default = 'WOOT!';
  122. $this->assertSame($sample_data['id'], $data_collection->get('id'));
  123. $this->assertSame($default, $data_collection->get(static::$nonexistent_key, $default));
  124. $this->assertNull($data_collection->get(static::$nonexistent_key));
  125. }
  126. public function testSet()
  127. {
  128. // Test data
  129. $data = array(
  130. 'dog' => 'cooper',
  131. );
  132. // Create our collection with NO data
  133. $data_collection = new DataCollection();
  134. // Make sure its first empty
  135. $this->assertSame(array(), $data_collection->all());
  136. // Set our data from our test data
  137. $return_val = $data_collection->set(key($data), current($data));
  138. // Make sure the set worked
  139. $this->assertSame(current($data), $data_collection->get(key($data)));
  140. // Make sure it returned the instance during "set"
  141. $this->assertEquals($return_val, $data_collection);
  142. $this->assertSame($return_val, $data_collection);
  143. }
  144. /**
  145. * @dataProvider sampleDataProvider
  146. */
  147. public function testReplace($sample_data, $data_collection)
  148. {
  149. $totally_different_sample_data = current(
  150. current($this->totallyDifferentSampleDataProvider())
  151. );
  152. $data_collection->replace($totally_different_sample_data);
  153. $this->assertNotSame($sample_data, $totally_different_sample_data);
  154. $this->assertNotSame($sample_data, $data_collection->all());
  155. $this->assertSame($totally_different_sample_data, $data_collection->all());
  156. }
  157. /**
  158. * @dataProvider sampleDataProvider
  159. */
  160. public function testMerge($sample_data, $data_collection)
  161. {
  162. $totally_different_sample_data = current(
  163. current($this->totallyDifferentSampleDataProvider())
  164. );
  165. $merged_data = array_merge($sample_data, $totally_different_sample_data);
  166. $data_collection->merge($totally_different_sample_data);
  167. $this->assertNotSame($sample_data, $totally_different_sample_data);
  168. $this->assertNotSame($sample_data, $data_collection->all());
  169. $this->assertNotSame($totally_different_sample_data, $data_collection->all());
  170. $this->assertSame($merged_data, $data_collection->all());
  171. }
  172. /**
  173. * @dataProvider sampleDataProvider
  174. */
  175. public function testMergeHard($sample_data, $data_collection)
  176. {
  177. $totally_different_sample_data = current(
  178. current($this->totallyDifferentSampleDataProvider())
  179. );
  180. $replaced_data = array_replace($sample_data, $totally_different_sample_data);
  181. $data_collection->merge($totally_different_sample_data, true);
  182. $this->assertNotSame($sample_data, $totally_different_sample_data);
  183. $this->assertNotSame($sample_data, $data_collection->all());
  184. $this->assertNotSame($totally_different_sample_data, $data_collection->all());
  185. $this->assertSame($replaced_data, $data_collection->all());
  186. }
  187. /**
  188. * @dataProvider sampleDataProvider
  189. */
  190. public function testExists($sample_data, $data_collection)
  191. {
  192. $this->assertTrue($data_collection->exists(key($sample_data)));
  193. $this->assertFalse($data_collection->exists(static::$nonexistent_key));
  194. }
  195. /**
  196. * @dataProvider sampleDataProvider
  197. */
  198. public function testRemove($sample_data, $data_collection)
  199. {
  200. $this->assertTrue($data_collection->exists(key($sample_data)));
  201. $data_collection->remove(key($sample_data));
  202. $this->assertFalse($data_collection->exists(key($sample_data)));
  203. }
  204. /**
  205. * @dataProvider sampleDataProvider
  206. */
  207. public function testMagicGet($sample_data, $data_collection)
  208. {
  209. $this->assertSame($sample_data['float'], $data_collection->float);
  210. $this->assertNull($data_collection->{static::$nonexistent_key});
  211. }
  212. public function testMagicSet()
  213. {
  214. // Test data
  215. $data = array(
  216. 'dog' => 'cooper',
  217. );
  218. // Create our collection with NO data
  219. $data_collection = new DataCollection();
  220. // Set our data from our test data
  221. $data_collection->{key($data)} = current($data);
  222. // Make sure the set worked
  223. $this->assertSame(current($data), $data_collection->get(key($data)));
  224. }
  225. /**
  226. * @dataProvider sampleDataProvider
  227. */
  228. public function testMagicIsset($sample_data, $data_collection)
  229. {
  230. $this->assertTrue(isset($data_collection->{key($sample_data)}));
  231. $this->assertFalse(isset($data_collection->{static::$nonexistent_key}));
  232. }
  233. /**
  234. * @dataProvider sampleDataProvider
  235. */
  236. public function testMagicUnset($sample_data, $data_collection)
  237. {
  238. $this->assertTrue(isset($data_collection->{key($sample_data)}));
  239. unset($data_collection->{key($sample_data)});
  240. $this->assertFalse(isset($data_collection->{key($sample_data)}));
  241. }
  242. /**
  243. * @dataProvider sampleDataProvider
  244. */
  245. public function testIteratorAggregate($sample_data, $data_collection)
  246. {
  247. $filled_data = array();
  248. foreach ($data_collection as $key => $data) {
  249. $filled_data[$key] = $data;
  250. }
  251. $this->assertSame($filled_data, $sample_data);
  252. }
  253. /**
  254. * @dataProvider sampleDataProvider
  255. */
  256. public function testArrayAccessGet($sample_data, $data_collection)
  257. {
  258. $this->assertSame($sample_data['float'], $data_collection['float']);
  259. $this->assertNull($data_collection[static::$nonexistent_key]);
  260. }
  261. public function testArrayAccessSet()
  262. {
  263. // Test data
  264. $data = array(
  265. 'dog' => 'cooper',
  266. );
  267. // Create our collection with NO data
  268. $data_collection = new DataCollection();
  269. // Set our data from our test data
  270. $data_collection[key($data)] = current($data);
  271. // Make sure the set worked
  272. $this->assertSame(current($data), $data_collection->get(key($data)));
  273. }
  274. /**
  275. * @dataProvider sampleDataProvider
  276. */
  277. public function testArrayAccessIsset($sample_data, $data_collection)
  278. {
  279. $this->assertTrue(isset($data_collection[key($sample_data)]));
  280. $this->assertFalse(isset($data_collection[static::$nonexistent_key]));
  281. }
  282. /**
  283. * @dataProvider sampleDataProvider
  284. */
  285. public function testArrayAccessUnset($sample_data, $data_collection)
  286. {
  287. $this->assertTrue(isset($data_collection[key($sample_data)]));
  288. unset($data_collection[key($sample_data)]);
  289. $this->assertFalse(isset($data_collection[key($sample_data)]));
  290. }
  291. }