Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Wharenn committed Mar 26, 2015
1 parent 52046bd commit 15aaec9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function getConfigTreeBuilder()
->ifArray()
->then(function($items) {
if (isset($items['provider'])) {
$disallowedItems = array('items', 'label', 'label_catalogue');
$disallowedItems = array('items', 'label');
foreach($disallowedItems as $item) {
if (isset($items[$item])) {
throw new \InvalidArgumentException(sprintf('The config value "%s" cannot be used alongside "provider" config value', $item));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ public function testProcessParsingFullValidConfig()
$this->assertEquals(array('articleId' => 3) , $dashboardGroupsSettings['sonata_group_one']['items'][2]['route_params']);
$this->assertContains('sonata_news_admin', $dashboardGroupsSettings['sonata_group_one']['item_adds']);
$this->assertContains('ROLE_ONE', $dashboardGroupsSettings['sonata_group_one']['roles']);

$this->assertArrayHasKey('sonata_group_two', $dashboardGroupsSettings);
$this->assertArrayHasKey('provider', $dashboardGroupsSettings['sonata_group_two']);
$this->assertContains('my_menu', $dashboardGroupsSettings['sonata_group_two']['provider']);
}

/**
Expand Down Expand Up @@ -148,6 +152,10 @@ public function testProcessResultingConfig()
$this->assertFalse(in_array('sonata_article_admin', $adminGroups['sonata_group_one']['items']));
$this->assertContains('ROLE_ONE', $adminGroups['sonata_group_one']['roles']);

$this->assertArrayHasKey('sonata_group_two', $adminGroups);
$this->assertArrayHasKey('provider', $adminGroups['sonata_group_two']);
$this->assertContains('my_menu', $adminGroups['sonata_group_two']['provider']);

$this->assertArrayHasKey('Sonata\AdminBundle\Tests\DependencyInjection\Post', $adminClasses);
$this->assertContains('sonata_post_admin', $adminClasses['Sonata\AdminBundle\Tests\DependencyInjection\Post']);
$this->assertArrayHasKey('Sonata\AdminBundle\Tests\DependencyInjection\Article', $adminClasses);
Expand Down Expand Up @@ -286,6 +294,9 @@ protected function getConfig()
),
'roles' => array('ROLE_ONE'),
),
'sonata_group_two' => array(
'provider' => 'my_menu',
),
)
),
'admin_services' => array(
Expand Down
40 changes: 40 additions & 0 deletions Tests/Twig/Extension/SonataAdminExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Sonata\AdminBundle\Tests\Twig\Extension;

use Knp\Menu\MenuFactory;
use Knp\Menu\MenuItem;
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
use Sonata\AdminBundle\Admin\AdminInterface;
Expand Down Expand Up @@ -95,7 +97,13 @@ public function setUp()

$this->logger = $this->getMock('Psr\Log\LoggerInterface');

$menu = new MenuItem('bar', new MenuFactory());
$menu->addChild('foo');
$this->helper = $this->getMockBuilder('Knp\Menu\Twig\Helper')->disableOriginalConstructor()->getMock();
$this->helper->expects($this->any())
->method('get')
->with('my_menu')
->willReturn($menu);

$this->twigExtension = new SonataAdminExtension($this->pool, $this->router, $this->helper, $this->logger);

Expand Down Expand Up @@ -1041,4 +1049,36 @@ public function testGetKnpMenuWithNotGrantedList()
$this->assertArrayNotHasKey('bar', $menu->getChildren());
$this->assertCount(0, $menu->getChildren());
}

public function testGetKnpMenuWithProvider()
{
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');

$adminGroups = array(
"bar" => array(
"provider" => 'my_menu',
"label_catalogue" => '',
"icon" => '<i class="fa fa-edit"></i>',
"roles" => array(),
),
);
$this->pool->setAdminGroups($adminGroups);
$menu = $this->twigExtension->getKnpMenu($request);

$this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
$this->assertArrayHasKey('bar', $menu->getChildren());

foreach ($menu->getChildren() as $key => $child) {
$this->assertInstanceOf('Knp\Menu\MenuItem', $child);
$this->assertEquals("bar", $child->getName());
$this->assertEquals("bar", $child->getLabel());

// menu items
$children = $child->getChildren();
$this->assertCount(1, $children);
$this->assertArrayHasKey('foo', $children);
$this->assertInstanceOf('Knp\Menu\MenuItem', $child['foo']);
$this->assertEquals('foo', $child['foo']->getLabel());
}
}
}

0 comments on commit 15aaec9

Please sign in to comment.