Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use PHPUnit\Framework\TestCase;
- /**
- * Created by PhpStorm.
- * User: szabo
- * Date: 16.06.2018
- * Time: 17:03
- */
- class ArrayGroupByOnKeyTest extends TestCase {
- private $sample = [
- [
- "state" => "IN",
- "city" => "Indianapolis",
- "object" => "School bus"
- ],
- [
- "state" => "IN",
- "city" => "Indianapolis",
- "object" => "Manhole"
- ],
- [
- "state" => "IN",
- "city" => "Plainfield",
- "object" => "Basketball"
- ],
- [
- "state" => "CA",
- "city" => "San Diego",
- "object" => "Light bulb"
- ],
- [
- "state" => "CA",
- "city" => "Mountain View",
- "object" => "Space pen"
- ]
- ];
- /**
- * @test
- */
- public function groupByState() {
- $expectedResult = [
- "IN" => [
- [
- "state" => "IN",
- "city" => "Indianapolis",
- "object" => "School bus"
- ],
- [
- "state" => "IN",
- "city" => "Indianapolis",
- "object" => "Manhole"
- ],
- [
- "state" => "IN",
- "city" => "Plainfield",
- "object" => "Basketball"
- ],
- ],
- "CA" => [
- [
- "state" => "CA",
- "city" => "San Diego",
- "object" => "Light bulb"
- ],
- [
- "state" => "CA",
- "city" => "Mountain View",
- "object" => "Space pen"
- ]
- ]
- ];
- $this->assertEquals($expectedResult, array_group_by($this->sample, "state"));
- }
- /**
- * @test
- */
- public function groupByCity() {
- $expectedResult = [
- "Indianapolis" => [
- [
- "state" => "IN",
- "city" => "Indianapolis",
- "object" => "School bus"
- ],
- [
- "state" => "IN",
- "city" => "Indianapolis",
- "object" => "Manhole"
- ],
- ],
- "Plainfield" => [
- [
- "state" => "IN",
- "city" => "Plainfield",
- "object" => "Basketball"
- ]
- ],
- "San Diego" => [
- [
- "state" => "CA",
- "city" => "San Diego",
- "object" => "Light bulb"
- ]
- ],
- "Mountain View" => [
- [
- "state" => "CA",
- "city" => "Mountain View",
- "object" => "Space pen"
- ]
- ]
- ];
- $result = array_group_by($this->sample, "city");
- $this->assertEquals($expectedResult, $result);
- }
- }
Add Comment
Please, Sign In to add comment