123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Form\Tests\Util;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\Form\Util\OrderedHashMap;
- /**
- * @author Bernhard Schussek <bschussek@gmail.com>
- */
- class OrderedHashMapTest extends TestCase
- {
- public function testGet()
- {
- $map = new OrderedHashMap();
- $map['first'] = 1;
- $this->assertSame(1, $map['first']);
- }
- /**
- * @expectedException \OutOfBoundsException
- */
- public function testGetNonExistingFails()
- {
- $map = new OrderedHashMap();
- $map['first'];
- }
- public function testInsertStringKeys()
- {
- $map = new OrderedHashMap();
- $map['first'] = 1;
- $map['second'] = 2;
- $this->assertSame(array('first' => 1, 'second' => 2), iterator_to_array($map));
- }
- public function testInsertNullKeys()
- {
- $map = new OrderedHashMap();
- $map[] = 1;
- $map['foo'] = 2;
- $map[] = 3;
- $this->assertSame(array(0 => 1, 'foo' => 2, 1 => 3), iterator_to_array($map));
- }
- public function testInsertLooselyEqualKeys()
- {
- $map = new OrderedHashMap();
- $map['1 as a string'] = '1 as a string';
- $map[1] = 1;
- $this->assertSame(array('1 as a string' => '1 as a string', 1 => 1), iterator_to_array($map));
- }
- /**
- * Updates should not change the position of an element, otherwise we could
- * turn foreach loops into endless loops if they change the current
- * element.
- *
- * foreach ($map as $index => $value) {
- * $map[$index] = $value + 1;
- * }
- *
- * And we don't want this, right? :)
- */
- public function testUpdateDoesNotChangeElementPosition()
- {
- $map = new OrderedHashMap();
- $map['first'] = 1;
- $map['second'] = 2;
- $map['first'] = 1;
- $this->assertSame(array('first' => 1, 'second' => 2), iterator_to_array($map));
- }
- public function testIsset()
- {
- $map = new OrderedHashMap();
- $map['first'] = 1;
- $this->assertArrayHasKey('first', $map);
- }
- public function testIssetReturnsFalseForNonExisting()
- {
- $map = new OrderedHashMap();
- $this->assertArrayNotHasKey('first', $map);
- }
- public function testIssetReturnsFalseForNull()
- {
- $map = new OrderedHashMap();
- $map['first'] = null;
- $this->assertArrayNotHasKey('first', $map);
- }
- public function testUnset()
- {
- $map = new OrderedHashMap();
- $map['first'] = 1;
- $map['second'] = 2;
- unset($map['first']);
- $this->assertSame(array('second' => 2), iterator_to_array($map));
- }
- public function testUnsetFromLooselyEqualKeysHashMap()
- {
- $map = new OrderedHashMap();
- $map['1 as a string'] = '1 as a string';
- $map[1] = 1;
- unset($map[1]);
- $this->assertSame(array('1 as a string' => '1 as a string'), iterator_to_array($map));
- }
- public function testUnsetNonExistingSucceeds()
- {
- $map = new OrderedHashMap();
- $map['second'] = 2;
- unset($map['first']);
- $this->assertSame(array('second' => 2), iterator_to_array($map));
- }
- public function testEmptyIteration()
- {
- $map = new OrderedHashMap();
- $it = $map->getIterator();
- $it->rewind();
- $this->assertFalse($it->valid());
- $this->assertNull($it->key());
- $this->assertNull($it->current());
- }
- public function testIterationSupportsInsertion()
- {
- $map = new OrderedHashMap(array('first' => 1));
- $it = $map->getIterator();
- $it->rewind();
- $this->assertTrue($it->valid());
- $this->assertSame('first', $it->key());
- $this->assertSame(1, $it->current());
- // dynamic modification
- $map['added'] = 2;
- // iterator is unchanged
- $this->assertTrue($it->valid());
- $this->assertSame('first', $it->key());
- $this->assertSame(1, $it->current());
- // continue iteration
- $it->next();
- $this->assertTrue($it->valid());
- $this->assertSame('added', $it->key());
- $this->assertSame(2, $it->current());
- // end of map
- $it->next();
- $this->assertFalse($it->valid());
- $this->assertNull($it->key());
- $this->assertNull($it->current());
- }
- public function testIterationSupportsDeletionAndInsertion()
- {
- $map = new OrderedHashMap(array('first' => 1, 'removed' => 2));
- $it = $map->getIterator();
- $it->rewind();
- $this->assertTrue($it->valid());
- $this->assertSame('first', $it->key());
- $this->assertSame(1, $it->current());
- // dynamic modification
- unset($map['removed']);
- $map['added'] = 3;
- // iterator is unchanged
- $this->assertTrue($it->valid());
- $this->assertSame('first', $it->key());
- $this->assertSame(1, $it->current());
- // continue iteration
- $it->next();
- $this->assertTrue($it->valid());
- $this->assertSame('added', $it->key());
- $this->assertSame(3, $it->current());
- // end of map
- $it->next();
- $this->assertFalse($it->valid());
- $this->assertNull($it->key());
- $this->assertNull($it->current());
- }
- public function testIterationSupportsDeletionOfCurrentElement()
- {
- $map = new OrderedHashMap(array('removed' => 1, 'next' => 2));
- $it = $map->getIterator();
- $it->rewind();
- $this->assertTrue($it->valid());
- $this->assertSame('removed', $it->key());
- $this->assertSame(1, $it->current());
- unset($map['removed']);
- // iterator is unchanged
- $this->assertTrue($it->valid());
- $this->assertSame('removed', $it->key());
- $this->assertSame(1, $it->current());
- // continue iteration
- $it->next();
- $this->assertTrue($it->valid());
- $this->assertSame('next', $it->key());
- $this->assertSame(2, $it->current());
- // end of map
- $it->next();
- $this->assertFalse($it->valid());
- $this->assertNull($it->key());
- $this->assertNull($it->current());
- }
- public function testIterationIgnoresReplacementOfCurrentElement()
- {
- $map = new OrderedHashMap(array('replaced' => 1, 'next' => 2));
- $it = $map->getIterator();
- $it->rewind();
- $this->assertTrue($it->valid());
- $this->assertSame('replaced', $it->key());
- $this->assertSame(1, $it->current());
- $map['replaced'] = 3;
- // iterator is unchanged
- $this->assertTrue($it->valid());
- $this->assertSame('replaced', $it->key());
- $this->assertSame(1, $it->current());
- // continue iteration
- $it->next();
- $this->assertTrue($it->valid());
- $this->assertSame('next', $it->key());
- $this->assertSame(2, $it->current());
- // end of map
- $it->next();
- $this->assertFalse($it->valid());
- $this->assertNull($it->key());
- $this->assertNull($it->current());
- }
- public function testIterationSupportsDeletionOfCurrentAndLastElement()
- {
- $map = new OrderedHashMap(array('removed' => 1));
- $it = $map->getIterator();
- $it->rewind();
- $this->assertTrue($it->valid());
- $this->assertSame('removed', $it->key());
- $this->assertSame(1, $it->current());
- unset($map['removed']);
- // iterator is unchanged
- $this->assertTrue($it->valid());
- $this->assertSame('removed', $it->key());
- $this->assertSame(1, $it->current());
- // end of map
- $it->next();
- $this->assertFalse($it->valid());
- $this->assertNull($it->key());
- $this->assertNull($it->current());
- }
- public function testIterationIgnoresReplacementOfCurrentAndLastElement()
- {
- $map = new OrderedHashMap(array('replaced' => 1));
- $it = $map->getIterator();
- $it->rewind();
- $this->assertTrue($it->valid());
- $this->assertSame('replaced', $it->key());
- $this->assertSame(1, $it->current());
- $map['replaced'] = 2;
- // iterator is unchanged
- $this->assertTrue($it->valid());
- $this->assertSame('replaced', $it->key());
- $this->assertSame(1, $it->current());
- // end of map
- $it->next();
- $this->assertFalse($it->valid());
- $this->assertNull($it->key());
- $this->assertNull($it->current());
- }
- public function testIterationSupportsDeletionOfPreviousElement()
- {
- $map = new OrderedHashMap(array('removed' => 1, 'next' => 2, 'onemore' => 3));
- $it = $map->getIterator();
- $it->rewind();
- $this->assertTrue($it->valid());
- $this->assertSame('removed', $it->key());
- $this->assertSame(1, $it->current());
- // continue iteration
- $it->next();
- $this->assertTrue($it->valid());
- $this->assertSame('next', $it->key());
- $this->assertSame(2, $it->current());
- unset($map['removed']);
- // iterator is unchanged
- $this->assertTrue($it->valid());
- $this->assertSame('next', $it->key());
- $this->assertSame(2, $it->current());
- // continue iteration
- $it->next();
- $this->assertTrue($it->valid());
- $this->assertSame('onemore', $it->key());
- $this->assertSame(3, $it->current());
- // end of map
- $it->next();
- $this->assertFalse($it->valid());
- $this->assertNull($it->key());
- $this->assertNull($it->current());
- }
- public function testIterationIgnoresReplacementOfPreviousElement()
- {
- $map = new OrderedHashMap(array('replaced' => 1, 'next' => 2, 'onemore' => 3));
- $it = $map->getIterator();
- $it->rewind();
- $this->assertTrue($it->valid());
- $this->assertSame('replaced', $it->key());
- $this->assertSame(1, $it->current());
- // continue iteration
- $it->next();
- $this->assertTrue($it->valid());
- $this->assertSame('next', $it->key());
- $this->assertSame(2, $it->current());
- $map['replaced'] = 4;
- // iterator is unchanged
- $this->assertTrue($it->valid());
- $this->assertSame('next', $it->key());
- $this->assertSame(2, $it->current());
- // continue iteration
- $it->next();
- $this->assertTrue($it->valid());
- $this->assertSame('onemore', $it->key());
- $this->assertSame(3, $it->current());
- // end of map
- $it->next();
- $this->assertFalse($it->valid());
- $this->assertNull($it->key());
- $this->assertNull($it->current());
- }
- public function testIterationSupportsDeletionOfMultiplePreviousElements()
- {
- $map = new OrderedHashMap(array('removed' => 1, 'alsoremoved' => 2, 'next' => 3, 'onemore' => 4));
- $it = $map->getIterator();
- $it->rewind();
- $this->assertTrue($it->valid());
- $this->assertSame('removed', $it->key());
- $this->assertSame(1, $it->current());
- // continue iteration
- $it->next();
- $this->assertTrue($it->valid());
- $this->assertSame('alsoremoved', $it->key());
- $this->assertSame(2, $it->current());
- // continue iteration
- $it->next();
- $this->assertTrue($it->valid());
- $this->assertSame('next', $it->key());
- $this->assertSame(3, $it->current());
- unset($map['removed'], $map['alsoremoved']);
- // iterator is unchanged
- $this->assertTrue($it->valid());
- $this->assertSame('next', $it->key());
- $this->assertSame(3, $it->current());
- // continue iteration
- $it->next();
- $this->assertTrue($it->valid());
- $this->assertSame('onemore', $it->key());
- $this->assertSame(4, $it->current());
- // end of map
- $it->next();
- $this->assertFalse($it->valid());
- $this->assertNull($it->key());
- $this->assertNull($it->current());
- }
- public function testParallelIteration()
- {
- $map = new OrderedHashMap(array('first' => 1, 'second' => 2));
- $it1 = $map->getIterator();
- $it2 = $map->getIterator();
- $it1->rewind();
- $this->assertTrue($it1->valid());
- $this->assertSame('first', $it1->key());
- $this->assertSame(1, $it1->current());
- $it2->rewind();
- $this->assertTrue($it2->valid());
- $this->assertSame('first', $it2->key());
- $this->assertSame(1, $it2->current());
- // 1: continue iteration
- $it1->next();
- $this->assertTrue($it1->valid());
- $this->assertSame('second', $it1->key());
- $this->assertSame(2, $it1->current());
- // 2: remains unchanged
- $this->assertTrue($it2->valid());
- $this->assertSame('first', $it2->key());
- $this->assertSame(1, $it2->current());
- // 1: advance to end of map
- $it1->next();
- $this->assertFalse($it1->valid());
- $this->assertNull($it1->key());
- $this->assertNull($it1->current());
- // 2: remains unchanged
- $this->assertTrue($it2->valid());
- $this->assertSame('first', $it2->key());
- $this->assertSame(1, $it2->current());
- // 2: continue iteration
- $it2->next();
- $this->assertTrue($it2->valid());
- $this->assertSame('second', $it2->key());
- $this->assertSame(2, $it2->current());
- // 1: remains unchanged
- $this->assertFalse($it1->valid());
- $this->assertNull($it1->key());
- $this->assertNull($it1->current());
- // 2: advance to end of map
- $it2->next();
- $this->assertFalse($it2->valid());
- $this->assertNull($it2->key());
- $this->assertNull($it2->current());
- // 1: remains unchanged
- $this->assertFalse($it1->valid());
- $this->assertNull($it1->key());
- $this->assertNull($it1->current());
- }
- public function testCount()
- {
- $map = new OrderedHashMap();
- $map[] = 1;
- $map['foo'] = 2;
- unset($map[0]);
- $map[] = 3;
- $this->assertCount(2, $map);
- }
- }
|