Skip to content

Commit

Permalink
[RFR] Fix #24: bridge normalizer (#25)
Browse files Browse the repository at this point in the history
* Fix #24: bridge normalizer

* Fix PHP5.5 typehint

* Use custom normalizer bridge
  • Loading branch information
vincentchalamon authored Jun 6, 2017
1 parent 7b82fca commit 6ebb7ed
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 7 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ script:
- if [[ $lint = 1 ]]; then php php-cs-fixer.phar fix --dry-run --diff; fi
- vendor/bin/phpunit
- vendor/bin/behat
- vendor/bin/behat -p jmsserializer
2 changes: 1 addition & 1 deletion Controller/ForgotPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

use CoopTilleuls\ForgotPasswordBundle\Entity\AbstractPasswordToken;
use CoopTilleuls\ForgotPasswordBundle\Manager\ForgotPasswordManager;
use CoopTilleuls\ForgotPasswordBundle\Normalizer\NormalizerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* @author Vincent Chalamon <[email protected]>
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function getConfigTreeBuilder()
->scalarNode('password_field')->defaultValue('password')->cannotBeEmpty()->info('User password field name.')->end()
->end()
->end()
->booleanNode('use_jms_serializer')->defaultFalse()->end()
->end();

return $treeBuilder;
Expand Down
8 changes: 8 additions & 0 deletions DependencyInjection/CoopTilleulsForgotPasswordExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@

namespace CoopTilleuls\ForgotPasswordBundle\DependencyInjection;

use CoopTilleuls\ForgotPasswordBundle\Normalizer\JMSNormalizer;
use CoopTilleuls\ForgotPasswordBundle\Normalizer\SymfonyNormalizer;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
Expand Down Expand Up @@ -47,5 +51,9 @@ public function load(array $configs, ContainerBuilder $container)
throw new \LogicException(sprintf('Service "%s" does not exist.', $config['manager']));
}
$container->setAlias('coop_tilleuls_forgot_password.manager', $config['manager']);

// Build normalizer
$class = true === $config['use_jms_serializer'] ? JMSNormalizer::class : SymfonyNormalizer::class;
$container->setDefinition('coop_tilleuls_forgot_password.normalizer', new Definition($class, [new Reference('serializer')]))->setPublic(false);
}
}
42 changes: 42 additions & 0 deletions Normalizer/JMSNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the CoopTilleulsForgotPasswordBundle package.
*
* (c) Vincent Chalamon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CoopTilleuls\ForgotPasswordBundle\Normalizer;

use CoopTilleuls\ForgotPasswordBundle\Entity\AbstractPasswordToken;
use JMS\Serializer\ArrayTransformerInterface;

/**
* @author Vincent Chalamon <[email protected]>
*/
final class JMSNormalizer implements NormalizerInterface
{
/**
* @var ArrayTransformerInterface
*/
private $normalizer;

/**
* @param ArrayTransformerInterface $normalizer
*/
public function __construct(ArrayTransformerInterface $normalizer)
{
$this->normalizer = $normalizer;
}

/**
* {@inheritdoc}
*/
public function normalize(AbstractPasswordToken $object, $format, array $context = [])
{
return $this->normalizer->toArray($object);
}
}
29 changes: 29 additions & 0 deletions Normalizer/NormalizerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the CoopTilleulsForgotPasswordBundle package.
*
* (c) Vincent Chalamon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CoopTilleuls\ForgotPasswordBundle\Normalizer;

use CoopTilleuls\ForgotPasswordBundle\Entity\AbstractPasswordToken;

/**
* @author Vincent Chalamon <[email protected]>
*/
interface NormalizerInterface
{
/**
* @param AbstractPasswordToken $object
* @param string $format
* @param array $context
*
* @return mixed
*/
public function normalize(AbstractPasswordToken $object, $format, array $context = []);
}
42 changes: 42 additions & 0 deletions Normalizer/SymfonyNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the CoopTilleulsForgotPasswordBundle package.
*
* (c) Vincent Chalamon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CoopTilleuls\ForgotPasswordBundle\Normalizer;

use CoopTilleuls\ForgotPasswordBundle\Entity\AbstractPasswordToken;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface as SymfonyNormalizerInterface;

/**
* @author Vincent Chalamon <[email protected]>
*/
final class SymfonyNormalizer implements NormalizerInterface
{
/**
* @var SymfonyNormalizerInterface
*/
private $normalizer;

/**
* @param SymfonyNormalizerInterface $normalizer
*/
public function __construct(SymfonyNormalizerInterface $normalizer)
{
$this->normalizer = $normalizer;
}

/**
* {@inheritdoc}
*/
public function normalize(AbstractPasswordToken $object, $format, array $context = [])
{
return $this->normalizer->normalize($object, $format, $context);
}
}
2 changes: 1 addition & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<services>
<service id="coop_tilleuls_forgot_password.controller.forgot_password" class="CoopTilleuls\ForgotPasswordBundle\Controller\ForgotPasswordController">
<argument type="service" id="coop_tilleuls_forgot_password.manager.forgot_password" />
<argument type="service" id="serializer" />
<argument type="service" id="coop_tilleuls_forgot_password.normalizer" />
<argument>%coop_tilleuls_forgot_password.password_token_serialization_groups%</argument>
</service>

