Esempio n. 1
0
 public function testSetObjectFieldValueActionWithViolations()
 {
     $bar = new AdminControllerHelper_Bar();
     $object = new AdminControllerHelper_Foo();
     $object->setBar($bar);
     $fieldDescription = $this->getMock('Sonata\\AdminBundle\\Admin\\FieldDescriptionInterface');
     $fieldDescription->expects($this->once())->method('getOption')->will($this->returnValue(true));
     $admin = $this->getMock('Sonata\\AdminBundle\\Admin\\AdminInterface');
     $admin->expects($this->once())->method('getObject')->will($this->returnValue($object));
     $admin->expects($this->once())->method('isGranted')->will($this->returnValue(true));
     $admin->expects($this->once())->method('getListFieldDescription')->will($this->returnValue($fieldDescription));
     $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, 'field' => 'bar.enabled', 'value' => 1, 'context' => 'list'), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
     $pool = new Pool($container, 'title', 'logo');
     $pool->setAdminServiceIds(array('sonata.post.admin'));
     $helper = new AdminHelper($pool);
     $violations = new ConstraintViolationList(array(new ConstraintViolation('error1', null, array(), null, 'enabled', null), new ConstraintViolation('error2', null, array(), null, 'enabled', null)));
     $validator = $this->getMock('Symfony\\Component\\Validator\\ValidatorInterface');
     $validator->expects($this->once())->method('validateProperty')->with($bar, 'enabled')->will($this->returnValue($violations));
     $controller = new HelperController($twig, $pool, $helper, $validator);
     $response = $controller->setObjectFieldValueAction($request);
     $this->assertEquals('{"status":"KO","message":"error1\\nerror2"}', $response->getContent());
 }
 public function testsetObjectFieldValueAction()
 {
     $object = new AdminControllerHelper_Foo();
     $fieldDescription = $this->getMock('Sonata\\AdminBundle\\Admin\\FieldDescriptionInterface');
     $fieldDescription->expects($this->once())->method('getOption')->will($this->returnValue(true));
     $admin = $this->getMock('Sonata\\AdminBundle\\Admin\\AdminInterface');
     $admin->expects($this->once())->method('getObject')->will($this->returnValue($object));
     $admin->expects($this->once())->method('isGranted')->will($this->returnValue(true));
     $admin->expects($this->once())->method('getListFieldDescription')->will($this->returnValue($fieldDescription));
     $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
     $container->expects($this->any())->method('get')->will($this->returnValue($admin));
     $adminExtension = $this->getMock('Twig_ExtensionInterface', array('renderListElement', 'initRuntime', 'getTokenParsers', 'getNodeVisitors', 'getFilters', 'getTests', 'getFunctions', 'getOperators', 'getGlobals', 'getName'));
     $adminExtension->expects($this->once())->method('getName')->will($this->returnValue('sonata_admin'));
     $adminExtension->expects($this->once())->method('renderListElement')->will($this->returnValue('<foo />'));
     $twig = new Twig();
     $twig->addExtension($adminExtension);
     $request = new Request(array('code' => 'sonata.post.admin', 'objectId' => 42, 'field' => 'enabled', 'value' => 1, 'context' => 'list'), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST'));
     $pool = new Pool($container, 'title', 'logo');
     $helper = new AdminHelper($pool);
     $controller = new HelperController($twig, $pool, $helper);
     $response = $controller->setObjectFieldValueAction($request);
     $this->assertEquals('{"status":"OK","content":"<foo \\/>"}', $response->getContent());
 }