Skip to content

Instantly share code, notes, and snippets.

@davidyell
Created April 20, 2020 14:53
Show Gist options
  • Save davidyell/3cabd7aabd0e4919299a9ba451afe4ea to your computer and use it in GitHub Desktop.
Save davidyell/3cabd7aabd0e4919299a9ba451afe4ea to your computer and use it in GitHub Desktop.
<?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