PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/Request/ParamConverter/DoctrineParamConverterTest.php

https://github.com/schmittjoh/SensioFrameworkExtraBundle
PHP | 276 lines | 214 code | 56 blank | 6 comment | 3 complexity | 48d8eefa14795bcd7c6029a795012d8d MD5 | raw file
  1. <?php
  2. namespace Sensio\Bundle\FrameworkExtraBundle\Tests\Request\ParamConverter;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\DoctrineParamConverter;
  5. class DoctrineParamConverterTest extends \PHPUnit_Framework_TestCase
  6. {
  7. /**
  8. * @var Doctrine\Common\Persistence\ManagerRegistry
  9. */
  10. private $registry;
  11. /**
  12. * @var DoctrineParamConverter
  13. */
  14. private $converter;
  15. public function setUp()
  16. {
  17. if (!interface_exists('Doctrine\Common\Persistence\ManagerRegistry')) {
  18. $this->markTestSkipped();
  19. }
  20. $this->registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
  21. $this->converter = new DoctrineParamConverter($this->registry);
  22. }
  23. public function createConfiguration($class = null, array $options = null, $name = 'arg', $isOptional = false)
  24. {
  25. $config = $this->getMock(
  26. 'Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface', array(
  27. 'getClass', 'getAliasName', 'getOptions', 'isOptional', 'getName',
  28. ));
  29. if ($options !== null) {
  30. $config->expects($this->once())
  31. ->method('getOptions')
  32. ->will($this->returnValue($options));
  33. }
  34. if ($class !== null) {
  35. $config->expects($this->any())
  36. ->method('getClass')
  37. ->will($this->returnValue($class));
  38. }
  39. $config->expects($this->any())
  40. ->method('getName')
  41. ->will($this->returnValue($name));
  42. $config->expects($this->any())
  43. ->method('isOptional')
  44. ->will($this->returnValue($isOptional));
  45. return $config;
  46. }
  47. public function testApplyWithNoIdAndData()
  48. {
  49. $request = new Request();
  50. $config = $this->createConfiguration(null, array());
  51. $objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
  52. $this->setExpectedException('LogicException');
  53. $this->converter->apply($request, $config);
  54. }
  55. public function testApplyWithNoIdAndDataOptional()
  56. {
  57. $request = new Request();
  58. $config = $this->createConfiguration(null, array(), 'arg', true);
  59. $objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
  60. $ret = $this->converter->apply($request, $config);
  61. $this->assertTrue($ret);
  62. $this->assertNull($request->attributes->get('arg'));
  63. }
  64. public function testApplyWithId()
  65. {
  66. $request = new Request();
  67. $request->attributes->set('id', 1);
  68. $config = $this->createConfiguration('stdClass', array('id' => 'id'), 'arg');
  69. $manager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
  70. $objectRepository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository');
  71. $this->registry->expects($this->once())
  72. ->method('getManagerForClass')
  73. ->with('stdClass')
  74. ->will($this->returnValue($manager));
  75. $manager->expects($this->once())
  76. ->method('getRepository')
  77. ->with('stdClass')
  78. ->will($this->returnValue($objectRepository));
  79. $objectRepository->expects($this->once())
  80. ->method('find')
  81. ->with($this->equalTo(1))
  82. ->will($this->returnValue($object =new \stdClass));
  83. $ret = $this->converter->apply($request, $config);
  84. $this->assertTrue($ret);
  85. $this->assertSame($object, $request->attributes->get('arg'));
  86. }
  87. public function testApplyWithMappingAndExclude()
  88. {
  89. $request = new Request();
  90. $request->attributes->set('foo', 1);
  91. $request->attributes->set('bar', 2);
  92. $config = $this->createConfiguration(
  93. 'stdClass',
  94. array('mapping' => array('foo' => 'Foo'), 'exclude' => array('bar')),
  95. 'arg'
  96. );
  97. $manager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
  98. $metadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
  99. $repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository');
  100. $this->registry->expects($this->once())
  101. ->method('getManagerForClass')
  102. ->with('stdClass')
  103. ->will($this->returnValue($manager));
  104. $manager->expects($this->once())
  105. ->method('getClassMetadata')
  106. ->with('stdClass')
  107. ->will($this->returnValue($metadata));
  108. $manager->expects($this->once())
  109. ->method('getRepository')
  110. ->with('stdClass')
  111. ->will($this->returnValue($repository));
  112. $metadata->expects($this->once())
  113. ->method('hasField')
  114. ->with($this->equalTo('Foo'))
  115. ->will($this->returnValue(true));
  116. $repository->expects($this->once())
  117. ->method('findOneBy')
  118. ->with($this->equalTo(array('Foo' => 1)))
  119. ->will($this->returnValue($object =new \stdClass));
  120. $ret = $this->converter->apply($request, $config);
  121. $this->assertTrue($ret);
  122. $this->assertSame($object, $request->attributes->get('arg'));
  123. }
  124. public function testApplyWithRepositoryMethod()
  125. {
  126. $request = new Request();
  127. $request->attributes->set('id', 1);
  128. $config = $this->createConfiguration(
  129. 'stdClass',
  130. array('repository_method' => 'getClassName'),
  131. 'arg'
  132. );
  133. $objectRepository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository');
  134. $this->manager->expects($this->once())
  135. ->method('getRepository')
  136. ->will($this->returnValue($objectRepository));
  137. $objectRepository->expects($this->once())
  138. ->method('getClassName')
  139. ->will($this->returnValue($className = 'ObjectRepository'));
  140. $ret = $this->converter->apply($request, $config);
  141. $this->assertTrue($ret);
  142. $this->assertSame($className, $request->attributes->get('arg'));
  143. }
  144. public function testApplyWithRepositoryMethodAndMapping()
  145. {
  146. $request = new Request();
  147. $request->attributes->set('id', 1);
  148. $config = $this->createConfiguration(
  149. 'stdClass',
  150. array('repository_method' => 'getClassName', 'mapping' => array('foo' => 'Foo')),
  151. 'arg'
  152. );
  153. $objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
  154. $metadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
  155. $this->manager->expects($this->once())
  156. ->method('getManager')
  157. ->will($this->returnValue($objectManager));
  158. $objectManager->expects($this->once())
  159. ->method('getClassMetadata')
  160. ->will($this->returnValue($metadata));
  161. $metadata->expects($this->once())
  162. ->method('hasField')
  163. ->with($this->equalTo('Foo'))
  164. ->will($this->returnValue(true));
  165. $objectRepository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository');
  166. $this->manager->expects($this->once())
  167. ->method('getRepository')
  168. ->will($this->returnValue($objectRepository));
  169. $objectRepository->expects($this->once())
  170. ->method('getClassName')
  171. ->will($this->returnValue($className = 'ObjectRepository'));
  172. $ret = $this->converter->apply($request, $config);
  173. $this->assertTrue($ret);
  174. $this->assertSame($className, $request->attributes->get('arg'));
  175. }
  176. public function testSupports()
  177. {
  178. $config = $this->createConfiguration('stdClass', array());
  179. $metadataFactory = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadataFactory');
  180. $metadataFactory->expects($this->once())
  181. ->method('isTransient')
  182. ->with($this->equalTo('stdClass'))
  183. ->will($this->returnValue( false ));
  184. $objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
  185. $objectManager->expects($this->once())
  186. ->method('getMetadataFactory')
  187. ->will($this->returnValue($metadataFactory));
  188. $this->registry->expects($this->once())
  189. ->method('getManagers')
  190. ->will($this->returnValue(array($objectManager)));
  191. $this->registry->expects($this->once())
  192. ->method('getManagerForClass')
  193. ->with('stdClass')
  194. ->will($this->returnValue($objectManager));
  195. $ret = $this->converter->supports($config);
  196. $this->assertTrue($ret, "Should be supported");
  197. }
  198. public function testSupportsWithConfiguredEntityManager()
  199. {
  200. $config = $this->createConfiguration('stdClass', array('entity_manager' => 'foo'));
  201. $metadataFactory = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadataFactory');
  202. $metadataFactory->expects($this->once())
  203. ->method('isTransient')
  204. ->with($this->equalTo('stdClass'))
  205. ->will($this->returnValue( false ));
  206. $objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
  207. $objectManager->expects($this->once())
  208. ->method('getMetadataFactory')
  209. ->will($this->returnValue($metadataFactory));
  210. $this->registry->expects($this->once())
  211. ->method('getManagers')
  212. ->will($this->returnValue(array($objectManager)));
  213. $this->registry->expects($this->once())
  214. ->method('getManager')
  215. ->with('foo')
  216. ->will($this->returnValue($objectManager));
  217. $ret = $this->converter->supports($config);
  218. $this->assertTrue($ret, "Should be supported");
  219. }
  220. }