Skip to content

Commit a916a0d

Browse files
committed
Merge pull request #108 from fazy/refactor-tests
Tests: Moved mock objects 'expects()' calls inline to improve readability
2 parents 9ac4431 + 5e29ff9 commit a916a0d

File tree

3 files changed

+157
-133
lines changed

3 files changed

+157
-133
lines changed

Tests/BaseTestCase.php

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

Tests/Document/ContentRepositoryTest.php

Lines changed: 75 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,45 @@
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+
private $document;
10+
private $document2;
11+
private $objectManager;
12+
private $objectManager2;
13+
private $managerRegistry;
14+
15+
public function setUp()
16+
{
17+
$this->document = $this->getMock('Symfony\Cmf\Bundle\RoutingBundle\Tests\Document\TestDocument');
18+
$this->document2 = $this->getMock('Symfony\Cmf\Bundle\RoutingBundle\Tests\Document\TestDocument');
19+
$this->objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
20+
$this->objectManager2 = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
21+
$this->managerRegistry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
22+
}
23+
1024
public function testFindById()
1125
{
12-
$managerRegistry = $this->getManagerRegistry(
13-
array(
14-
'default' => $this->getObjectManager(
15-
array('id-123' => $this->getDocument('/cms/my-document'))
16-
)
17-
)
18-
);
26+
$this->document
27+
->expects($this->any())
28+
->method('getPath')
29+
->will($this->returnValue('/cms/my-document'))
30+
;
1931

20-
$contentRepository = new ContentRepository($managerRegistry);
32+
$this->objectManager
33+
->expects($this->any())
34+
->method('find')
35+
->will($this->returnValue($this->document))
36+
;
37+
38+
$this->managerRegistry
39+
->expects($this->any())
40+
->method('getManager')
41+
->will($this->returnValue($this->objectManager))
42+
;
43+
44+
$contentRepository = new ContentRepository($this->managerRegistry);
2145
$contentRepository->setManagerName('default');
2246

2347
$foundDocument = $contentRepository->findById('id-123');
@@ -37,17 +61,48 @@ public function testGetContentId()
3761
*/
3862
public function testChangingDocumentManager()
3963
{
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-
)
64+
$this->document
65+
->expects($this->any())
66+
->method('getPath')
67+
->will($this->returnValue('/cms/my-document'))
68+
;
69+
70+
$this->document2
71+
->expects($this->any())
72+
->method('getPath')
73+
->will($this->returnValue('/cms/new-document'))
74+
;
75+
76+
$this->objectManager
77+
->expects($this->any())
78+
->method('find')
79+
->with(null, 'id-123')
80+
->will($this->returnValue($this->document))
81+
;
82+
83+
$this->objectManager2
84+
->expects($this->any())
85+
->method('find')
86+
->with(null, 'id-123')
87+
->will($this->returnValue($this->document2))
88+
;
89+
90+
$objectManagers = array(
91+
'default' => $this->objectManager,
92+
'new_manager' => $this->objectManager2
4993
);
50-
$contentRepository = new ContentRepository($managerRegistry);
94+
$this->managerRegistry
95+
->expects($this->any())
96+
->method('getManager')
97+
->will(
98+
$this->returnCallback(
99+
function ($name) use ($objectManagers) {
100+
return $objectManagers[$name];
101+
}
102+
)
103+
);
104+
105+
$contentRepository = new ContentRepository($this->managerRegistry);
51106

52107
$contentRepository->setManagerName('default');
53108
$foundDocument = $contentRepository->findById('id-123');
@@ -59,18 +114,4 @@ public function testChangingDocumentManager()
59114
$this->assertInstanceOf('Symfony\Cmf\Bundle\RoutingBundle\Tests\Document\TestDocument', $newFoundDocument);
60115
$this->assertEquals('/cms/new-document', $newFoundDocument->getPath());
61116
}
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-
}
76117
}

Tests/Document/RouteProviderTest.php

