Created
April 20, 2020 14:53
-
-
Save davidyell/3cabd7aabd0e4919299a9ba451afe4ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use PHPUnit\Framework\TestCase; | |
class ArraySplitTest extends TestCase | |
{ | |
/** | |
* Array of data to test with, items which are slot_type 1 are regular, and slot_type 2 split up groups | |
* | |
* @var array[] | |
*/ | |
public $data = [ | |
[ | |
'name' => 'First', | |
'slot' => [ | |
'slot_type' => 1, | |
'type' => [ | |
'id' => 1 | |
] | |
] | |
], | |
[ | |
'name' => 'Go to second', | |
'slot' => [ | |
'slot_type' => 2, | |
'type' => [ | |
'id' => 2 | |
] | |
] | |
], | |
[ | |
'name' => 'Third', | |
'slot' => [ | |
'slot_type' => 1, | |
'type' => [ | |
'id' => 1 | |
] | |
] | |
], | |
[ | |
'name' => 'Go to fifth', | |
'slot' => [ | |
'slot_type' => 2, | |
'type' => [ | |
'id' => 2 | |
] | |
] | |
], | |
[ | |
'name' => 'Fifth', | |
'slot' => [ | |
'slot_type' => 1, | |
'type' => [ | |
'id' => 1 | |
] | |
] | |
], | |
[ | |
'name' => 'Sixth', | |
'slot' => [ | |
'slot_type' => 1, | |
'type' => [ | |
'id' => 1 | |
] | |
] | |
] | |
]; | |
/** | |
* Get only the parts of the array which appear after the last occurrence of a 'slot_type = 2' | |
* | |
* @return void | |
*/ | |
public function testGettingTheLastSection (): void | |
{ | |
$expected = [ | |
[ | |
'name' => 'Fifth', | |
'slot' => [ | |
'slot_type' => 1, | |
'type' => [ | |
'id' => 1 | |
] | |
] | |
], | |
[ | |
'name' => 'Sixth', | |
'slot' => [ | |
'slot_type' => 1, | |
'type' => [ | |
'id' => 1 | |
] | |
] | |
] | |
]; | |
$reversed = array_reverse($this->data); | |
$found = []; | |
foreach ($reversed as $part) { | |
if ($part['slot']['slot_type'] !== 2) { | |
$found[] = $part; | |
} elseif ($part['slot']['slot_type'] === 2) { | |
break; | |
} | |
} | |
$this->assertEquals($expected, array_reverse($found)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment