1
0

ArrayHelperTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace fphammerle\helpers\tests;
  3. use fphammerle\helpers\ArrayHelper;
  4. class ArrayHelperTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function testFlattenEmpty()
  7. {
  8. $this->assertEquals(
  9. [],
  10. ArrayHelper::flatten([])
  11. );
  12. }
  13. public function testFlattenFlat()
  14. {
  15. $this->assertEquals(
  16. [1, 2, 3],
  17. ArrayHelper::flatten([1, 2, 3])
  18. );
  19. }
  20. public function testFlatten2D()
  21. {
  22. $this->assertEquals(
  23. [1, 2, 3, 4, 5],
  24. ArrayHelper::flatten([1, [2, 3], [4], 5])
  25. );
  26. }
  27. public function testFlatten2DEmpty()
  28. {
  29. $this->assertEquals(
  30. [1, 2, 3, 4, 5],
  31. ArrayHelper::flatten([[], 1, [2, 3], [], [4], 5])
  32. );
  33. }
  34. public function testFlatten3D()
  35. {
  36. $this->assertEquals(
  37. [1, 2, 3, 4, 5, 6],
  38. ArrayHelper::flatten([1, [[2, 3], 4], [[5], [6]]])
  39. );
  40. }
  41. public function testFlatten3DEmpty()
  42. {
  43. $this->assertEquals(
  44. [1, 2, 3, 4, 5, 6],
  45. ArrayHelper::flatten([1, [[2, [], 3], 4, []], [[5], [6]], []])
  46. );
  47. }
  48. public function testFlattenAssociativeLetterFlat()
  49. {
  50. $this->assertEquals(
  51. [1, 2, 3, 4],
  52. ArrayHelper::flatten(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4])
  53. );
  54. }
  55. public function testFlattenAssociativeDigit()
  56. {
  57. $this->assertEquals(
  58. [1, 2, 3, 4],
  59. ArrayHelper::flatten([1 => 1, 2 => 2, 3 => 3, 4 => 4])
  60. );
  61. }
  62. public function testFlattenAssociativeDigitReverse()
  63. {
  64. $this->assertEquals(
  65. [1, 2, 3, 4],
  66. ArrayHelper::flatten([3 => 1, 2 => 2, 1 => 3, 0 => 4])
  67. );
  68. }
  69. public function testFlattenMixed2D()
  70. {
  71. $this->assertEquals(
  72. [1, 2, 3, 4, 5, 6, 7, 8],
  73. ArrayHelper::flatten([
  74. 'a' => 1,
  75. 'b' => [2, 3],
  76. 'c' => ['#' => 4, '@' => 5],
  77. 'd' => [6, '$' => 7],
  78. 8,
  79. ])
  80. );
  81. }
  82. }