Expand Down
18 changes: 18 additions & 0 deletions behat.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Test with Symfony Serializer
default:
suites:
default:
Expand All @@ -13,3 +14,20 @@ default:
debug: 'true'
path: 'features/app/AppKernel.php'
bootstrap: 'features/app/bootstrap.php'

# Test with JmsSerializerBundle
jmsserializer:
suites:
default:
contexts:
- FeatureContext:
- '@test.client'
- '@doctrine'
- '@coop_tilleuls_forgot_password.manager.password_token'
extensions:
'Behat\Symfony2Extension':
kernel:
env: 'jmsserializer'
debug: 'true'
path: 'features/app/AppKernel.php'
bootstrap: 'features/app/bootstrap.php'
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"symfony/dependency-injection": "^2.3 || ^3.0",
"symfony/event-dispatcher": "^2.3 || ^3.0",
"symfony/http-foundation": "^2.3 || ^3.0",
"symfony/http-kernel": "^2.3 || ^3.0",
"symfony/serializer": "^2.7 || ^3.0"
"symfony/http-kernel": "^2.3 || ^3.0"
},
"require-dev": {
"behat/behat": "^3.1",
Expand All @@ -42,7 +41,9 @@
"symfony/security-bundle": "^2.3 || ^3.0",
"symfony/swiftmailer-bundle": "^2.3.11",
"symfony/templating": "^2.3 || ^3.0",
"symfony/twig-bundle": "^2.8 || ^3.0"
"symfony/twig-bundle": "^2.8 || ^3.0",
"symfony/serializer": "^2.7 || ^3.0",
"jms/serializer-bundle": "^1.4"
},
"suggest": {
"doctrine/doctrine-bundle": "To connect with Doctrine in Symfony project",
Expand Down
8 changes: 7 additions & 1 deletion features/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class AppKernel extends Kernel

public function registerBundles()
{
return [
$bundles = [
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
Expand All @@ -39,6 +39,11 @@ public function registerBundles()
new CoopTilleuls\ForgotPasswordBundle\CoopTilleulsForgotPasswordBundle(),
new CoopTilleuls\ForgotPasswordBundle\Tests\TestBundle\CoopTilleulsTestBundle(),
];
if ('jmsserializer' === $this->getEnvironment()) {
$bundles[] = new JMS\SerializerBundle\JMSSerializerBundle();
}

return $bundles;
}

protected function configureRoutes(RouteCollectionBuilder $routes)
Expand All @@ -51,6 +56,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
$c->loadFromExtension('coop_tilleuls_forgot_password', [
'password_token_class' => PasswordToken::class,
'user_class' => User::class,
'use_jms_serializer' => 'jmsserializer' === $this->getEnvironment(),
]);

$c->loadFromExtension('swiftmailer', [
Expand Down
2 changes: 1 addition & 1 deletion tests/Controller/ForgotPasswordControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
use CoopTilleuls\ForgotPasswordBundle\Controller\ForgotPasswordController;
use CoopTilleuls\ForgotPasswordBundle\Entity\AbstractPasswordToken;
use CoopTilleuls\ForgotPasswordBundle\Manager\ForgotPasswordManager;
use CoopTilleuls\ForgotPasswordBundle\Normalizer\NormalizerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* @author Vincent Chalamon <[email protected]>
Expand Down
33 changes: 33 additions & 0 deletions tests/Normalizer/JMSNormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the CoopTilleulsForgotPasswordBundle package.
*
* (c) Vincent Chalamon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tests\ForgotPasswordBundle\Normalizer;

use CoopTilleuls\ForgotPasswordBundle\Entity\AbstractPasswordToken;
use CoopTilleuls\ForgotPasswordBundle\Normalizer\JMSNormalizer;
use JMS\Serializer\ArrayTransformerInterface;

/**
* @author Vincent Chalamon <[email protected]>
*/
final class JMSNormalizerTest extends \PHPUnit_Framework_TestCase
{
public function testNormalize()
{
$normalizerMock = $this->prophesize(ArrayTransformerInterface::class);
$passwordTokenMock = $this->prophesize(AbstractPasswordToken::class);

$normalizerMock->toArray($passwordTokenMock)->willReturn('foo')->shouldBeCalledTimes(1);

$normalizer = new JMSNormalizer($normalizerMock->reveal());
$this->assertEquals('foo', $normalizer->normalize($passwordTokenMock->reveal(), 'json'));
}
}
33 changes: 33 additions & 0 deletions tests/Normalizer/SymfonyNormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the CoopTilleulsForgotPasswordBundle package.
*
* (c) Vincent Chalamon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tests\ForgotPasswordBundle\Normalizer;

use CoopTilleuls\ForgotPasswordBundle\Entity\AbstractPasswordToken;
use CoopTilleuls\ForgotPasswordBundle\Normalizer\SymfonyNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* @author Vincent Chalamon <[email protected]>
*/
final class SymfonyNormalizerTest extends \PHPUnit_Framework_TestCase
{
public function testNormalize()
{
$normalizerMock = $this->prophesize(NormalizerInterface::class);
$passwordTokenMock = $this->prophesize(AbstractPasswordToken::class);

$normalizerMock->normalize($passwordTokenMock, 'json', [])->willReturn('foo')->shouldBeCalledTimes(1);

$normalizer = new SymfonyNormalizer($normalizerMock->reveal());
$this->assertEquals('foo', $normalizer->normalize($passwordTokenMock->reveal(), 'json'));
}
}

0 comments on commit 6ebb7ed

Please sign in to comment.