PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ArrayBeast/Tests/ArrayBeastTest.php

https://bitbucket.org/cfralick/arraybeast
PHP | 192 lines | 124 code | 25 blank | 43 comment | 2 complexity | 66eadec80fdee80092d7e58de78f775c MD5 | raw file
  1. <?php
  2. namespace ArrayBeast\Tests;
  3. use ArrayBeast\ArrayBeast;
  4. class ArrayBeastTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public $sample_data;
  7. protected function setUp()
  8. {
  9. $this->sample_data = array(
  10. 'key' => 'stringValue',
  11. 'index' => 30,
  12. 'property' => true,
  13. 'identifier' => array(1, 2, 3),
  14. 'innerarray' => array('str' => 'astring', 'num' => 10)
  15. );
  16. }
  17. public function provider()
  18. {
  19. $data = [];
  20. $size = rand(5, 40);
  21. for($i = 1; $i <= $size; $i++) {
  22. $data[(String) substr(md5(uniqid(rand())), 2,20)] = substr(md5(rand()), 1, 18);
  23. }
  24. return $data;
  25. }
  26. /**
  27. * @covers ArrayBeast\ArrayBeast::__construct
  28. * @covers ArrayBeast\ArrayBeast::getIterator
  29. * @covers ArrayBeast\ArrayBeast::keys
  30. * @covers ArrayBeast\ArrayBeast::values
  31. * @covers ArrayBeast\ArrayBeast::count
  32. */
  33. public function testInstantiation()
  34. {
  35. $this->assertInstanceOf('ArrayBeast\ArrayBeast', new ArrayBeast());
  36. $sample_data = $this->provider();
  37. $arrayBeast = new ArrayBeast($sample_data);
  38. $this->assertEquals(count($sample_data), $arrayBeast->count());
  39. $this->assertInstanceOf('\Iterator', $arrayBeast->getIterator());
  40. $this->assertInstanceOf('\SplFixedArray', $arrayBeast->values());
  41. $this->assertInstanceOf('\SplFixedArray', $arrayBeast->keys());
  42. }
  43. /**
  44. * @covers ArrayBeast\ArrayBeast::first
  45. */
  46. public function testFirstMethod()
  47. {
  48. $arrayBeast = new ArrayBeast();
  49. $this->assertFalse($arrayBeast->first(function($c, $k, $i) { return isset($c);}));
  50. $arrayBeast['value'] = true;
  51. $this->assertTrue($arrayBeast->first(function($c, $k, $i) { return $c === true;}));
  52. }
  53. /**
  54. * @covers ArrayBeast\ArrayBeast::each
  55. * @covers ArrayBeast\ArrayBeast::getArrayCopy
  56. * @covers ArrayBeast\ArrayBeast::filter
  57. * @covers ArrayBeast\ArrayBeast::purge
  58. * @covers ArrayBeast\ArrayBeast::flatten
  59. */
  60. public function testMethodsThatChangeOriginalValues()
  61. {
  62. $arrayBeast = new ArrayBeast($this->provider());
  63. $original_values = $arrayBeast->getArrayCopy();
  64. $arrayBeast->each(function($item) { return [$item]; });
  65. $modified_values = $arrayBeast->getArrayCopy();
  66. $this->assertNotSame($original_values, $modified_values);
  67. $this->assertSame(array_keys($original_values), array_keys($modified_values));
  68. $arrayBeast->purge(function($x) { return !is_array($x) || preg_match('/[a-z][0-9][a-z]/', $x[0]);});
  69. $this->assertNotSame($modified_values, $arrayBeast->getArrayCopy());
  70. $flattened = $arrayBeast->flatten();
  71. foreach($flattened as $value) {
  72. $this->assertNotInternalType('array', $value);
  73. }
  74. }
  75. /**
  76. * @covers ArrayBeast\ArrayBeast::clear
  77. */
  78. public function testClear()
  79. {
  80. $arrayBeast = new ArrayBeast($this->provider());
  81. $this->assertGreaterThan(0, $arrayBeast->count());
  82. $arrayBeast->clear();
  83. $this->assertEquals(0, $arrayBeast->count());
  84. }
  85. /**
  86. * @covers ArrayBeast\ArrayBeast::shift
  87. */
  88. public function testShift()
  89. {
  90. $arrayBeast = new ArrayBeast($this->sample_data);
  91. $slice = $arrayBeast->shift('index');
  92. $this->assertSame(30, $slice);
  93. }
  94. /**
  95. * @covers ArrayBeast\ArrayBeast::select
  96. */
  97. public function testSelect()
  98. {
  99. $arrayBeast = new ArrayBeast($this->sample_data);
  100. $slice = $arrayBeast->select(['index']);
  101. $this->assertEquals(1, $slice->count());
  102. }
  103. /**
  104. * @covers ArrayBeast\ArrayBeast::reassign
  105. */
  106. public function testReassign()
  107. {
  108. $arrayBeast = new ArrayBeast($this->sample_data);
  109. $arrayBeast->reassign('property', 'item');
  110. $this->assertNull($arrayBeast['property']);
  111. }
  112. /**
  113. * @covers ArrayBeast\ArrayBeast::map
  114. * @covers ArrayBeast\ArrayBeast::mapIf
  115. */
  116. public function testMapping()
  117. {
  118. $arrayBeast = new ArrayBeast($this->provider());
  119. $arrayBeastMap = $arrayBeast->map(function($v) { return new ArrayBeast([$v]); });
  120. $this->assertInstanceOf('\ArrayBeast\ArrayBeast', $arrayBeastMap);
  121. foreach($arrayBeastMap as $key => $value) {
  122. $this->assertInstanceOf('\ArrayBeast\ArrayBeast', $value);
  123. }
  124. $original_count = $arrayBeast->count();
  125. $arrayBeastMapIf = $arrayBeast->mapIf(function($x) { return preg_match('/[a8ehgs]/', $x); }, function($v) { return new ArrayBeast([$v]); });
  126. $this->assertInstanceOf('\ArrayBeast\ArrayBeast', $arrayBeastMapIf);
  127. foreach($arrayBeastMapIf as $key => $value) {
  128. $this->assertInstanceOf('\ArrayBeast\ArrayBeast', $value);
  129. }
  130. $this->assertGreaterThanOrEqual($arrayBeastMapIf->count(), $original_count);
  131. }
  132. /**
  133. * @covers ArrayBeast\ArrayBeast::merge
  134. * @covers ArrayBeast\ArrayBeast::filter
  135. */
  136. public function testMergeAndFilter()
  137. {
  138. $initial_data = $this->provider();
  139. $merge_data = $this->provider();
  140. $arrayBeast = new ArrayBeast($initial_data);
  141. $arrayBeast->merge($merge_data);
  142. $this->assertSame(array_merge($initial_data, $merge_data), $arrayBeast->getArrayCopy());
  143. $pattern = '/[a-z][nvflasjoiewt0-9]/';
  144. $arrayBeastFiltered = $arrayBeast->filter(function($x) use ($pattern) { return preg_match($pattern, $x); });
  145. foreach($arrayBeastFiltered as $key => $value) {
  146. $this->assertRegExp($pattern, $value);
  147. }
  148. }
  149. /**
  150. * @covers ArrayBeast\ArrayBeast::pick
  151. */
  152. public function testPickSimpleKeySelect()
  153. {
  154. $arrayBeast = new ArrayBeast($this->sample_data);
  155. $result = $arrayBeast->pick(array('index'));
  156. $this->assertSame(30, $result['index']);
  157. }
  158. /**
  159. * @covers ArrayBeast\ArrayBeast::pick
  160. */
  161. public function testPickApplyClosure()
  162. {
  163. $arrayBeast = new ArrayBeast($this->sample_data);
  164. $result = $arrayBeast->pick(array('index' => function($i) { return $i * 2; }));
  165. $this->assertSame(60, $result['index']);
  166. }
  167. }