Skip to content

Commit

Permalink
#328 Fix encoder autowiring for symfony <3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
chalasr authored Apr 28, 2017
2 parents 4bc0b3b + ce71938 commit 81f7315
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 16 deletions.
6 changes: 3 additions & 3 deletions DependencyInjection/LexikJWTAuthenticationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWSProvider\JWSProviderInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTManagerInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor\ChainTokenExtractor;
use Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor\TokenExtractorInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
Expand Down Expand Up @@ -60,7 +60,7 @@ public function load(array $configs, ContainerBuilder $container)
private static function registerAutowiringTypes(ContainerBuilder $container)
{
$container
->findDefinition('lexik_jwt_authentication.encoder')
->findDefinition('lexik_jwt_authentication.encoder.default')
->addAutowiringType(JWTEncoderInterface::class);

$container
Expand All @@ -69,7 +69,7 @@ private static function registerAutowiringTypes(ContainerBuilder $container)

$container
->getDefinition('lexik_jwt_authentication.extractor.chain_extractor')
->addAutowiringType(ChainTokenExtractor::class);
->addAutowiringType(TokenExtractorInterface::class);

$container
->getDefinition('lexik_jwt_authentication.jwt_manager')
Expand Down
63 changes: 51 additions & 12 deletions Tests/Functional/DependencyInjection/AutowiringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use Lexik\Bundle\JWTAuthenticationBundle\DependencyInjection\LexikJWTAuthenticationExtension;
use Lexik\Bundle\JWTAuthenticationBundle\Encoder\DefaultEncoder;
use Lexik\Bundle\JWTAuthenticationBundle\LexikJWTAuthenticationBundle;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWSProvider\DefaultJWSProvider;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTManager;
use Lexik\Bundle\JWTAuthenticationBundle\Tests\Stubs\Autowired;
use Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor\ChainTokenExtractor;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
Expand All @@ -19,18 +21,7 @@ class AutowiringTest extends \PHPUnit_Framework_TestCase
{
public function testAutowiring()
{
$container = new ContainerBuilder(new ParameterBag([
'kernel.bundles' => ['FrameworkBundle' => FrameworkBundle::class, 'LexikJWTAuthenticationBundle' => LexikJWTAuthenticationBundle::class],
'kernel.bundles_metadata' => [],
'kernel.cache_dir' => __DIR__,
'kernel.debug' => false,
'kernel.environment' => 'test',
'kernel.name' => 'kernel',
'kernel.root_dir' => __DIR__,
'kernel.container_class' => 'AutowiringTestContainer',
'kernel.charset' => 'utf8',
]));

$container = $this->createContainerBuilder();
$container->registerExtension(new SecurityExtension());
$container->registerExtension(new FrameworkExtension());
$container->registerExtension(new LexikJWTAuthenticationExtension());
Expand All @@ -47,5 +38,53 @@ public function testAutowiring()

$this->assertInstanceOf(JWTManager::class, $autowired->getJWTManager());
$this->assertInstanceOf(DefaultEncoder::class, $autowired->getJWTEncoder());
$this->assertInstanceOf(ChainTokenExtractor::class, $autowired->getTokenExtractor());
$this->assertInstanceOf(DefaultJWSProvider::class, $autowired->getJWSProvider());
}

public function testAutowireConfiguredEncoderServiceForInterfaceTypeHint()
{
if (!method_exists(ContainerBuilder::class, 'fileExists')) {
$this->markTestSkipped('Using the configured encoder for autowiring is supported using symfony 3.3+ only.');
}

$container = $this->createContainerBuilder();
$container->registerExtension(new SecurityExtension());
$container->registerExtension(new FrameworkExtension());
$container->registerExtension(new LexikJWTAuthenticationExtension());

(new YamlFileLoader($container, new FileLocator([__DIR__.'/../app/config'])))->load('config_custom_encoder.yml');

$container
->register('autowired', Autowired::class)
->setAutowired(true);

$container->compile();

$autowired = $container->get('autowired');

$this->assertInstanceOf(DummyEncoder::class, $autowired->getJWTEncoder());
}

private static function createContainerBuilder()
{
return new ContainerBuilder(new ParameterBag([
'kernel.bundles' => ['FrameworkBundle' => FrameworkBundle::class, 'LexikJWTAuthenticationBundle' => LexikJWTAuthenticationBundle::class],
'kernel.bundles_metadata' => [],
'kernel.cache_dir' => __DIR__,
'kernel.debug' => false,
'kernel.environment' => 'test',
'kernel.name' => 'kernel',
'kernel.root_dir' => __DIR__,
'kernel.container_class' => 'AutowiringTestContainer',
'kernel.charset' => 'utf8',
]));
}
}

final class DummyEncoder extends DefaultEncoder
{
public function __construct()
{
}
}
13 changes: 13 additions & 0 deletions Tests/Functional/app/config/config_custom_encoder.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
imports:
- { resource: base_config.yml }

lexik_jwt_authentication:
private_key_path: '%kernel.root_dir%/../var/jwt/private.pem'
public_key_path: '%kernel.root_dir%/../var/jwt/public.pem'
pass_phrase: testing
encoder:
service: app.dummy_encoder

services:
app.dummy_encoder:
class: Lexik\Bundle\JWTAuthenticationBundle\Tests\Functional\DependencyInjection\DummyEncoder
18 changes: 17 additions & 1 deletion Tests/Stubs/Autowired.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
namespace Lexik\Bundle\JWTAuthenticationBundle\Tests\Stubs;

use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWSProvider\JWSProviderInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTManager;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor\TokenExtractorInterface;

class Autowired
{
private $jwtManager;
private $jwtEncoder;
private $tokenExtractor;
private $jwsProvider;

public function __construct(JWTTokenManagerInterface $jwtManager, JWTEncoderInterface $jwtEncoder)
public function __construct(JWTTokenManagerInterface $jwtManager, JWTEncoderInterface $jwtEncoder, TokenExtractorInterface $tokenExtractor, JWSProviderInterface $jwsProvider)
{
$this->jwtManager = $jwtManager;
$this->jwtEncoder = $jwtEncoder;
$this->tokenExtractor = $tokenExtractor;
$this->jwsProvider = $jwsProvider;
}

public function getJWTManager()
Expand All @@ -26,4 +32,14 @@ public function getJWTEncoder()
{
return $this->jwtEncoder;
}

public function getTokenExtractor()
{
return $this->tokenExtractor;
}

public function getJWSProvider()
{
return $this->jwsProvider;
}
}

0 comments on commit 81f7315

Please sign in to comment.