|
12 | 12 | namespace Symfony\Cmf\Bundle\MenuBundle\Tests\Unit\Provider;
|
13 | 13 |
|
14 | 14 | use Symfony\Cmf\Bundle\MenuBundle\Provider\PhpcrMenuProvider;
|
| 15 | +use Doctrine\ODM\PHPCR\DocumentManagerInterface; |
| 16 | +use Doctrine\Common\Persistence\ManagerRegistry; |
| 17 | +use Knp\Menu\NodeInterface; |
| 18 | +use Knp\Menu\ItemInterface; |
| 19 | +use Knp\Menu\Loader\NodeLoader; |
| 20 | +use Symfony\Component\HttpFoundation\Request; |
| 21 | +use PHPCR\SessionInterface; |
| 22 | +use Prophecy\Argument; |
15 | 23 |
|
16 | 24 | class PhpcrMenuProviderTest extends \PHPUnit_Framework_Testcase
|
17 | 25 | {
|
18 |
| - private function getDmMock($path) |
| 26 | + private $manager; |
| 27 | + |
| 28 | + public function setUp() |
19 | 29 | {
|
20 |
| - $session = $this->getMock('PHPCR\SessionInterface'); |
21 |
| - $session->expects($this->once()) |
22 |
| - ->method('getNode') |
23 |
| - ->with($path) |
24 |
| - ; |
25 |
| - $session->expects($this->once()) |
26 |
| - ->method('getNamespacePrefixes') |
27 |
| - ->will($this->returnValue(array('jcr', 'nt'))) |
28 |
| - ; |
29 |
| - $dm = $this |
30 |
| - ->getMockBuilder('Doctrine\ODM\PHPCR\DocumentManager') |
31 |
| - ->disableOriginalConstructor() |
32 |
| - ->getMock() |
33 |
| - ; |
34 |
| - $dm->expects($this->once()) |
35 |
| - ->method('getPhpcrSession') |
36 |
| - ->will($this->returnValue($session)) |
37 |
| - ; |
38 |
| - |
39 |
| - return $dm; |
| 30 | + $this->manager = $this->prophesize(DocumentManagerInterface::class); |
| 31 | + $this->registry = $this->prophesize(ManagerRegistry::class); |
| 32 | + $this->document = $this->prophesize(NodeInterface::class); |
| 33 | + $this->item = $this->prophesize(ItemInterface::class); |
| 34 | + $this->nodeLoader = $this->prophesize(NodeLoader::class); |
| 35 | + $this->session = $this->prophesize(SessionInterface::class); |
| 36 | + |
| 37 | + $this->manager->getPhpcrSession()->willReturn($this->session->reveal()); |
| 38 | + $this->registry->getManager(null)->willReturn($this->manager->reveal()); |
40 | 39 | }
|
41 | 40 |
|
42 | 41 | /**
|
43 |
| - * @dataProvider getMenuTests |
| 42 | + * @dataProvider provideMenuTests |
44 | 43 | */
|
45 | 44 | public function testGet($menuRoot, $name, $expectedPath)
|
46 | 45 | {
|
47 |
| - $objectManager = $this->getDmMock($expectedPath); |
48 |
| - $objectManager->expects($this->once()) |
49 |
| - ->method('find') |
50 |
| - ->with($this->equalTo(null), $this->equalTo($expectedPath)) |
51 |
| - ->will($this->returnValue($this->getMock('Knp\Menu\NodeInterface'))) |
52 |
| - ; |
53 |
| - |
54 |
| - $managerRegistry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry'); |
55 |
| - $managerRegistry->expects($this->once()) |
56 |
| - ->method('getManager') |
57 |
| - ->will($this->returnValue($objectManager)); |
58 |
| - |
59 |
| - $loader = $this->getMockBuilder('Knp\Menu\Loader\NodeLoader')->disableOriginalConstructor()->getMock(); |
60 |
| - $loader->expects($this->once()) |
61 |
| - ->method('load') |
62 |
| - ->will($this->returnValue($this->getMock('Knp\Menu\ItemInterface'))); |
63 |
| - |
64 |
| - $provider = new PhpcrMenuProvider( |
65 |
| - $loader, |
66 |
| - $managerRegistry, |
67 |
| - $menuRoot |
68 |
| - ); |
| 46 | + $this->manager->find(null, $expectedPath) |
| 47 | + ->willReturn($this->document->reveal()); |
| 48 | + $this->nodeLoader->load($this->document->reveal()) |
| 49 | + ->willReturn($this->item->reveal()); |
69 | 50 |
|
70 |
| - $provider->setRequest($this->getMock('Symfony\Component\HttpFoundation\Request')); |
| 51 | + $provider = $this->createProvider($menuRoot); |
| 52 | + $provider->setRequest(Request::create('/')); |
| 53 | + $item = $provider->get($name); |
71 | 54 |
|
72 |
| - $provider->get($name); |
| 55 | + $this->assertSame($this->item->reveal(), $item); |
73 | 56 | }
|
74 | 57 |
|
75 | 58 | /**
|
76 |
| - * @dataProvider getMenuTests |
| 59 | + * @dataProvider provideMenuTests |
77 | 60 | */
|
78 | 61 | public function testHas($menuRoot, $name, $expectedPath)
|
79 | 62 | {
|
80 |
| - $objectManager = $this->getDmMock($expectedPath); |
81 |
| - $objectManager->expects($this->once()) |
82 |
| - ->method('find') |
83 |
| - ->with($this->equalTo(null), $this->equalTo($expectedPath)) |
84 |
| - ->will($this->returnValue($this->getMock('Knp\Menu\NodeInterface'))); |
85 |
| - |
86 |
| - $managerRegistry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry'); |
87 |
| - $managerRegistry->expects($this->once()) |
88 |
| - ->method('getManager') |
89 |
| - ->will($this->returnValue($objectManager)); |
90 |
| - |
91 |
| - $provider = new PhpcrMenuProvider( |
92 |
| - $this->getMockBuilder('Knp\Menu\Loader\NodeLoader')->disableOriginalConstructor()->getMock(), |
93 |
| - $managerRegistry, |
94 |
| - $menuRoot |
95 |
| - ); |
| 63 | + $this->manager->find(null, $expectedPath) |
| 64 | + ->willReturn($this->document->reveal()); |
96 | 65 |
|
| 66 | + $provider = $this->createProvider($menuRoot); |
97 | 67 | $this->assertTrue($provider->has($name));
|
98 | 68 | }
|
99 | 69 |
|
100 | 70 | public function testHasNot()
|
101 | 71 | {
|
102 |
| - $session = $this->getMock('PHPCR\SessionInterface'); |
103 |
| - $session->expects($this->never()) |
104 |
| - ->method('getNode') |
105 |
| - ; |
106 |
| - $session->expects($this->any()) |
107 |
| - ->method('getNamespacePrefixes') |
108 |
| - ->will($this->returnValue(array('jcr', 'nt'))) |
109 |
| - ; |
110 |
| - $objectManager = $this |
111 |
| - ->getMockBuilder('Doctrine\ODM\PHPCR\DocumentManager') |
112 |
| - ->disableOriginalConstructor() |
113 |
| - ->getMock() |
114 |
| - ; |
115 |
| - $objectManager->expects($this->any()) |
116 |
| - ->method('getPhpcrSession') |
117 |
| - ->will($this->returnValue($session)) |
118 |
| - ; |
119 |
| - $objectManager->expects($this->never()) |
120 |
| - ->method('find') |
121 |
| - ; |
122 |
| - |
123 |
| - $managerRegistry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry'); |
124 |
| - $managerRegistry->expects($this->any()) |
125 |
| - ->method('getManager') |
126 |
| - ->will($this->returnValue($objectManager)); |
127 |
| - |
128 |
| - $provider = new PhpcrMenuProvider( |
129 |
| - $this->getMockBuilder('Knp\Menu\Loader\NodeLoader')->disableOriginalConstructor()->getMock(), |
130 |
| - $managerRegistry, |
131 |
| - '/foo' |
132 |
| - ); |
| 72 | + $this->session->getNode()->shouldNotBeCalled(); |
| 73 | + $this->session->getNamespacePrefixes() |
| 74 | + ->willReturn(array('jcr', 'nt')); |
| 75 | + |
| 76 | + $this->manager->find(Argument::cetera())->shouldNotBeCalled(); |
| 77 | + |
| 78 | + $provider = $this->createProvider('/foo'); |
133 | 79 |
|
134 | 80 | $this->assertFalse($provider->has('notavalidnamespace:bar'));
|
135 | 81 | $this->assertFalse($provider->has('not:a:valid:name'));
|
136 | 82 | }
|
137 | 83 |
|
138 |
| - public function getMenuTests() |
| 84 | + public function provideMenuTests() |
139 | 85 | {
|
140 | 86 | return array(
|
141 | 87 | array('/test/menu', 'foo', '/test/menu/foo'),
|
142 | 88 | array('/test/menu', '/another/menu/path', '/another/menu/path'),
|
143 | 89 | array('/test/menu', 'jcr:namespaced', '/test/menu/jcr:namespaced'),
|
144 | 90 | );
|
145 | 91 | }
|
| 92 | + |
| 93 | + private function createProvider($basePath) |
| 94 | + { |
| 95 | + return new PhpcrMenuProvider( |
| 96 | + $this->nodeLoader->reveal(), |
| 97 | + $this->registry->reveal(), |
| 98 | + $basePath |
| 99 | + ); |
| 100 | + } |
146 | 101 | }
|
0 commit comments