Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Evolutionnutrition\ApiBundle\Tests\Base;
- use Evolutionnutrition\ApiBundle\Tests\Base\BaseUnitTests;
- abstract class BaseControllerTests extends BaseUnitTests
- {
- protected $controllerMethodsList = [
- 'parseControls',
- 'getParsedEntityClass',
- 'getParsedFormClass',
- 'beginTransaction',
- 'getUser',
- 'isAnonym',
- 'getEM',
- 'dispatchBeforeCreateEvent',
- 'dispatchAfterCreateEvent',
- 'checkDates',
- 'getFormBuilder',
- 'secureSearchById',
- 'getEntityShortName',
- 'dispatchBeforeUpdateEvent',
- 'dispatchAfterUpdateEvent',
- ];
- protected function getControllerMock (MockDataMapper $controllerDM, $params)
- {
- $inParams = $params['in'];
- $data = $params['in']['data'];
- $isAnonym = $inParams['currentUser'] ? false : true;
- $entityName = $controllerDM->getEntityName();
- $entityClassFullName = "Evolutionnutrition\\ApiBundle\\Entity\\{$entityName}";
- $entityObj = null;
- if ('create' === $controllerDM->getAction()) {
- $entityObj = new $entityClassFullName;
- $controllerDM->setEntity($entityObj);
- } elseif ('update' === $controllerDM->getAction()) {
- $entityObj = $controllerDM->getEntity();
- }
- $className = "Evolutionnutrition\\ApiBundle\\Controller\\{$entityName}Controller";
- $mockBuilder = $this->getMockBuilder($className);
- $controllerDM->addMethods($this->controllerMethodsList);
- $methodsList = $controllerDM->getMethods();
- $controller = $mockBuilder
- ->setMethods($methodsList)
- ->getMock();
- $controller->setContainer(static::$container);
- $controller
- ->expects($this->any())
- ->method('getParsedEntityClass')
- ->will($this->returnValue($entityClassFullName));
- $controller
- ->expects($this->any())
- ->method('getEntityShortName')
- ->will($this->returnValue($entityName));
- $controller
- ->expects($this->any())
- ->method('getParsedFormClass')
- ->will($this->returnValue("Evolutionnutrition\\ApiBundle\\Form\\{$entityName}Type"));
- $controller
- ->expects($this->any())
- ->method('getUser')
- ->will($this->returnValue($inParams['currentUser']));
- $controller
- ->expects($this->any())
- ->method('secureSearchById')
- ->will($this->returnValue($controllerDM->hasKey('dbFoundEntity') ? $controllerDM->getDbFoundEntity() : $entityObj));
- $controller
- ->expects($this->any())
- ->method('isAnonym')
- ->will($this->returnValue($isAnonym));
- $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
- ->setMethods(['persist', 'flush', 'detach', 'getConnection'])
- ->disableOriginalConstructor()
- ->getMock();
- $connection = $this->getMockBuilder('\Doctrine\DBAL\Connection')
- ->disableOriginalConstructor()
- ->getMock();
- $em->expects($this->any())
- ->method('getConnection')
- ->will($this->returnValue($connection));
- $controller
- ->expects($this->any())
- ->method('getEM')
- ->will($this->returnValue($em));
- $controllerDM->setEM($em);
- $formBuilder = $this->getMockBuilder("Evolutionnutrition\\ApiBundle\\Form\\{$entityName}Type")
- // ->setConstructorArgs([static::$container])
- // ->disableOriginalConstructor()
- ->setMethods(['buildForm'])
- ->getMock();
- $controllerDM->setFormBuilder($formBuilder);
- $formBuilder
- ->expects($this->any())
- ->method('buildForm')
- ->will($this->returnCallback(function ($builder, $options) use ($formBuilder) {
- $formBuilder->afterBuildForm($builder, $options);
- }));
- $controller
- ->expects($this->any())
- ->method('getFormBuilder')
- ->will($this->returnValue($formBuilder));
- $outParams = $params['out'];
- if (!empty($outParams['exception'])) {
- $this->setExpectedException('\Exception');
- }
- if (!empty($outParams['expectations'])) {
- $outParams['expectations']($entityObj, $controllerDM, $params);
- }
- $controllerDM->setRunTest(function () use ($controller, $data, $entityObj, $outParams, $controllerDM, $params) {
- $action = $controllerDM->getAction();
- switch ($action) {
- case 'create':
- $ret = $controller->$action($data, $entityObj);
- break;
- case 'update':
- $id = $controllerDM->hasKey('id') ? $controllerDM->getId('id') : 'some id';
- $ret = $controller->$action($data, $id, $entityObj);
- break;
- default:
- throw new \Exception("Unknown action {$action}", 1);
- break;
- }
- if (!empty($outParams['onFinishChecker'])) {
- $outParams['onFinishChecker']($entityObj, $controllerDM, $params);
- }
- if (!empty($outParams['errorMessage'])) {
- $messagesArr = $controller->getErrorMessages();
- if ($outParams['errorMessage'] === 'print') {
- print_r([
- 'errors' => $messagesArr,
- 'ret' => is_object($ret) ? '[object] ' . get_class($ret) : $ret,
- ]);
- } elseif ($outParams['errorMessage'] === -1) {
- $this->assertCount(0, $messagesArr, 'Unexpected error message.');
- } elseif (is_array($outParams['errorMessage'])) {
- foreach ($outParams['errorMessage'] as $message) {
- $this->assertContains($message, $messagesArr, 'Wrong error message.');
- }
- } else {
- $this->assertContains($outParams['errorMessage'], $messagesArr, 'Wrong error message.');
- }
- };
- });
- return $controller;
- }
- protected function generateContainerMock ($currentUser)
- {
- $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $container
- ->expects($this->any())
- ->method('get')
- ->with('security.context')
- ->will($this->returnValue($this->getSecurityContext($currentUser)));
- return $container;
- }
- private function getSecurityContext ($user)
- {
- $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
- $token->expects($this->any())
- ->method('getUser')
- ->will($this->returnValue($user));
- $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface', ['getToken', 'setToken', 'isGranted']);
- $securityContext->expects($this->any())
- ->method('getToken')
- ->will($this->returnValue($token));
- $securityContext->expects($this->any())
- ->method('setToken')
- ->will($this->returnValue($token));
- return $securityContext;
- }
- }
Add Comment
Please, Sign In to add comment