123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769 |
- <?php
- /**
- * Dot - PHP dot notation access to arrays
- *
- * @author Riku Särkinen <riku@adbar.io>
- * @link https://github.com/adbario/php-dot-notation
- * @license https://github.com/adbario/php-dot-notation/blob/2.x/LICENSE.md (MIT License)
- */
- namespace Adbar\Tests;
- use Adbar\Dot;
- use ArrayIterator;
- use PHPUnit\Framework\TestCase;
- class DotTest extends TestCase
- {
- /*
- * --------------------------------------------------------------
- * Construct
- * --------------------------------------------------------------
- */
- public function testConstructWithoutValues()
- {
- $dot = new Dot;
- $this->assertSame([], $dot->all());
- }
- public function testConstructWithArray()
- {
- $dot = new Dot(['foo' => 'bar']);
- $this->assertEquals('bar', $dot->get('foo'));
- }
- public function testConstructWithString()
- {
- $dot = new Dot('foobar');
- $this->assertEquals('foobar', $dot->get(0));
- }
- public function testConstructWithDot()
- {
- $dot1 = new Dot(['foo' => 'bar']);
- $dot2 = new Dot($dot1);
- $this->assertEquals('bar', $dot2->get('foo'));
- }
- public function testConstructHelper()
- {
- $dot = dot(['foo' => 'bar']);
- $this->assertInstanceOf(Dot::class, $dot);
- $this->assertEquals('bar', $dot->get('foo'));
- }
- /*
- * --------------------------------------------------------------
- * Add
- * --------------------------------------------------------------
- */
- public function testAddKeyValuePair()
- {
- $dot = new Dot;
- $dot->add('foo.bar', 'baz');
- $this->assertEquals('baz', $dot->get('foo.bar'));
- }
- public function testAddKeyValuePairWithCustomDelimeter()
- {
- $dot = new Dot([], '/');
- $dot->add('foo/bar', 'baz');
- $this->assertEquals('baz', $dot->get('foo/bar'));
- }
- public function testAddValueToExistingKey()
- {
- $dot = new Dot(['foo' => 'bar']);
- $dot->add('foo', 'baz');
- $this->assertEquals('bar', $dot->get('foo'));
- }
- public function testAddArrayOfKeyValuePairs()
- {
- $dot = new Dot(['foobar' => 'baz']);
- $dot->add([
- 'foobar' => 'qux',
- 'corge' => 'grault'
- ]);
- $this->assertSame(['foobar' => 'baz', 'corge' => 'grault'], $dot->all());
- }
- /*
- * --------------------------------------------------------------
- * All
- * --------------------------------------------------------------
- */
- public function testAllReturnsAllItems()
- {
- $dot = new Dot(['foo' => 'bar']);
- $this->assertSame(['foo' => 'bar'], $dot->all());
- }
- /*
- * --------------------------------------------------------------
- * Clear
- * --------------------------------------------------------------
- */
- public function testClearKey()
- {
- $dot = new Dot(['foo' => ['bar' => 'baz']]);
- $dot->clear('foo.bar');
- $this->assertSame([], $dot->get('foo.bar'));
- }
- public function testClearKeyWithCustomDelimiter()
- {
- $dot = new Dot(['foo' => ['bar' => 'baz']], '/');
- $dot->clear('foo/bar');
- $this->assertSame([], $dot->get('foo/bar'));
- }
- public function testClearNonExistingKey()
- {
- $dot = new Dot;
- $dot->clear('foo');
- $this->assertSame([], $dot->get('foo'));
- }
- public function testClearArrayOfKeys()
- {
- $dot = new Dot(['foo' => 'bar', 'baz' => 'qux']);
- $dot->clear(['foo', 'baz']);
- $this->assertSame(['foo' => [], 'baz' => []], $dot->all());
- }
- public function testClearAll()
- {
- $dot = new Dot(['foo' => 'bar']);
- $dot->clear();
- $this->assertSame([], $dot->all());
- }
- /*
- * --------------------------------------------------------------
- * Delete
- * --------------------------------------------------------------
- */
- public function testDeleteKey()
- {
- $dot = new Dot(['foo' => ['bar' => 'baz']]);
- $dot->delete('foo.bar');
- $this->assertFalse($dot->has('foo.bar'));
- }
- public function testDeleteKeyWithCustomDelimeter()
- {
- $dot = new Dot(['foo' => ['bar' => 'baz']], '/');
- $dot->delete('foo/bar');
- $this->assertFalse($dot->has('foo/bar'));
- }
- public function testDeleteNonExistingKey()
- {
- $dot = new Dot(['foo' => 'bar']);
- $dot->delete('baz.qux');
- $this->assertSame(['foo' => 'bar'], $dot->all());
- }
- public function testDeleteArrayOfKeys()
- {
- $dot = new Dot(['foo' => 'bar', 'baz' => 'qux']);
- $dot->delete(['foo', 'baz']);
- $this->assertSame([], $dot->all());
- }
- /*
- * --------------------------------------------------------------
- * Flatten
- * --------------------------------------------------------------
- */
- public function testFlatten()
- {
- $dot = new Dot(['foo' => ['abc' => 'xyz', 'bar' => ['baz']]]);
- $flatten = $dot->flatten();
- $this->assertEquals('xyz', $flatten['foo.abc']);
- $this->assertEquals('baz', $flatten['foo.bar.0']);
- }
- public function testFlattenWithCustomDelimiter()
- {
- $dot = new Dot(['foo' => ['abc' => 'xyz', 'bar' => ['baz']]], '/');
- $flatten = $dot->flatten();
- $this->assertEquals('xyz', $flatten['foo/abc']);
- $this->assertEquals('baz', $flatten['foo/bar/0']);
- }
- public function testFlattenWithDoubleCustomDelimiter()
- {
- $dot = new Dot(['foo' => ['abc' => 'xyz', 'bar' => ['baz']]], '/');
- $flatten = $dot->flatten('_');
- $this->assertEquals('xyz', $flatten['foo_abc']);
- $this->assertEquals('baz', $flatten['foo_bar_0']);
- }
- /*
- * --------------------------------------------------------------
- * Get
- * --------------------------------------------------------------
- */
- public function testGetValueFromKey()
- {
- $dot = new Dot(['foo' => ['bar' => 'baz']]);
- $this->assertEquals('baz', $dot->get('foo.bar'));
- }
- public function testGetValueFromNonExistingKey()
- {
- $dot = new Dot;
- $this->assertNull($dot->get('foo'));
- }
- public function testGetGivenDefaultValueFromNonExistingKey()
- {
- $dot = new Dot;
- $this->assertEquals('bar', $dot->get('foo', 'bar'));
- }
- /*
- * --------------------------------------------------------------
- * Has
- * --------------------------------------------------------------
- */
- public function testHasKey()
- {
- $dot = new Dot(['foo' => ['bar' => 'baz']]);
- $this->assertTrue($dot->has('foo.bar'));
- $dot->delete('foo.bar');
- $this->assertFalse($dot->has('foo.bar'));
- }
- public function testHasArrayOfKeys()
- {
- $dot = new Dot(['foo' => 'bar', 'baz' => 'qux']);
- $this->assertTrue($dot->has(['foo', 'baz']));
- $dot->delete('foo');
- $this->assertFalse($dot->has(['foo', 'baz']));
- }
- public function testHasWithEmptyDot()
- {
- $dot = new Dot;
- $this->assertFalse($dot->has('foo'));
- }
- /*
- * --------------------------------------------------------------
- * Is empty
- * --------------------------------------------------------------
- */
- public function testIsEmptyDot()
- {
- $dot = new Dot;
- $this->assertTrue($dot->isEmpty());
- $dot->set('foo', 'bar');
- $this->assertFalse($dot->isEmpty());
- }
- public function testIsEmptyKey()
- {
- $dot = new Dot;
- $this->assertTrue($dot->isEmpty('foo.bar'));
- $dot->set('foo.bar', 'baz');
- $this->assertFalse($dot->isEmpty('foo.bar'));
- }
- public function testIsEmptyArrayOfKeys()
- {
- $dot = new Dot;
- $this->assertTrue($dot->isEmpty(['foo', 'bar']));
- $dot->set('foo', 'baz');
- $this->assertFalse($dot->isEmpty(['foo', 'bar']));
- }
- /*
- * --------------------------------------------------------------
- * Merge
- * --------------------------------------------------------------
- */
- public function testMergeArrayWithDot()
- {
- $dot = new Dot(['foo' => ['bar' => 'baz']]);
- $dot->merge(['foo' => ['bar' => 'qux']]);
- $this->assertEquals('qux', $dot->get('foo.bar'));
- }
- public function testMergeArrayWithKey()
- {
- $dot = new Dot(['foo' => ['bar' => 'baz']]);
- $dot->merge('foo', ['bar' => 'qux']);
- $this->assertEquals('qux', $dot->get('foo.bar'));
- }
- public function testMergeDotWithDot()
- {
- $dot1 = new Dot(['foo' => ['bar' => 'baz']]);
- $dot2 = new Dot(['foo' => ['bar' => 'qux']]);
- $dot1->merge($dot2);
- $this->assertEquals('qux', $dot1->get('foo.bar'));
- }
- public function testMergeDotObjectWithKey()
- {
- $dot1 = new Dot(['foo' => ['bar' => 'baz']]);
- $dot2 = new Dot(['bar' => 'qux']);
- $dot1->merge('foo', $dot2);
- $this->assertEquals('qux', $dot1->get('foo.bar'));
- }
- /*
- * --------------------------------------------------------------
- * Recursive merge
- * --------------------------------------------------------------
- */
- public function testRecursiveMergeArrayWithDot()
- {
- $dot = new Dot(['foo' => ['bar' => 'baz']]);
- $dot->mergeRecursive(['foo' => ['bar' => 'qux', 'quux' => 'quuz']]);
- $this->assertEquals(['baz', 'qux'], $dot->get('foo.bar'));
- $this->assertEquals('quuz', $dot->get('foo.quux'));
- }
- public function testRecursiveMergeArrayWithKey()
- {
- $dot = new Dot(['foo' => ['bar' => 'baz']]);
- $dot->mergeRecursive('foo', ['bar' => 'qux', 'quux' => 'quuz']);
- $this->assertEquals(['baz', 'qux'], $dot->get('foo.bar'));
- $this->assertEquals('quuz', $dot->get('foo.quux'));
- }
- public function testRecursiveMergeDotWithDot()
- {
- $dot1 = new Dot(['foo' => ['bar' => 'baz']]);
- $dot2 = new Dot(['foo' => ['bar' => 'qux', 'quux' => 'quuz']]);
- $dot1->mergeRecursive($dot2);
- $this->assertEquals(['baz', 'qux'], $dot1->get('foo.bar'));
- $this->assertEquals('quuz', $dot1->get('foo.quux'));
- }
- public function testRecursiveMergeDotObjectWithKey()
- {
- $dot1 = new Dot(['foo' => ['bar' => 'baz']]);
- $dot2 = new Dot(['bar' => 'qux', 'quux' => 'quuz']);
- $dot1->mergeRecursive('foo', $dot2);
- $this->assertEquals(['baz', 'qux'], $dot1->get('foo.bar'));
- $this->assertEquals('quuz', $dot1->get('foo.quux'));
- }
- /*
- * --------------------------------------------------------------
- * Recursive distinct merge
- * --------------------------------------------------------------
- */
- public function testRecursiveDistinctMergeArrayWithDot()
- {
- $dot = new Dot(['foo' => ['bar' => 'baz']]);
- $dot->mergeRecursiveDistinct(['foo' => ['bar' => 'qux', 'quux' => 'quuz']]);
- $this->assertEquals('qux', $dot->get('foo.bar'));
- $this->assertEquals('quuz', $dot->get('foo.quux'));
- }
- public function testRecursiveDistinctMergeArrayWithKey()
- {
- $dot = new Dot(['foo' => ['bar' => 'baz']]);
- $dot->mergeRecursiveDistinct('foo', ['bar' => 'qux', 'quux' => 'quuz']);
- $this->assertEquals('qux', $dot->get('foo.bar'));
- $this->assertEquals('quuz', $dot->get('foo.quux'));
- }
- public function testRecursiveDistinctMergeDotWithDot()
- {
- $dot1 = new Dot(['foo' => ['bar' => 'baz']]);
- $dot2 = new Dot(['foo' => ['bar' => 'qux', 'quux' => 'quuz']]);
- $dot1->mergeRecursiveDistinct($dot2);
- $this->assertEquals('qux', $dot1->get('foo.bar'));
- $this->assertEquals('quuz', $dot1->get('foo.quux'));
- }
- public function testRecursiveDistinctMergeDotObjectWithKey()
- {
- $dot1 = new Dot(['foo' => ['bar' => 'baz']]);
- $dot2 = new Dot(['bar' => 'qux', 'quux' => 'quuz']);
- $dot1->mergeRecursiveDistinct('foo', $dot2);
- $this->assertEquals('qux', $dot1->get('foo.bar'));
- $this->assertEquals('quuz', $dot1->get('foo.quux'));
- }
- /*
- * --------------------------------------------------------------
- * Pull
- * --------------------------------------------------------------
- */
- public function testPullKey()
- {
- $dot = new Dot(['foo' => 'bar']);
- $this->assertEquals('bar', $dot->pull('foo'));
- $this->assertFalse($dot->has('foo'));
- }
- public function testPullNonExistingKey()
- {
- $dot = new Dot;
- $this->assertNull($dot->pull('foo'));
- }
- public function testPullNonExistingKeyWithDefaultValue()
- {
- $dot = new Dot;
- $this->assertEquals('bar', $dot->pull('foo', 'bar'));
- }
- public function testPullAll()
- {
- $dot = new Dot(['foo' => 'bar']);
- $this->assertSame(['foo' => 'bar'], $dot->pull());
- $this->assertSame([], $dot->all());
- }
- /*
- * --------------------------------------------------------------
- * Push
- * --------------------------------------------------------------
- */
- public function testPushValue()
- {
- $dot = new Dot;
- $dot->push('foo');
- $this->assertEquals('foo', $dot->get(0));
- }
- public function testPushValueToKey()
- {
- $dot = new Dot(['foo' => [0 => 'bar']]);
- $dot->push('foo', 'baz');
- $this->assertSame(['bar', 'baz'], $dot->get('foo'));
- }
- /*
- * --------------------------------------------------------------
- * Replace
- * --------------------------------------------------------------
- */
- public function testReplaceWithArray()
- {
- $dot = new Dot(['foo' => ['bar' => 'baz']]);
- $dot->replace(['foo' => ['qux' => 'quux']]);
- $this->assertEquals(['qux' => 'quux'], $dot->get('foo'));
- }
- public function testReplaceKeyWithArray()
- {
- $dot = new Dot(['foo' => ['bar' => 'baz', 'qux' => 'quux']]);
- $dot->replace('foo', ['qux' => 'corge']);
- $this->assertEquals(['bar' => 'baz', 'qux' => 'corge'], $dot->get('foo'));
- }
- public function testReplaceWithDot()
- {
- $dot1 = new Dot(['foo' => ['bar' => 'baz']]);
- $dot2 = new Dot(['foo' => ['bar' => 'qux']]);
- $dot1->replace($dot2);
- $this->assertEquals(['bar' => 'qux'], $dot1->get('foo'));
- }
- public function testReplaceKeyWithDot()
- {
- $dot1 = new Dot(['foo' => ['bar' => 'baz', 'qux' => 'quux']]);
- $dot2 = new Dot(['qux' => 'corge']);
- $dot1->merge('foo', $dot2);
- $this->assertEquals(['bar' => 'baz', 'qux' => 'corge'], $dot1->get('foo'));
- }
- /*
- * --------------------------------------------------------------
- * Set
- * --------------------------------------------------------------
- */
- public function testSetKeyValuePair()
- {
- $dot = new Dot;
- $dot->set('foo.bar', 'baz');
- $this->assertEquals('baz', $dot->get('foo.bar'));
- }
- public function testSetKeyValuePairWithCustomDelimiter()
- {
- $dot = new Dot([], '/');
- $dot->set('foo/bar', 'baz');
- $this->assertEquals('baz', $dot->get('foo/bar'));
- }
- public function testSetArrayOfKeyValuePairs()
- {
- $dot = new Dot;
- $dot->set(['foo' => 'bar', 'baz' => 'qux']);
- $this->assertSame(['foo' => 'bar', 'baz' => 'qux'], $dot->all());
- }
- /*
- * --------------------------------------------------------------
- * Set array
- * --------------------------------------------------------------
- */
- public function testSetArray()
- {
- $dot = new Dot;
- $dot->setArray(['foo' => 'bar']);
- $this->assertSame(['foo' => 'bar'], $dot->all());
- }
- /*
- * --------------------------------------------------------------
- * Set reference
- * --------------------------------------------------------------
- */
- public function testSetReference()
- {
- $dot = new Dot;
- $items = ['foo' => 'bar'];
- $dot->setReference($items);
- $dot->set('foo', 'baz');
- $this->assertEquals('baz', $items['foo']);
- }
- /*
- * --------------------------------------------------------------
- * ArrayAccess interface
- * --------------------------------------------------------------
- */
- public function testOffsetExists()
- {
- $dot = new Dot(['foo' => 'bar']);
- $this->assertTrue(isset($dot['foo']));
- unset($dot['foo']);
- $this->assertFalse(isset($dot['foo']));
- }
- public function testOffsetGet()
- {
- $dot = new Dot(['foo' => 'bar']);
- $this->assertEquals('bar', $dot['foo']);
- }
- public function testOffsetSet()
- {
- $dot = new Dot;
- $dot['foo.bar'] = 'baz';
- $this->assertEquals('baz', $dot['foo.bar']);
- }
- public function testOffsetSetWithoutKey()
- {
- $dot = new Dot;
- $dot[] = 'foobar';
- $this->assertEquals('foobar', $dot->get(0));
- }
- public function testOffsetUnset()
- {
- $dot = new Dot(['foo' => 'bar']);
- unset($dot['foo']);
- $this->assertFalse(isset($dot['foo']));
- }
- /*
- * --------------------------------------------------------------
- * To JSON
- * --------------------------------------------------------------
- */
- public function testToJsonAll()
- {
- $dot = new Dot(['foo' => 'bar']);
- $this->assertJsonStringEqualsJsonString(
- json_encode(['foo' => 'bar']),
- $dot->toJson()
- );
- }
- public function testToJsonAllWithOption()
- {
- $dot = new Dot(['foo' => "'bar'"]);
- $this->assertJsonStringEqualsJsonString(
- json_encode(['foo' => "'bar'"], JSON_HEX_APOS),
- $dot->toJson(JSON_HEX_APOS)
- );
- }
- public function testToJsonKey()
- {
- $dot = new Dot(['foo' => ['bar' => 'value']]);
- $this->assertJsonStringEqualsJsonString(
- json_encode(['bar' => "value"]),
- $dot->toJson('foo')
- );
- }
- public function testToJsonKeyWithOptions()
- {
- $dot = new Dot(['foo' => ['bar' => "'value'"]]);
- $this->assertEquals(
- json_encode(['bar' => "'value'"], JSON_HEX_APOS),
- $dot->toJson('foo', JSON_HEX_APOS)
- );
- }
- /*
- * --------------------------------------------------------------
- * Countable interface
- * --------------------------------------------------------------
- */
- public function testCount()
- {
- $dot = new Dot([1, 2, 3]);
- $this->assertEquals(3, $dot->count());
- }
- public function testCountable()
- {
- $dot = new Dot([1, 2, 3]);
- $this->assertCount(3, $dot);
- }
- /*
- * --------------------------------------------------------------
- * IteratorAggregate interface
- * --------------------------------------------------------------
- */
- public function testGetIteratorReturnsArrayIterator()
- {
- $dot = new Dot;
- $this->assertInstanceOf(ArrayIterator::class, $dot->getIterator());
- }
- public function testIterationReturnsOriginalValues()
- {
- $dot = new Dot([1, 2, 3]);
- foreach ($dot as $item) {
- $items[] = $item;
- }
- $this->assertSame([1, 2, 3], $items);
- }
- /*
- * --------------------------------------------------------------
- * JsonSerializable interface
- * --------------------------------------------------------------
- */
- public function testJsonEncodingReturnsJson()
- {
- $dot = new Dot(['foo' => 'bar']);
- $this->assertJsonStringEqualsJsonString(
- json_encode(['foo' => 'bar']),
- json_encode($dot)
- );
- }
- }
|