/** * {@inheritdoc} */ protected function setUp() { $this->container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface'); $this->request = new Request(); $this->pool = new Pool($this->container, 'title', 'logo.png'); $this->pool->setAdminServiceIds(array('foo.admin')); $this->request->attributes->set('_sonata_admin', 'foo.admin'); $this->admin = $this->getMock('Sonata\\AdminBundle\\Admin\\AdminInterface'); $this->parameters = array(); $this->template = ''; // php 5.3 BC $params =& $this->parameters; $template =& $this->template; $templating = $this->getMock('Symfony\\Bundle\\FrameworkBundle\\Templating\\DelegatingEngine', array(), array($this->container, array())); $templating->expects($this->any())->method('renderResponse')->will($this->returnCallback(function ($view, array $parameters = array(), Response $response = null) use(&$params, &$template) { $template = $view; if (null === $response) { $response = new Response(); } $params = $parameters; return $response; })); $this->session = new Session(new MockArraySessionStorage()); // php 5.3 BC $pool = $this->pool; $request = $this->request; $admin = $this->admin; $session = $this->session; $twig = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock(); $twigRenderer = $this->getMock('Symfony\\Bridge\\Twig\\Form\\TwigRendererInterface'); $formExtension = new FormExtension($twigRenderer); $twig->expects($this->any())->method('getExtension')->will($this->returnCallback(function ($name) use($formExtension) { switch ($name) { case 'form': return $formExtension; } return; })); $exporter = $this->getMock('Sonata\\AdminBundle\\Export\\Exporter'); $exporter->expects($this->any())->method('getResponse')->will($this->returnValue(new StreamedResponse())); $this->auditManager = $this->getMockBuilder('Sonata\\AdminBundle\\Model\\AuditManager')->disableOriginalConstructor()->getMock(); $this->adminObjectAclManipulator = $this->getMockBuilder('Sonata\\AdminBundle\\Util\\AdminObjectAclManipulator')->disableOriginalConstructor()->getMock(); // php 5.3 BC $auditManager = $this->auditManager; $adminObjectAclManipulator = $this->adminObjectAclManipulator; $this->csrfProvider = $this->getMockBuilder('Symfony\\Component\\Form\\Extension\\Csrf\\CsrfProvider\\CsrfProviderInterface')->getMock(); $this->csrfProvider->expects($this->any())->method('generateCsrfToken')->will($this->returnCallback(function ($intention) { return 'csrf-token-123_' . $intention; })); $this->csrfProvider->expects($this->any())->method('isCsrfTokenValid')->will($this->returnCallback(function ($intention, $token) { if ($token == 'csrf-token-123_' . $intention) { return true; } return false; })); // php 5.3 BC $csrfProvider = $this->csrfProvider; $this->logger = $this->getMock('Psr\\Log\\LoggerInterface'); $logger = $this->logger; // php 5.3 BC $requestStack = null; if (Kernel::MINOR_VERSION > 3) { $requestStack = new \Symfony\Component\HttpFoundation\RequestStack(); $requestStack->push($request); } $this->container->expects($this->any())->method('get')->will($this->returnCallback(function ($id) use($pool, $request, $admin, $templating, $twig, $session, $exporter, $auditManager, $adminObjectAclManipulator, $requestStack, $csrfProvider, $logger) { switch ($id) { case 'sonata.admin.pool': return $pool; case 'request_stack': return $requestStack; case 'request': return $request; case 'foo.admin': return $admin; case 'templating': return $templating; case 'twig': return $twig; case 'session': return $session; case 'sonata.admin.exporter': return $exporter; case 'sonata.admin.audit.manager': return $auditManager; case 'sonata.admin.object.manipulator.acl.admin': return $adminObjectAclManipulator; case 'form.csrf_provider': return $csrfProvider; case 'logger': return $logger; } return; })); // php 5.3 $tthis = $this; $this->container->expects($this->any())->method('has')->will($this->returnCallback(function ($id) use($tthis) { if ($id == 'form.csrf_provider' && $tthis->getCsrfProvider() !== null) { return true; } if ($id == 'logger') { return true; } return false; })); $this->admin->expects($this->any())->method('getTemplate')->will($this->returnCallback(function ($name) { switch ($name) { case 'ajax': return 'SonataAdminBundle::ajax_layout.html.twig'; case 'layout': return 'SonataAdminBundle::standard_layout.html.twig'; case 'show': return 'SonataAdminBundle:CRUD:show.html.twig'; case 'show_compare': return 'SonataAdminBundle:CRUD:show_compare.html.twig'; case 'edit': return 'SonataAdminBundle:CRUD:edit.html.twig'; case 'dashboard': return 'SonataAdminBundle:Core:dashboard.html.twig'; case 'search': return 'SonataAdminBundle:Core:search.html.twig'; case 'list': return 'SonataAdminBundle:CRUD:list.html.twig'; case 'preview': return 'SonataAdminBundle:CRUD:preview.html.twig'; case 'history': return 'SonataAdminBundle:CRUD:history.html.twig'; case 'acl': return 'SonataAdminBundle:CRUD:acl.html.twig'; case 'delete': return 'SonataAdminBundle:CRUD:delete.html.twig'; case 'batch': return 'SonataAdminBundle:CRUD:list__batch.html.twig'; case 'batch_confirmation': return 'SonataAdminBundle:CRUD:batch_confirmation.html.twig'; } return; })); $this->admin->expects($this->any())->method('getIdParameter')->will($this->returnValue('id')); $this->admin->expects($this->any())->method('generateUrl')->will($this->returnCallback(function ($name, array $parameters = array(), $absolute = false) { $result = $name; if (!empty($parameters)) { $result .= '?' . http_build_query($parameters); } return $result; })); $this->admin->expects($this->any())->method('generateObjectUrl')->will($this->returnCallback(function ($name, $object, array $parameters = array(), $absolute = false) { $result = get_class($object) . '_' . $name; if (!empty($parameters)) { $result .= '?' . http_build_query($parameters); } return $result; })); $this->controller = new CRUDController(); $this->controller->setContainer($this->container); // Make some methods public to test them $testedMethods = array('renderJson', 'isXmlHttpRequest', 'configure', 'getBaseTemplate', 'redirectTo', 'addFlash'); foreach ($testedMethods as $testedMethod) { $method = new \ReflectionMethod('Sonata\\AdminBundle\\Controller\\CRUDController', $testedMethod); $method->setAccessible(true); $this->protectedTestedMethods[$testedMethod] = $method; } }