Skip to content
This repository has been archived by the owner on Jul 4, 2018. It is now read-only.

Commit

Permalink
fixed Symfony Form Component deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jan 20, 2015
1 parent 1c7dd57 commit 3af0b91
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 20 deletions.
19 changes: 15 additions & 4 deletions src/Silex/Provider/FormServiceProvider.php
Expand Up @@ -20,6 +20,9 @@
use Symfony\Component\Form\Extension\Validator\ValidatorExtension as FormValidatorExtension;
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\ResolvedFormTypeFactory;
use Symfony\Component\Security\Csrf\CsrfTokenManager;
use Symfony\Component\Security\Csrf\TokenStorage\NativeSessionTokenStorage;
use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;

/**
* Symfony Form component Provider.
Expand Down Expand Up @@ -104,11 +107,19 @@ public function register(Application $app)
});

$app['form.csrf_provider'] = $app->share(function ($app) {
if (isset($app['session'])) {
return new SessionCsrfProvider($app['session'], $app['form.secret']);
}
if (!class_exists('Symfony\Component\Form\Extension\DataCollector\DataCollectorExtension')) {
// Symfony 2.3
if (isset($app['session'])) {
return new SessionCsrfProvider($app['session'], $app['form.secret']);
}

return new DefaultCsrfProvider($app['form.secret']);
return new DefaultCsrfProvider($app['form.secret']);
} else {
// Symfony 2.4+
$storage = isset($app['session']) ? new SessionTokenStorage($app['session']) : new NativeSessionTokenStorage();

return new CsrfTokenManager(null, $storage);
}
});
}

Expand Down
83 changes: 68 additions & 15 deletions tests/Silex/Tests/Provider/FormServiceProviderTest.php
Expand Up @@ -20,8 +20,11 @@
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
use Symfony\Component\Form\FormTypeGuesserChain;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;

class FormServiceProviderTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -112,7 +115,13 @@ public function testFormServiceProviderWillUseTranslatorIfAvailable()
))));

$this->assertFalse($form->isValid());
$this->assertContains('ERROR: German translation', $form->getErrorsAsString());
$r = new \ReflectionMethod($form, 'getErrors');
if (!$r->getNumberOfParameters()) {
$this->assertContains('ERROR: German translation', $form->getErrorsAsString());
} else {
// as of 2.5
$this->assertContains('ERROR: German translation', (string) $form->getErrors(true, false));
}
}

public function testFormServiceProviderWillNotAddNonexistentTranslationFiles()
Expand Down Expand Up @@ -149,28 +158,72 @@ public function getName()
}
}

class DummyFormTypeExtension extends AbstractTypeExtension
{
public function getExtendedType()
if (method_exists('Symfony\Component\Form\AbstractType', 'configureOptions')) {
class DummyFormTypeExtension extends AbstractTypeExtension
{
return 'file';
}
public function getExtendedType()
{
return 'file';
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefined(array('image_path'));
}
}
} else {
class DummyFormTypeExtension extends AbstractTypeExtension
{
$resolver->setOptional(array('image_path'));
public function getExtendedType()
{
return 'file';
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
if (!method_exists($resolver, 'setDefined')) {
$resolver->setOptional(array('image_path'));
} else {
$resolver->setDefined(array('image_path'));
}
}
}
}

class FakeCsrfProvider implements CsrfProviderInterface
{
public function generateCsrfToken($intention)
if (!class_exists('Symfony\Component\Form\Extension\DataCollector\DataCollectorExtension')) {
// Symfony 2.3 only
class FakeCsrfProvider implements CsrfProviderInterface
{
return $intention.'123';
}
public function generateCsrfToken($intention)
{
return $intention.'123';
}

public function isCsrfTokenValid($intention, $token)
public function isCsrfTokenValid($intention, $token)
{
return $token === $this->generateCsrfToken($intention);
}
}
} else {
class FakeCsrfProvider implements CsrfTokenManagerInterface
{
return $token === $this->generateCsrfToken($intention);
public function getToken($tokenId)
{
return new CsrfToken($tokenId, '123');
}

public function refreshToken($tokenId)
{
return new CsrfToken($tokenId, '123');
}

public function removeToken($tokenId)
{
}

public function isTokenValid(CsrfToken $token)
{
return '123' === $token->getValue();
}
}
}
Expand Up @@ -98,7 +98,7 @@ public function testValidatorConstraint($email, $isValid, $nbGlobalError, $nbEma
->getForm()
;

$form->bind(array('email' => $email));
$form->submit(array('email' => $email));

$this->assertEquals($isValid, $form->isValid());
$this->assertEquals($nbGlobalError, count($form->getErrors()));
Expand Down

0 comments on commit 3af0b91

Please sign in to comment.