Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #256 from symfony-cmf/provider_test_prophecy
Browse files Browse the repository at this point in the history
Improved Provider Unit Test (use Prophecy)
  • Loading branch information
dbu committed May 23, 2016
2 parents 4957d5c + 39782c2 commit c605b26
Showing 1 changed file with 50 additions and 95 deletions.
145 changes: 50 additions & 95 deletions Tests/Unit/Provider/PhpcrMenuProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,135 +12,90 @@
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'),
array('/test/menu', '/another/menu/path', '/another/menu/path'),
array('/test/menu', 'jcr:namespaced', '/test/menu/jcr:namespaced'),
);
}

private function createProvider($basePath)
{
return new PhpcrMenuProvider(
$this->nodeLoader->reveal(),
$this->registry->reveal(),
$basePath
);
}
}

0 comments on commit c605b26

Please sign in to comment.