public function testgetShortObjectDescriptionActionObject() { $mockTemplate = 'AdminHelperTest:mock-short-object-description.html.twig'; $admin = $this->getMock('Sonata\\AdminBundle\\Admin\\AdminInterface'); $admin->expects($this->once())->method('setUniqid'); $admin->expects($this->once())->method('getTemplate')->will($this->returnValue($mockTemplate)); $admin->expects($this->once())->method('getObject')->will($this->returnValue(new AdminControllerHelper_Foo())); $admin->expects($this->once())->method('toString')->will($this->returnValue('bar')); $admin->expects($this->once())->method('generateObjectUrl')->will($this->returnCallback(function ($type, $object, $parameters = array()) { if ($type != 'edit') { return 'invalid name'; } return '/ok/url'; })); $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface'); $container->expects($this->any())->method('get')->will($this->returnValue($admin)); $twig = $this->getMock('Twig_Environment'); $twig->expects($this->once())->method('render')->with($mockTemplate)->will($this->returnCallback(function ($templateName, $templateParams) { return sprintf('<a href="%s" target="new">%s</a>', $templateParams['admin']->generateObjectUrl('edit', $templateParams['object']), $templateParams['description']); })); $request = new Request(array('code' => 'sonata.post.admin', 'objectId' => 42, 'uniqid' => 'asdasd123', '_format' => 'html')); $pool = new Pool($container, 'title', 'logo'); $pool->setAdminServiceIds(array('sonata.post.admin')); $helper = new AdminHelper($pool); $validator = $this->getMock('Symfony\\Component\\Validator\\ValidatorInterface'); $controller = new HelperController($twig, $pool, $helper, $validator); $response = $controller->getShortObjectDescriptionAction($request); $expected = '<a href="/ok/url" target="new">bar</a>'; $this->assertEquals($expected, $response->getContent()); }
public function testgetShortObjectDescriptionActionObject() { $admin = $this->getMock('Sonata\\AdminBundle\\Admin\\AdminInterface'); $admin->expects($this->once())->method('setUniqid'); $admin->expects($this->once())->method('getObject')->will($this->returnValue(new AdminControllerHelper_Foo())); $admin->expects($this->once())->method('generateUrl')->will($this->returnCallback(function ($name, $parameters) { if ($name != 'edit') { return 'invalid name'; } if (!isset($parameters['id'])) { return 'id parameter not set'; } return '/ok/url'; })); $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface'); $container->expects($this->any())->method('get')->will($this->returnValue($admin)); $twig = new Twig(); $request = new Request(array('code' => 'sonata.post.admin', 'objectId' => 42, 'uniqid' => 'asdasd123')); $pool = new Pool($container, 'title', 'logo'); $helper = new AdminHelper($pool); $controller = new HelperController($twig, $pool, $helper); $response = $controller->getShortObjectDescriptionAction($request); $expected = '<a href="/ok/url" target="new">bar</a>'; $this->assertEquals($expected, $response->getContent()); }