-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for caching authentication tokens (#18)
* Add support for caching authentication tokens * Fix typo in README.md * Allow to use psr/cache 1.0 * Use `--php-version` flah in psalm CI * Allow to disable the auth cache * Code enhancement
- Loading branch information
1 parent
c096aa1
commit 29211cc
Showing
14 changed files
with
238 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetitPress\GpsMessengerBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
|
||
final class Configuration implements ConfigurationInterface | ||
{ | ||
public function getConfigTreeBuilder(): TreeBuilder | ||
{ | ||
$treeBuilder = new TreeBuilder('petit_press_gps_messenger'); | ||
$rootNode = $treeBuilder->getRootNode(); | ||
|
||
$rootNode | ||
->children() | ||
->scalarNode('auth_cache') | ||
->cannotBeEmpty() | ||
->defaultValue('cache.app') | ||
->info('A cache for storing access tokens.') | ||
->end() | ||
->end(); | ||
|
||
return $treeBuilder; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetitPress\GpsMessengerBundle\DependencyInjection; | ||
|
||
use PetitPress\GpsMessengerBundle\Transport\GpsTransportFactory; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
|
||
final class PetitPressGpsMessengerExtension extends Extension | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function load(array $configs, ContainerBuilder $container): void | ||
{ | ||
$loader = new PhpFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); | ||
$loader->load('services.php'); | ||
|
||
$configuration = new Configuration(); | ||
$config = $this->processConfiguration($configuration, $configs); | ||
|
||
if ($config['auth_cache']) { | ||
$gpsTransportFactoryDefinition = $container->getDefinition(GpsTransportFactory::class); | ||
$gpsTransportFactoryDefinition->replaceArgument(1, new Reference($config['auth_cache'])); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PetitPress\GpsMessengerBundle\Transport\GpsTransportFactory; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ReferenceConfigurator; | ||
use PetitPress\GpsMessengerBundle\Transport\GpsConfigurationResolverInterface; | ||
use PetitPress\GpsMessengerBundle\Transport\GpsConfigurationResolver; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$containerConfigurator->services() | ||
->set(GpsTransportFactory::class) | ||
->args([ | ||
new ReferenceConfigurator(GpsConfigurationResolverInterface::class), | ||
null | ||
]) | ||
->tag('messenger.transport_factory') | ||
|
||
->set(GpsConfigurationResolver::class) | ||
|
||
->alias(GpsConfigurationResolverInterface::class, GpsConfigurationResolver::class) | ||
; | ||
}; |
This file was deleted.
Oops, something went wrong.
96 changes: 96 additions & 0 deletions
96
Tests/DependencyInjection/PetitPressGpsMessengerExtensionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DependencyInjection; | ||
|
||
use PetitPress\GpsMessengerBundle\DependencyInjection\PetitPressGpsMessengerExtension; | ||
use PetitPress\GpsMessengerBundle\Transport\GpsTransportFactory; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\Yaml\Parser; | ||
|
||
class PetitPressGpsMessengerExtensionTest extends TestCase | ||
{ | ||
private ?ContainerBuilder $configuration; | ||
|
||
protected function tearDown(): void | ||
{ | ||
$this->configuration = null; | ||
} | ||
|
||
public function testSimpleConfiguration(): void | ||
{ | ||
$this->configuration = new ContainerBuilder(); | ||
$loader = new PetitPressGpsMessengerExtension(); | ||
$config = $this->getSimpleConfig(); | ||
$loader->load([$config], $this->configuration); | ||
|
||
$this->assertTrue($this->configuration->hasDefinition(GpsTransportFactory::class)); | ||
$gpsTransportFactoryDefinition = $this->configuration->getDefinition(GpsTransportFactory::class); | ||
$cacheArgument = $gpsTransportFactoryDefinition->getArgument(1); | ||
$this->assertInstanceOf(Reference::class, $cacheArgument); | ||
$this->assertEquals('cache.app', (string) $cacheArgument); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
private function getSimpleConfig() | ||
{ | ||
// use all defaults | ||
return (new Parser())->parse(''); | ||
} | ||
|
||
public function testFullConfiguration(): void | ||
{ | ||
$this->configuration = new ContainerBuilder(); | ||
$loader = new PetitPressGpsMessengerExtension(); | ||
$config = $this->getFullConfig(); | ||
$loader->load([$config], $this->configuration); | ||
|
||
$this->assertTrue($this->configuration->hasDefinition(GpsTransportFactory::class)); | ||
$gpsTransportFactoryDefinition = $this->configuration->getDefinition(GpsTransportFactory::class); | ||
$cacheArgument = $gpsTransportFactoryDefinition->getArgument(1); | ||
$this->assertInstanceOf(Reference::class, $cacheArgument); | ||
$this->assertEquals('foo', (string) $cacheArgument); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
private function getFullConfig() | ||
{ | ||
$yaml = <<<EOF | ||
auth_cache: 'foo' | ||
EOF; | ||
|
||
return (new Parser())->parse($yaml); | ||
} | ||
|
||
|
||
public function testConfigurationWithDisabledAuthCache(): void | ||
{ | ||
$this->configuration = new ContainerBuilder(); | ||
$loader = new PetitPressGpsMessengerExtension(); | ||
$config = $this->getDisabledCacheConfig(); | ||
$loader->load([$config], $this->configuration); | ||
|
||
$this->assertTrue($this->configuration->hasDefinition(GpsTransportFactory::class)); | ||
$gpsTransportFactoryDefinition = $this->configuration->getDefinition(GpsTransportFactory::class); | ||
$this->assertNull($gpsTransportFactoryDefinition->getArgument(1)); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
private function getDisabledCacheConfig() | ||
{ | ||
$yaml = <<<EOF | ||
auth_cache: false | ||
EOF; | ||
|
||
return (new Parser())->parse($yaml); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.