Lines changed: 82 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,59 @@
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+
private $route;
10+
private $route2;
11+
private $objectManager;
12+
private $objectManager2;
13+
private $managerRegistry;
14+
15+
public function setUp()
16+
{
17+
$this->route = $this->getMockBuilder('Symfony\Component\Routing\Route')
18+
->disableOriginalConstructor()
19+
->getMock()
20+
;
21+
$this->route2 = $this->getMockBuilder('Symfony\Component\Routing\Route')
22+
->disableOriginalConstructor()
23+
->getMock();
24+
$this->objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
25+
$this->objectManager2 = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
26+
$this->managerRegistry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
27+
}
28+
1029
public function testGetRouteCollectionForRequest()
1130
{
1231
$this->markTestIncomplete();
1332
}
1433

1534
public function testGetRouteByName()
1635
{
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);
36+
$this->route
37+
->expects($this->any())
38+
->method('getPath')
39+
->will($this->returnValue('/cms/routes/test-route'));
40+
41+
$this->objectManager
42+
->expects($this->any())
43+
->method('find')
44+
->with(null, '/cms/routes/test-route')
45+
->will($this->returnValue($this->route))
46+
;
47+
48+
$this->managerRegistry
49+
->expects($this->any())
50+
->method('getManager')
51+
->will($this->returnValue($this->objectManager))
52+
;
53+
54+
$routeProvider = new RouteProvider($this->managerRegistry);
2555
$routeProvider->setManagerName('default');
2656

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

2959
$this->assertInstanceOf('Symfony\Component\Routing\Route', $foundRoute);
3060
$this->assertEquals('/cms/routes/test-route', $foundRoute->getPath());
@@ -41,38 +71,55 @@ public function testGetRoutesByNames()
4171
*/
4272
public function testChangingDocumentManager()
4373
{
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-
)
74+
$this->route
75+
->expects($this->any())
76+
->method('getPath')
77+
->will($this->returnValue('/cms/routes/test-route'));
78+
79+
$this->route2
80+
->expects($this->any())
81+
->method('getPath')
82+
->will($this->returnValue('/cms/routes/new-route'));
83+
84+
$this->objectManager
85+
->expects($this->any())
86+
->method('find')
87+
->with(null, '/cms/routes/test-route')
88+
->will($this->returnValue($this->route))
89+
;
90+
91+
$this->objectManager2
92+
->expects($this->any())
93+
->method('find')
94+
->with(null, '/cms/routes/test-route')
95+
->will($this->returnValue($this->route2))
96+
;
97+
98+
$objectManagers = array(
99+
'default' => $this->objectManager,
100+
'new_manager' => $this->objectManager2
53101
);
54-
$routeProvider = new RouteProvider($managerRegistry);
102+
$this->managerRegistry
103+
->expects($this->any())
104+
->method('getManager')
105+
->will(
106+
$this->returnCallback(
107+
function ($name) use ($objectManagers) {
108+
return $objectManagers[$name];
109+
}
110+
)
111+
);
112+
113+
$routeProvider = new RouteProvider($this->managerRegistry);
55114

56115
$routeProvider->setManagerName('default');
57-
$foundRoute = $routeProvider->getRouteByName('test-route');
116+
$foundRoute = $routeProvider->getRouteByName('/cms/routes/test-route');
58117
$this->assertInstanceOf('Symfony\Component\Routing\Route', $foundRoute);
59118
$this->assertEquals('/cms/routes/test-route', $foundRoute->getPath());
60119

61120
$routeProvider->setManagerName('new_manager');
62-
$newFoundRoute = $routeProvider->getRouteByName('test-route');
121+
$newFoundRoute = $routeProvider->getRouteByName('/cms/routes/test-route');
63122
$this->assertInstanceOf('Symfony\Component\Routing\Route', $newFoundRoute);
64123
$this->assertEquals('/cms/routes/new-route', $newFoundRoute->getPath());
65124
}
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-
}
78125
}

0 commit comments

Comments
 (0)