Skip to content

Commit db742c2

Browse files
author
Lars Janssen
committed
Tests: Moved mock objects 'expects()' calls inline to improve readability
1 parent 852b2a9 commit db742c2

File tree

3 files changed

+175
-133
lines changed

3 files changed

+175
-133
lines changed

Tests/BaseTestCase.php

Lines changed: 0 additions & 64 deletions
This file was deleted.

Tests/Document/ContentRepositoryTest.php

Lines changed: 84 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,54 @@
33
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Document;
44

55
use Symfony\Cmf\Bundle\RoutingBundle\Document\ContentRepository;
6-
use Symfony\Cmf\Bundle\RoutingBundle\Tests\BaseTestCase;
76

8-
class ContentRepositoryTest extends BaseTestCase
7+
class ContentRepositoryTest extends \PHPUnit_Framework_Testcase
98
{
9+
/** @var \PHPUnit_Framework_MockObject_MockObject */
10+
private $document;
11+
12+
/** @var \PHPUnit_Framework_MockObject_MockObject */
13+
private $document2;
14+
15+
/** @var \PHPUnit_Framework_MockObject_MockObject */
16+
private $objectManager;
17+
18+
/** @var \PHPUnit_Framework_MockObject_MockObject */
19+
private $objectManager2;
20+
21+
/** @var \PHPUnit_Framework_MockObject_MockObject */
22+
private $managerRegistry;
23+
24+
public function setUp()
25+
{
26+
$this->document = $this->getMock('Symfony\Cmf\Bundle\RoutingBundle\Tests\Document\TestDocument');
27+
$this->document2 = $this->getMock('Symfony\Cmf\Bundle\RoutingBundle\Tests\Document\TestDocument');
28+
$this->objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
29+
$this->objectManager2 = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
30+
$this->managerRegistry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
31+
}
32+
1033
public function testFindById()
1134
{
12-
$managerRegistry = $this->getManagerRegistry(
13-
array(
14-
'default' => $this->getObjectManager(
15-
array('id-123' => $this->getDocument('/cms/my-document'))
16-
)
17-
)
18-
);
35+
$this->document
36+
->expects($this->any())
37+
->method('getPath')
38+
->will($this->returnValue('/cms/my-document'))
39+
;
1940

20-
$contentRepository = new ContentRepository($managerRegistry);
41+
$this->objectManager
42+
->expects($this->any())
43+
->method('find')
44+
->will($this->returnValue($this->document))
45+
;
46+
47+
$this->managerRegistry
48+
->expects($this->any())
49+
->method('getManager')
50+
->will($this->returnValue($this->objectManager))
51+
;
52+
53+
$contentRepository = new ContentRepository($this->managerRegistry);
2154
$contentRepository->setManagerName('default');
2255

2356
$foundDocument = $contentRepository->findById('id-123');
@@ -37,17 +70,48 @@ public function testGetContentId()
3770
*/
3871
public function testChangingDocumentManager()
3972
{
40-
$managerRegistry = $this->getManagerRegistry(
41-
array(
42-
'default' => $this->getObjectManager(
43-
array('id-123' => $this->getDocument('/cms/my-document'))
44-
),
45-
'new_manager' => $this->getObjectManager(
46-
array('id-123' => $this->getDocument('/cms/new-document'))
47-
)
48-
)
73+
$this->document
74+
->expects($this->any())
75+
->method('getPath')
76+
->will($this->returnValue('/cms/my-document'))
77+
;
78+
79+
$this->document2
80+
->expects($this->any())
81+
->method('getPath')
82+
->will($this->returnValue('/cms/new-document'))
83+
;
84+
85+
$this->objectManager
86+
->expects($this->any())
87+
->method('find')
88+
->with(null, 'id-123')
89+
->will($this->returnValue($this->document))
90+
;
91+
92+
$this->objectManager2
93+
->expects($this->any())
94+
->method('find')
95+
->with(null, 'id-123')
96+
->will($this->returnValue($this->document2))
97+
;
98+
99+
$objectManagers = array(
100+
'default' => $this->objectManager,
101+
'new_manager' => $this->objectManager2
49102
);
50-
$contentRepository = new ContentRepository($managerRegistry);
103+
$this->managerRegistry
104+
->expects($this->any())
105+
->method('getManager')
106+
->will(
107+
$this->returnCallback(
108+
function ($name) use ($objectManagers) {
109+
return $objectManagers[$name];
110+
}
111+
)
112+
);
113+
114+
$contentRepository = new ContentRepository($this->managerRegistry);
51115

52116
$contentRepository->setManagerName('default');
53117
$foundDocument = $contentRepository->findById('id-123');
@@ -59,18 +123,4 @@ public function testChangingDocumentManager()
59123
$this->assertInstanceOf('Symfony\Cmf\Bundle\RoutingBundle\Tests\Document\TestDocument', $newFoundDocument);
60124
$this->assertEquals('/cms/new-document', $newFoundDocument->getPath());
61125
}
62-
63-
/**
64-
* Get a mock document that returns the supplied $path for getPath().
65-
*
66-
* @param $path
67-
* @return \PHPUnit_Framework_MockObject_MockObject
68-
*/
69-
private function getDocument($path)
70-
{
71-
$document = $this->getMock('Symfony\Cmf\Bundle\RoutingBundle\Tests\Document\TestDocument');
72-
$document->expects($this->any())->method('getPath')->will($this->returnValue($path));
73-
74-
return $document;
75-
}
76126
}

Tests/Document/RouteProviderTest.php

Lines changed: 91 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,68 @@
22

33
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Document;
44

5-
use Symfony\Cmf\Bundle\RoutingBundle\Tests\BaseTestCase;
65
use Symfony\Cmf\Bundle\RoutingBundle\Document\RouteProvider;
76

8-
class RouteProviderTest extends BaseTestCase
7+
class RouteProviderTest extends \PHPUnit_Framework_Testcase
98
{
9+
/** @var \PHPUnit_Framework_MockObject_MockObject */
10+
private $route;
11+
12+
/** @var \PHPUnit_Framework_MockObject_MockObject */
13+
private $route2;
14+
15+
/** @var \PHPUnit_Framework_MockObject_MockObject */
16+
private $objectManager;
17+
18+
/** @var \PHPUnit_Framework_MockObject_MockObject */
19+
private $objectManager2;
20+
21+
/** @var \PHPUnit_Framework_MockObject_MockObject */
22+
private $managerRegistry;
23+
24+
public function setUp()
25+
{
26+
$this->route = $this->getMockBuilder('Symfony\Component\Routing\Route')
27+
->disableOriginalConstructor()
28+
->getMock()
29+
;
30+
$this->route2 = $this->getMockBuilder('Symfony\Component\Routing\Route')
31+
->disableOriginalConstructor()
32+
->getMock();
33+
$this->objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
34+
$this->objectManager2 = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
35+
$this->managerRegistry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
36+
}
37+
1038
public function testGetRouteCollectionForRequest()
1139
{
1240
$this->markTestIncomplete();
1341
}
1442

1543
public function testGetRouteByName()
1644
{
17-
$managerRegistry = $this->getManagerRegistry(
18-
array(
19-
'default' => $this->getObjectManager(
20-
array('test-route' => $this->getRoute('/cms/routes/test-route'))
21-
)
22-
)
23-
);
24-
$routeProvider = new RouteProvider($managerRegistry);
45+
$this->route
46+
->expects($this->any())
47+
->method('getPath')
48+
->will($this->returnValue('/cms/routes/test-route'));
49+
50+
$this->objectManager
51+
->expects($this->any())
52+
->method('find')
53+
->with(null, '/cms/routes/test-route')
54+
->will($this->returnValue($this->route))
55+
;
56+
57+
$this->managerRegistry
58+
->expects($this->any())
59+
->method('getManager')
60+
->will($this->returnValue($this->objectManager))
61+
;
62+
63+
$routeProvider = new RouteProvider($this->managerRegistry);
2564
$routeProvider->setManagerName('default');
2665

27-
$foundRoute = $routeProvider->getRouteByName('test-route');
66+
$foundRoute = $routeProvider->getRouteByName('/cms/routes/test-route');
2867

2968
$this->assertInstanceOf('Symfony\Component\Routing\Route', $foundRoute);
3069
$this->assertEquals('/cms/routes/test-route', $foundRoute->getPath());
@@ -41,38 +80,55 @@ public function testGetRoutesByNames()
4180
*/
4281
public function testChangingDocumentManager()
4382
{
44-
$managerRegistry = $this->getManagerRegistry(
45-
array(
46-
'default' => $this->getObjectManager(
47-
array('test-route' => $this->getRoute('/cms/routes/test-route'))
48-
),
49-
'new_manager' => $this->getObjectManager(
50-
array('test-route' => $this->getRoute('/cms/routes/new-route'))
51-
)
52-
)
83+
$this->route
84+
->expects($this->any())
85+
->method('getPath')
86+
->will($this->returnValue('/cms/routes/test-route'));
87+
88+
$this->route2
89+
->expects($this->any())
90+
->method('getPath')
91+
->will($this->returnValue('/cms/routes/new-route'));
92+
93+
$this->objectManager
94+
->expects($this->any())
95+
->method('find')
96+
->with(null, '/cms/routes/test-route')
97+
->will($this->returnValue($this->route))
98+
;
99+
100+
$this->objectManager2
101+
->expects($this->any())
102+
->method('find')
103+
->with(null, '/cms/routes/test-route')
104+
->will($this->returnValue($this->route2))
105+
;
106+
107+
$objectManagers = array(
108+
'default' => $this->objectManager,
109+
'new_manager' => $this->objectManager2
53110
);
54-
$routeProvider = new RouteProvider($managerRegistry);
111+
$this->managerRegistry
112+
->expects($this->any())
113+
->method('getManager')
114+
->will(
115+
$this->returnCallback(
116+
function ($name) use ($objectManagers) {
117+
return $objectManagers[$name];
118+
}
119+
)
120+
);
121+
122+
$routeProvider = new RouteProvider($this->managerRegistry);
55123

56124
$routeProvider->setManagerName('default');
57-
$foundRoute = $routeProvider->getRouteByName('test-route');
125+
$foundRoute = $routeProvider->getRouteByName('/cms/routes/test-route');
58126
$this->assertInstanceOf('Symfony\Component\Routing\Route', $foundRoute);
59127
$this->assertEquals('/cms/routes/test-route', $foundRoute->getPath());
60128

61129
$routeProvider->setManagerName('new_manager');
62-
$newFoundRoute = $routeProvider->getRouteByName('test-route');
130+
$newFoundRoute = $routeProvider->getRouteByName('/cms/routes/test-route');
63131
$this->assertInstanceOf('Symfony\Component\Routing\Route', $newFoundRoute);
64132
$this->assertEquals('/cms/routes/new-route', $newFoundRoute->getPath());
65133
}
66-
67-
/**
68-
* @param string $path
69-
* @return \PHPUnit_Framework_MockObject_MockObject
70-
*/
71-
private function getRoute($path)
72-
{
73-
$route = $this->getMockBuilder('Symfony\Component\Routing\Route')->disableOriginalConstructor()->getMock();
74-
$route->expects($this->any())->method('getPath')->will($this->returnValue($path));
75-
76-
return $route;
77-
}
78134
}

0 commit comments

Comments
 (0)