Guest User

Untitled

a guest
Jun 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3.  
  4. /**
  5. * Created by PhpStorm.
  6. * User: szabo
  7. * Date: 16.06.2018
  8. * Time: 17:03
  9. */
  10. class ArrayGroupByOnKeyTest extends TestCase {
  11.  
  12. private $sample = [
  13. [
  14. "state" => "IN",
  15. "city" => "Indianapolis",
  16. "object" => "School bus"
  17. ],
  18. [
  19. "state" => "IN",
  20. "city" => "Indianapolis",
  21. "object" => "Manhole"
  22. ],
  23. [
  24. "state" => "IN",
  25. "city" => "Plainfield",
  26. "object" => "Basketball"
  27. ],
  28. [
  29. "state" => "CA",
  30. "city" => "San Diego",
  31. "object" => "Light bulb"
  32. ],
  33. [
  34. "state" => "CA",
  35. "city" => "Mountain View",
  36. "object" => "Space pen"
  37. ]
  38. ];
  39.  
  40. /**
  41. * @test
  42. */
  43. public function groupByState() {
  44.  
  45. $expectedResult = [
  46. "IN" => [
  47. [
  48. "state" => "IN",
  49. "city" => "Indianapolis",
  50. "object" => "School bus"
  51. ],
  52. [
  53. "state" => "IN",
  54. "city" => "Indianapolis",
  55. "object" => "Manhole"
  56. ],
  57. [
  58. "state" => "IN",
  59. "city" => "Plainfield",
  60. "object" => "Basketball"
  61. ],
  62. ],
  63. "CA" => [
  64. [
  65. "state" => "CA",
  66. "city" => "San Diego",
  67. "object" => "Light bulb"
  68. ],
  69. [
  70. "state" => "CA",
  71. "city" => "Mountain View",
  72. "object" => "Space pen"
  73. ]
  74. ]
  75. ];
  76.  
  77. $this->assertEquals($expectedResult, array_group_by($this->sample, "state"));
  78. }
  79.  
  80. /**
  81. * @test
  82. */
  83. public function groupByCity() {
  84. $expectedResult = [
  85. "Indianapolis" => [
  86. [
  87. "state" => "IN",
  88. "city" => "Indianapolis",
  89. "object" => "School bus"
  90. ],
  91. [
  92. "state" => "IN",
  93. "city" => "Indianapolis",
  94. "object" => "Manhole"
  95. ],
  96. ],
  97. "Plainfield" => [
  98. [
  99. "state" => "IN",
  100. "city" => "Plainfield",
  101. "object" => "Basketball"
  102. ]
  103. ],
  104. "San Diego" => [
  105. [
  106. "state" => "CA",
  107. "city" => "San Diego",
  108. "object" => "Light bulb"
  109. ]
  110. ],
  111. "Mountain View" => [
  112. [
  113. "state" => "CA",
  114. "city" => "Mountain View",
  115. "object" => "Space pen"
  116. ]
  117. ]
  118. ];
  119.  
  120. $result = array_group_by($this->sample, "city");
  121.  
  122. $this->assertEquals($expectedResult, $result);
  123. }
  124. }
Add Comment
Please, Sign In to add comment