Guest User

Untitled

a guest
Sep 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. <?php
  2. namespace Evolutionnutrition\ApiBundle\Tests\Base;
  3.  
  4. use Evolutionnutrition\ApiBundle\Tests\Base\BaseUnitTests;
  5.  
  6.  
  7. abstract class BaseControllerTests extends BaseUnitTests
  8. {
  9. protected $controllerMethodsList = [
  10. 'parseControls',
  11. 'getParsedEntityClass',
  12. 'getParsedFormClass',
  13. 'beginTransaction',
  14. 'getUser',
  15. 'isAnonym',
  16. 'getEM',
  17. 'dispatchBeforeCreateEvent',
  18. 'dispatchAfterCreateEvent',
  19. 'checkDates',
  20. 'getFormBuilder',
  21. 'secureSearchById',
  22. 'getEntityShortName',
  23. 'dispatchBeforeUpdateEvent',
  24. 'dispatchAfterUpdateEvent',
  25. ];
  26.  
  27. protected function getControllerMock (MockDataMapper $controllerDM, $params)
  28. {
  29. $inParams = $params['in'];
  30. $data = $params['in']['data'];
  31. $isAnonym = $inParams['currentUser'] ? false : true;
  32.  
  33. $entityName = $controllerDM->getEntityName();
  34. $entityClassFullName = "Evolutionnutrition\\ApiBundle\\Entity\\{$entityName}";
  35.  
  36. $entityObj = null;
  37. if ('create' === $controllerDM->getAction()) {
  38. $entityObj = new $entityClassFullName;
  39. $controllerDM->setEntity($entityObj);
  40. } elseif ('update' === $controllerDM->getAction()) {
  41. $entityObj = $controllerDM->getEntity();
  42. }
  43.  
  44. $className = "Evolutionnutrition\\ApiBundle\\Controller\\{$entityName}Controller";
  45.  
  46. $mockBuilder = $this->getMockBuilder($className);
  47. $controllerDM->addMethods($this->controllerMethodsList);
  48. $methodsList = $controllerDM->getMethods();
  49.  
  50. $controller = $mockBuilder
  51. ->setMethods($methodsList)
  52. ->getMock();
  53.  
  54. $controller->setContainer(static::$container);
  55.  
  56. $controller
  57. ->expects($this->any())
  58. ->method('getParsedEntityClass')
  59. ->will($this->returnValue($entityClassFullName));
  60. $controller
  61. ->expects($this->any())
  62. ->method('getEntityShortName')
  63. ->will($this->returnValue($entityName));
  64. $controller
  65. ->expects($this->any())
  66. ->method('getParsedFormClass')
  67. ->will($this->returnValue("Evolutionnutrition\\ApiBundle\\Form\\{$entityName}Type"));
  68. $controller
  69. ->expects($this->any())
  70. ->method('getUser')
  71. ->will($this->returnValue($inParams['currentUser']));
  72. $controller
  73. ->expects($this->any())
  74. ->method('secureSearchById')
  75. ->will($this->returnValue($controllerDM->hasKey('dbFoundEntity') ? $controllerDM->getDbFoundEntity() : $entityObj));
  76. $controller
  77. ->expects($this->any())
  78. ->method('isAnonym')
  79. ->will($this->returnValue($isAnonym));
  80.  
  81. $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
  82. ->setMethods(['persist', 'flush', 'detach', 'getConnection'])
  83. ->disableOriginalConstructor()
  84. ->getMock();
  85. $connection = $this->getMockBuilder('\Doctrine\DBAL\Connection')
  86. ->disableOriginalConstructor()
  87. ->getMock();
  88. $em->expects($this->any())
  89. ->method('getConnection')
  90. ->will($this->returnValue($connection));
  91.  
  92. $controller
  93. ->expects($this->any())
  94. ->method('getEM')
  95. ->will($this->returnValue($em));
  96.  
  97. $controllerDM->setEM($em);
  98.  
  99.  
  100. $formBuilder = $this->getMockBuilder("Evolutionnutrition\\ApiBundle\\Form\\{$entityName}Type")
  101. // ->setConstructorArgs([static::$container])
  102. // ->disableOriginalConstructor()
  103. ->setMethods(['buildForm'])
  104. ->getMock();
  105. $controllerDM->setFormBuilder($formBuilder);
  106.  
  107. $formBuilder
  108. ->expects($this->any())
  109. ->method('buildForm')
  110. ->will($this->returnCallback(function ($builder, $options) use ($formBuilder) {
  111. $formBuilder->afterBuildForm($builder, $options);
  112. }));
  113.  
  114. $controller
  115. ->expects($this->any())
  116. ->method('getFormBuilder')
  117. ->will($this->returnValue($formBuilder));
  118.  
  119. $outParams = $params['out'];
  120. if (!empty($outParams['exception'])) {
  121. $this->setExpectedException('\Exception');
  122. }
  123.  
  124. if (!empty($outParams['expectations'])) {
  125. $outParams['expectations']($entityObj, $controllerDM, $params);
  126. }
  127.  
  128. $controllerDM->setRunTest(function () use ($controller, $data, $entityObj, $outParams, $controllerDM, $params) {
  129. $action = $controllerDM->getAction();
  130. switch ($action) {
  131. case 'create':
  132. $ret = $controller->$action($data, $entityObj);
  133. break;
  134. case 'update':
  135. $id = $controllerDM->hasKey('id') ? $controllerDM->getId('id') : 'some id';
  136. $ret = $controller->$action($data, $id, $entityObj);
  137. break;
  138. default:
  139. throw new \Exception("Unknown action {$action}", 1);
  140. break;
  141. }
  142.  
  143. if (!empty($outParams['onFinishChecker'])) {
  144. $outParams['onFinishChecker']($entityObj, $controllerDM, $params);
  145. }
  146.  
  147. if (!empty($outParams['errorMessage'])) {
  148. $messagesArr = $controller->getErrorMessages();
  149. if ($outParams['errorMessage'] === 'print') {
  150. print_r([
  151. 'errors' => $messagesArr,
  152. 'ret' => is_object($ret) ? '[object] ' . get_class($ret) : $ret,
  153. ]);
  154. } elseif ($outParams['errorMessage'] === -1) {
  155. $this->assertCount(0, $messagesArr, 'Unexpected error message.');
  156. } elseif (is_array($outParams['errorMessage'])) {
  157. foreach ($outParams['errorMessage'] as $message) {
  158. $this->assertContains($message, $messagesArr, 'Wrong error message.');
  159. }
  160. } else {
  161. $this->assertContains($outParams['errorMessage'], $messagesArr, 'Wrong error message.');
  162. }
  163. };
  164. });
  165. return $controller;
  166. }
  167.  
  168. protected function generateContainerMock ($currentUser)
  169. {
  170. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  171.  
  172. $container
  173. ->expects($this->any())
  174. ->method('get')
  175. ->with('security.context')
  176. ->will($this->returnValue($this->getSecurityContext($currentUser)));
  177. return $container;
  178. }
  179.  
  180. private function getSecurityContext ($user)
  181. {
  182. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  183. $token->expects($this->any())
  184. ->method('getUser')
  185. ->will($this->returnValue($user));
  186. $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface', ['getToken', 'setToken', 'isGranted']);
  187. $securityContext->expects($this->any())
  188. ->method('getToken')
  189. ->will($this->returnValue($token));
  190. $securityContext->expects($this->any())
  191. ->method('setToken')
  192. ->will($this->returnValue($token));
  193.  
  194. return $securityContext;
  195. }
  196. }
Add Comment
Please, Sign In to add comment