diff --git a/Tests/Unit/Provider/PhpcrMenuProviderTest.php b/Tests/Unit/Provider/PhpcrMenuProviderTest.php index 07d288ac..65f0f552 100644 --- a/Tests/Unit/Provider/PhpcrMenuProviderTest.php +++ b/Tests/Unit/Provider/PhpcrMenuProviderTest.php @@ -12,130 +12,76 @@ namespace Symfony\Cmf\Bundle\MenuBundle\Tests\Unit\Provider; use Symfony\Cmf\Bundle\MenuBundle\Provider\PhpcrMenuProvider; +use Doctrine\ODM\PHPCR\DocumentManagerInterface; +use Doctrine\Common\Persistence\ManagerRegistry; +use Knp\Menu\NodeInterface; +use Knp\Menu\ItemInterface; +use Knp\Menu\Loader\NodeLoader; +use Symfony\Component\HttpFoundation\Request; +use PHPCR\SessionInterface; +use Prophecy\Argument; class PhpcrMenuProviderTest extends \PHPUnit_Framework_Testcase { - private function getDmMock($path) + private $manager; + + public function setUp() { - $session = $this->getMock('PHPCR\SessionInterface'); - $session->expects($this->once()) - ->method('getNode') - ->with($path) - ; - $session->expects($this->once()) - ->method('getNamespacePrefixes') - ->will($this->returnValue(array('jcr', 'nt'))) - ; - $dm = $this - ->getMockBuilder('Doctrine\ODM\PHPCR\DocumentManager') - ->disableOriginalConstructor() - ->getMock() - ; - $dm->expects($this->once()) - ->method('getPhpcrSession') - ->will($this->returnValue($session)) - ; - - return $dm; + $this->manager = $this->prophesize(DocumentManagerInterface::class); + $this->registry = $this->prophesize(ManagerRegistry::class); + $this->document = $this->prophesize(NodeInterface::class); + $this->item = $this->prophesize(ItemInterface::class); + $this->nodeLoader = $this->prophesize(NodeLoader::class); + $this->session = $this->prophesize(SessionInterface::class); + + $this->manager->getPhpcrSession()->willReturn($this->session->reveal()); + $this->registry->getManager(null)->willReturn($this->manager->reveal()); } /** - * @dataProvider getMenuTests + * @dataProvider provideMenuTests */ public function testGet($menuRoot, $name, $expectedPath) { - $objectManager = $this->getDmMock($expectedPath); - $objectManager->expects($this->once()) - ->method('find') - ->with($this->equalTo(null), $this->equalTo($expectedPath)) - ->will($this->returnValue($this->getMock('Knp\Menu\NodeInterface'))) - ; - - $managerRegistry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry'); - $managerRegistry->expects($this->once()) - ->method('getManager') - ->will($this->returnValue($objectManager)); - - $loader = $this->getMockBuilder('Knp\Menu\Loader\NodeLoader')->disableOriginalConstructor()->getMock(); - $loader->expects($this->once()) - ->method('load') - ->will($this->returnValue($this->getMock('Knp\Menu\ItemInterface'))); - - $provider = new PhpcrMenuProvider( - $loader, - $managerRegistry, - $menuRoot - ); + $this->manager->find(null, $expectedPath) + ->willReturn($this->document->reveal()); + $this->nodeLoader->load($this->document->reveal()) + ->willReturn($this->item->reveal()); - $provider->setRequest($this->getMock('Symfony\Component\HttpFoundation\Request')); + $provider = $this->createProvider($menuRoot); + $provider->setRequest(Request::create('/')); + $item = $provider->get($name); - $provider->get($name); + $this->assertSame($this->item->reveal(), $item); } /** - * @dataProvider getMenuTests + * @dataProvider provideMenuTests */ public function testHas($menuRoot, $name, $expectedPath) { - $objectManager = $this->getDmMock($expectedPath); - $objectManager->expects($this->once()) - ->method('find') - ->with($this->equalTo(null), $this->equalTo($expectedPath)) - ->will($this->returnValue($this->getMock('Knp\Menu\NodeInterface'))); - - $managerRegistry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry'); - $managerRegistry->expects($this->once()) - ->method('getManager') - ->will($this->returnValue($objectManager)); - - $provider = new PhpcrMenuProvider( - $this->getMockBuilder('Knp\Menu\Loader\NodeLoader')->disableOriginalConstructor()->getMock(), - $managerRegistry, - $menuRoot - ); + $this->manager->find(null, $expectedPath) + ->willReturn($this->document->reveal()); + $provider = $this->createProvider($menuRoot); $this->assertTrue($provider->has($name)); } public function testHasNot() { - $session = $this->getMock('PHPCR\SessionInterface'); - $session->expects($this->never()) - ->method('getNode') - ; - $session->expects($this->any()) - ->method('getNamespacePrefixes') - ->will($this->returnValue(array('jcr', 'nt'))) - ; - $objectManager = $this - ->getMockBuilder('Doctrine\ODM\PHPCR\DocumentManager') - ->disableOriginalConstructor() - ->getMock() - ; - $objectManager->expects($this->any()) - ->method('getPhpcrSession') - ->will($this->returnValue($session)) - ; - $objectManager->expects($this->never()) - ->method('find') - ; - - $managerRegistry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry'); - $managerRegistry->expects($this->any()) - ->method('getManager') - ->will($this->returnValue($objectManager)); - - $provider = new PhpcrMenuProvider( - $this->getMockBuilder('Knp\Menu\Loader\NodeLoader')->disableOriginalConstructor()->getMock(), - $managerRegistry, - '/foo' - ); + $this->session->getNode()->shouldNotBeCalled(); + $this->session->getNamespacePrefixes() + ->willReturn(array('jcr', 'nt')); + + $this->manager->find(Argument::cetera())->shouldNotBeCalled(); + + $provider = $this->createProvider('/foo'); $this->assertFalse($provider->has('notavalidnamespace:bar')); $this->assertFalse($provider->has('not:a:valid:name')); } - public function getMenuTests() + public function provideMenuTests() { return array( array('/test/menu', 'foo', '/test/menu/foo'), @@ -143,4 +89,13 @@ public function getMenuTests() array('/test/menu', 'jcr:namespaced', '/test/menu/jcr:namespaced'), ); } + + private function createProvider($basePath) + { + return new PhpcrMenuProvider( + $this->nodeLoader->reveal(), + $this->registry->reveal(), + $basePath + ); + } }