Skip to content

Commit

Permalink
Merge pull request #2285 from Richtermeister/fix-form-via-classname
Browse files Browse the repository at this point in the history
Fix ability to specify form via class name.
  • Loading branch information
Paweł Jędrzejewski committed Dec 26, 2014
2 parents e935c96 + 771526c commit d5f92d8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
Expand Up @@ -324,7 +324,9 @@ public function getForm($resource = null)
{
$type = $this->config->getFormType();

if (!$this->get('form.registry')->hasType($type)) {
if (strpos($type, '\\') !== false) { // full class name specified
$type = new $type();
} elseif (!$this->get('form.registry')->hasType($type)) { // form alias is not registered
$defaultFormFactory = new DefaultFormFactory($this->container->get('form.factory'));

return $defaultFormFactory->create($resource, $this->container->get($this->config->getServiceName('manager')));
Expand All @@ -334,10 +336,6 @@ public function getForm($resource = null)
return $this->container->get('form.factory')->createNamed('', $type, $resource, array('csrf_protection' => false));
}

if (strpos($type, '\\') !== false) {
$type = new $type;
}

return $this->createForm($type, $resource);
}

Expand Down
Expand Up @@ -2,8 +2,19 @@

namespace spec\Sylius\Bundle\ResourceBundle\Controller;

use Doctrine\Common\Persistence\ObjectManager;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\ResourceBundle\Controller\Configuration;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Translation\TranslatorInterface;

/**
* Resource controller spec.
Expand All @@ -12,9 +23,28 @@
*/
class ResourceControllerSpec extends ObjectBehavior
{
function let(Configuration $configuration)
{
function let(
Configuration $configuration,
ContainerInterface $container,
RouterInterface $router,
SessionInterface $session,
TranslatorInterface $translator,
ObjectManager $objectManager,
EventDispatcherInterface $eventDispatcher,
FormFactoryInterface $formFactory
) {
$this->beConstructedWith($configuration);
$configuration->isApiRequest()->willReturn(false);

$configuration->getServiceName('manager')->willReturn('some_manager');
$container->get('router')->willReturn($router);
$container->get('session')->willReturn($session);
$container->get('translator')->willReturn($translator);
$container->get('some_manager')->willReturn($objectManager);
$container->get('event_dispatcher')->willReturn($eventDispatcher);
$container->get('form.factory')->willReturn($formFactory);

$this->setContainer($container);
}

function it_is_initializable()
Expand All @@ -26,4 +56,27 @@ function it_is_a_controller()
{
$this->shouldHaveType('Symfony\Bundle\FrameworkBundle\Controller\Controller');
}

function it_gets_form_from_class_name(
Configuration $configuration,
FormFactoryInterface $formFactory,
FormInterface $form
) {
$formClass = 'spec\Sylius\Bundle\ResourceBundle\Controller\TestFormType';
$configuration->getFormType()->willReturn($formClass);
$formFactory->create(Argument::type($formClass), Argument::cetera())->shouldBeCalled()->willReturn($form);

$this->getForm()->shouldReturn($form);
}
}

class TestFormType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'sylius_test_form';
}
}

0 comments on commit d5f92d8

Please sign in to comment.