Permalink
Browse files

Merge pull request #118 from Heliopsis-HQ/dont-override-extras

Dont override extras
  • Loading branch information...
2 parents 9f4235f + e5c6546 commit 835ee51d911e4d8c9adf129907ebbebfa9a1e906 @Nek- Nek- committed Feb 3, 2014
Showing with 111 additions and 1 deletion.
  1. +18 −1 src/Knp/Menu/Factory/CoreExtension.php
  2. +93 −0 tests/Knp/Menu/Tests/Factory/CoreExtensionTest.php
@@ -50,10 +50,27 @@ public function buildItem(ItemInterface $item, array $options)
->setLinkAttributes($options['linkAttributes'])
->setChildrenAttributes($options['childrenAttributes'])
->setLabelAttributes($options['labelAttributes'])
- ->setExtras($options['extras'])
->setCurrent($options['current'])
->setDisplay($options['display'])
->setDisplayChildren($options['displayChildren'])
;
+
+ $this->buildExtras($item, $options);
+ }
+
+ /**
+ * Configures the newly created item's extras
+ * Extras are processed one by one in order not to reset values set by other extensions
+ *
+ * @param ItemInterface $item
+ * @param array $options
+ */
+ private function buildExtras(ItemInterface $item, array $options)
+ {
+ if (!empty($options['extras'])) {
+ foreach ($options['extras'] as $key => $value) {
+ $item->setExtra($key, $value);
+ }
+ }
}
}
@@ -0,0 +1,93 @@
+<?php
+/**
+ * @author: bchoquet
+ */
+
+namespace Knp\Menu\Tests\Factory;
+
+
+use Knp\Menu\Factory\CoreExtension;
+use Knp\Menu\MenuFactory;
+use Knp\Menu\MenuItem;
+
+class CoreExtensionTest extends \PHPUnit_Framework_TestCase
+{
+ public function testBuildOptions()
+ {
+ $extension = $this->getExtension();
+ $item = $this->createItem( 'test' );
+
+ $options = $extension->buildOptions( array() );
+
+ $this->assertArrayHasKey( 'uri', $options );
+ $this->assertArrayHasKey( 'label', $options );
+ $this->assertArrayHasKey( 'attributes', $options );
+ $this->assertArrayHasKey( 'linkAttributes', $options );
+ $this->assertArrayHasKey( 'childrenAttributes', $options );
+ $this->assertArrayHasKey( 'labelAttributes', $options );
+ $this->assertArrayHasKey( 'extras', $options );
+ $this->assertArrayHasKey( 'current', $options );
+ $this->assertArrayHasKey( 'display', $options );
+ $this->assertArrayHasKey( 'displayChildren', $options );
+ }
+
+ public function testBuildItemsSetsExtras()
+ {
+ $item = $this->createItem( 'test' );
+ $item->setExtra( 'test1', 'original value' );
+ $extension = $this->getExtension();
+ $options = $extension->buildOptions(
+ array(
+ 'extras' => array(
+ 'test1' => 'options value 1',
+ 'test2' => 'options value 2',
+ )
+ )
+ );
+
+ $extension->buildItem( $item, $options );
+
+ $extras = $item->getExtras();
+
+ $this->assertEquals( 2, count( $extras ) );
+
+ $this->assertArrayHasKey( 'test1', $extras );
+ $this->assertEquals( 'options value 1', $item->getExtra( 'test1' ) );
+
+ $this->assertArrayHasKey( 'test2', $extras );
+ $this->assertEquals( 'options value 2', $item->getExtra( 'test2' ) );
+ }
+
+ public function testBuildItemDoesNotOverrideExistingExtras()
+ {
+ $item = $this->createItem( 'test' );
+ $item->setExtra( 'test1', 'original value' );
+ $extension = $this->getExtension();
+ $options = $extension->buildOptions(
+ array(
+ 'extras' => array(
+ 'test2' => 'options value',
+ )
+ )
+ );
+
+ $extension->buildItem( $item, $options );
+
+ $this->assertArrayHasKey( 'test1', $item->getExtras() );
+ $this->assertEquals( 'original value', $item->getExtra( 'test1' ) );
+ }
+
+ private function getExtension()
+ {
+ return new CoreExtension();
+ }
+
+ private function createItem( $name )
+ {
+ $factory = new MenuFactory();
+ $item = new MenuItem( $name, $factory );
+
+ return $item;
+ }
+}
+

0 comments on commit 835ee51

Please sign in to comment.