|
| 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 | +} |
0 commit comments