flatrr/tests/FlatArrayPushPopTest.php

67 lines
2.1 KiB
PHP
Raw Normal View History

2018-08-17 21:02:50 +00:00
<?php
/* Flatrr | https://github.com/jobyone/flatrr | MIT License */
2022-12-06 01:34:57 +00:00
2018-08-17 21:02:50 +00:00
declare(strict_types=1);
2022-12-06 01:34:57 +00:00
2018-08-17 22:09:45 +00:00
namespace Flatrr;
2018-08-17 21:02:50 +00:00
use PHPUnit\Framework\TestCase;
class FlatArrayPushPopTest extends TestCase
{
public function testPushPop()
{
$f = new FlatArray();
$f->push(null, 'foo');
$f->push(null, 'bar');
2022-12-06 01:34:57 +00:00
$this->assertEquals(['foo', 'bar'], $f->get());
2018-08-17 21:02:50 +00:00
$this->assertEquals('bar', $f->pop(null));
$this->assertEquals(['foo'], $f->get());
$this->assertEquals('foo', $f->pop(null));
$this->assertEquals([], $f->get());
$this->assertNull($f->pop(null));
2018-08-17 21:02:50 +00:00
}
2022-12-06 01:34:57 +00:00
public function testPushIndexCreation()
{
// pushing to a nonexistent index creates it as an array
$f = new FlatArray();
$f->push('a.b', 'c');
$this->assertEquals(['c'], $f['a.b']);
$this->assertEquals(['a' => ['b' => ['c']]], $f->get());
// pushing to an existing non-array index does nothing
$f = new FlatArray(['a' => 'b']);
$f->push('a', 'c');
$this->assertEquals(['a' => 'b'], $f->get());
// poping off a non-array does nothing
$this->assertNull($f->pop('a'));
}
2018-08-17 21:02:50 +00:00
public function testShiftUnshift()
{
$f = new FlatArray();
2018-08-30 22:18:42 +00:00
$f->unshift(null, 'foo');
$f->unshift(null, 'bar');
2022-12-06 01:34:57 +00:00
$this->assertEquals(['bar', 'foo'], $f->get());
2018-08-30 22:18:42 +00:00
$this->assertEquals('bar', $f->shift(null));
2018-08-17 21:02:50 +00:00
$this->assertEquals(['foo'], $f->get());
2018-08-30 22:18:42 +00:00
$this->assertEquals('foo', $f->shift(null));
2018-08-17 21:02:50 +00:00
$this->assertEquals([], $f->get());
}
2022-12-06 01:34:57 +00:00
public function testUnshiftIndexCreation()
{
// unshifting to a nonexistent index creates it as an array
$f = new FlatArray();
$f->unshift('a.b', 'c');
$this->assertEquals(['c'], $f['a.b']);
$this->assertEquals(['a' => ['b' => ['c']]], $f->get());
// unshifting to an existing non-array index does nothing
$f = new FlatArray(['a' => 'b']);
$f->unshift('a', 'c');
$this->assertEquals(['a' => 'b'], $f->get());
// shifting off a non-array does nothing
$this->assertNull($f->shift('a'));
}
2018-08-17 21:02:50 +00:00
}