Skip to content

Commit 92420ab

Browse files
committed
Added ORM Unit tests
1 parent 02ee7a7 commit 92420ab

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Unit\Doctrine\Orm;
4+
5+
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\ContentRepository;
6+
7+
class ContentRepositoryTest extends \PHPUnit_Framework_TestCase
8+
{
9+
private $document;
10+
private $managerRegistry;
11+
private $objectManager;
12+
private $objectRepository;
13+
14+
public function setUp()
15+
{
16+
$this->document = new \stdClass;
17+
$this->objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
18+
$this->managerRegistry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
19+
$this->objectRepository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository');
20+
}
21+
22+
public function testFindById()
23+
{
24+
$this->objectManager
25+
->expects($this->any())
26+
->method('getRepository')
27+
->with($this->equalTo('stdClass'))
28+
->will($this->returnValue($this->objectRepository))
29+
;
30+
31+
$this->objectRepository
32+
->expects($this->any())
33+
->method('find')
34+
->with(123)
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);
45+
$contentRepository->setManagerName('default');
46+
47+
$foundDocument = $contentRepository->findById('stdClass:123');
48+
49+
$this->assertSame($this->document, $foundDocument);
50+
}
51+
52+
/**
53+
* @dataProvider getFindCorrectModelAndIdData
54+
*/
55+
public function testFindCorrectModelAndId($input, $model, $id)
56+
{
57+
$this->objectManager
58+
->expects($this->any())
59+
->method('getRepository')
60+
->with($this->equalTo($model))
61+
->will($this->returnValue($this->objectRepository))
62+
;
63+
64+
$this->objectRepository
65+
->expects($this->any())
66+
->method('find')
67+
->with($id)
68+
;
69+
70+
$this->managerRegistry
71+
->expects($this->any())
72+
->method('getManager')
73+
->will($this->returnValue($this->objectManager))
74+
;
75+
76+
$contentRepository = new ContentRepository($this->managerRegistry);
77+
$contentRepository->setManagerName('default');
78+
79+
$foundDocument = $contentRepository->findById($input);
80+
}
81+
82+
public function getFindCorrectModelAndIdData()
83+
{
84+
return array(
85+
array('Acme\ContentBundle\Entity\Content:12', 'Acme\ContentBundle\Entity\Content', 12),
86+
array('Id\Contains\Colon:12:1', 'Id\Contains\Colon', '12:1'),
87+
array('Class\EndsWith\Number12:20', 'Class\EndsWith\Number12', 20),
88+
);
89+
}
90+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Doctrine\Orm;
4+
5+
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\RouteProvider;
6+
7+
class RouteProviderTest extends \PHPUnit_Framework_Testcase
8+
{
9+
private $route;
10+
private $managerRegistry;
11+
private $objectManager;
12+
private $objectRepository;
13+
14+
public function setUp()
15+
{
16+
$this->route = $this->getMockBuilder('Symfony\Component\Routing\Route')
17+
->disableOriginalConstructor()
18+
->getMock();
19+
$this->objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
20+
$this->managerRegistry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
21+
$this->objectRepository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository');
22+
}
23+
24+
public function testGetRouteByName()
25+
{
26+
$this->route
27+
->expects($this->any())
28+
->method('getPath')
29+
->will($this->returnValue('/cms/routes/test-route'));
30+
31+
$this->objectManager
32+
->expects($this->any())
33+
->method('getRepository')
34+
->will($this->returnValue($this->objectRepository))
35+
;
36+
37+
$this->objectRepository
38+
->expects($this->any())
39+
->method('findBy')
40+
->with(array('name' => '/cms/routes/test-route'))
41+
->will($this->returnValue($this->route))
42+
;
43+
44+
$this->managerRegistry
45+
->expects($this->any())
46+
->method('getManager')
47+
->will($this->returnValue($this->objectManager))
48+
;
49+
50+
$routeProvider = new RouteProvider($this->managerRegistry);
51+
$routeProvider->setManagerName('default');
52+
53+
$foundRoute = $routeProvider->getRouteByName('/cms/routes/test-route');
54+
55+
$this->assertInstanceOf('Symfony\Component\Routing\Route', $foundRoute);
56+
$this->assertEquals('/cms/routes/test-route', $foundRoute->getPath());
57+
}
58+
59+
public function testGetRoutesByNames()
60+
{
61+
$this->markTestIncomplete();
62+
}
63+
}

0 commit comments

Comments
 (0)