From c079af981ed7e1aa44fac24b42bbf8df365e5d8d Mon Sep 17 00:00:00 2001 From: Guilherme Donato Date: Tue, 21 Aug 2018 17:48:48 -0300 Subject: [PATCH] renamed bundle and refactored services PROCERGS#804 --- app/AppKernel.php | 2 +- app/config/config.yml | 3 + app/config/routing.yml | 2 +- .../DependencyInjection/Configuration.php | 15 +++- ...p => PROCERGSCpfVerificationExtension.php} | 14 +++- ....php => PROCERGSCpfVerificationBundle.php} | 2 +- .../Resources/config/routing.yml | 2 +- .../Resources/config/services.yml | 14 ++++ .../Service/CpfVerificationHttpService.php | 20 +++++- .../DependencyInjection/ConfigurationTest.php | 59 ++++++++++++++++ .../PROCERGSCpfVerificationExtensionTest.php | 68 +++++++++++++++++++ .../CpfVerificationHttpServiceTest.php | 17 +++++ 12 files changed, 209 insertions(+), 9 deletions(-) rename src/PROCERGS/LoginCidadao/CpfVerificationBundle/DependencyInjection/{PROCERGSLoginCidadaoCpfVerificationExtension.php => PROCERGSCpfVerificationExtension.php} (60%) rename src/PROCERGS/LoginCidadao/CpfVerificationBundle/{PROCERGSLoginCidadaoCpfVerificationBundle.php => PROCERGSCpfVerificationBundle.php} (64%) create mode 100644 src/PROCERGS/LoginCidadao/CpfVerificationBundle/Tests/DependencyInjection/ConfigurationTest.php create mode 100644 src/PROCERGS/LoginCidadao/CpfVerificationBundle/Tests/DependencyInjection/PROCERGSCpfVerificationExtensionTest.php diff --git a/app/AppKernel.php b/app/AppKernel.php index 59c74d4d1..3f082b8e7 100644 --- a/app/AppKernel.php +++ b/app/AppKernel.php @@ -76,7 +76,7 @@ public function registerBundles() new Circle\RestClientBundle\CircleRestClientBundle(), new PROCERGS\SmsServiceBundle\PROCERGSSmsServiceBundle(), new PROCERGS\LoginCidadao\PhoneVerificationBundle\PROCERGSPhoneVerificationBundle(), - new PROCERGS\LoginCidadao\CpfVerificationBundle\PROCERGSLoginCidadaoCpfVerificationBundle(), + new PROCERGS\LoginCidadao\CpfVerificationBundle\PROCERGSCpfVerificationBundle(), ); if (in_array($this->getEnvironment(), array('dev', 'test'))) { diff --git a/app/config/config.yml b/app/config/config.yml index a685e62c1..3c305ebf6 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -681,3 +681,6 @@ procergs_sms_service: id: "%tpd_system_id%" key: "%tpd_system_key%" send: "%tpd_send_message%" + +procergs_cpf_verification: + base_uri: "%nfg_cpf_verification_base_uri%" diff --git a/app/config/routing.yml b/app/config/routing.yml index a4a4a2df5..5d8e028d0 100644 --- a/app/config/routing.yml +++ b/app/config/routing.yml @@ -1,5 +1,5 @@ procergs_login_cidadao_cpf_verification: - resource: "@PROCERGSLoginCidadaoCpfVerificationBundle/Resources/config/routing.yml" + resource: "@PROCERGSCpfVerificationBundle/Resources/config/routing.yml" prefix: / procergs_login_cidadao_accounting: diff --git a/src/PROCERGS/LoginCidadao/CpfVerificationBundle/DependencyInjection/Configuration.php b/src/PROCERGS/LoginCidadao/CpfVerificationBundle/DependencyInjection/Configuration.php index 6b1e7a070..0bd93b805 100644 --- a/src/PROCERGS/LoginCidadao/CpfVerificationBundle/DependencyInjection/Configuration.php +++ b/src/PROCERGS/LoginCidadao/CpfVerificationBundle/DependencyInjection/Configuration.php @@ -18,7 +18,20 @@ class Configuration implements ConfigurationInterface public function getConfigTreeBuilder() { $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('procergs_login_cidadao_cpf_verification'); + $rootNode = $treeBuilder->root('procergs_cpf_verification'); + + $rootNode + ->children() + ->scalarNode('base_uri') + ->isRequired() + ->end() + ->scalarNode('list_challenges_path') + ->defaultNull() + ->end() + ->scalarNode('challenge_path') + ->defaultNull() + ->end() + ->end(); // Here you should define the parameters that are allowed to // configure your bundle. See the documentation linked above for diff --git a/src/PROCERGS/LoginCidadao/CpfVerificationBundle/DependencyInjection/PROCERGSLoginCidadaoCpfVerificationExtension.php b/src/PROCERGS/LoginCidadao/CpfVerificationBundle/DependencyInjection/PROCERGSCpfVerificationExtension.php similarity index 60% rename from src/PROCERGS/LoginCidadao/CpfVerificationBundle/DependencyInjection/PROCERGSLoginCidadaoCpfVerificationExtension.php rename to src/PROCERGS/LoginCidadao/CpfVerificationBundle/DependencyInjection/PROCERGSCpfVerificationExtension.php index c4246d466..98ddc9f19 100644 --- a/src/PROCERGS/LoginCidadao/CpfVerificationBundle/DependencyInjection/PROCERGSLoginCidadaoCpfVerificationExtension.php +++ b/src/PROCERGS/LoginCidadao/CpfVerificationBundle/DependencyInjection/PROCERGSCpfVerificationExtension.php @@ -12,16 +12,28 @@ * * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} */ -class PROCERGSLoginCidadaoCpfVerificationExtension extends Extension +class PROCERGSCpfVerificationExtension extends Extension { /** * {@inheritdoc} + * @throws \Exception */ public function load(array $configs, ContainerBuilder $container) { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); + $container->setParameter('procergs.nfg.cpf_verification.base_uri', $config['base_uri']); + + $endpoints = []; + if (isset($config['list_challenges_path'])) { + $endpoints['listChallenges'] = $config['list_challenges_path']; + } + if (isset($config['challenge_path'])) { + $endpoints['challenge'] = $config['challenge_path']; + } + $container->setParameter('procergs.nfg.cpf_verification.options.endpoints', $endpoints); + $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('services.yml'); } diff --git a/src/PROCERGS/LoginCidadao/CpfVerificationBundle/PROCERGSLoginCidadaoCpfVerificationBundle.php b/src/PROCERGS/LoginCidadao/CpfVerificationBundle/PROCERGSCpfVerificationBundle.php similarity index 64% rename from src/PROCERGS/LoginCidadao/CpfVerificationBundle/PROCERGSLoginCidadaoCpfVerificationBundle.php rename to src/PROCERGS/LoginCidadao/CpfVerificationBundle/PROCERGSCpfVerificationBundle.php index 735c5e6c6..dae9d7f9e 100644 --- a/src/PROCERGS/LoginCidadao/CpfVerificationBundle/PROCERGSLoginCidadaoCpfVerificationBundle.php +++ b/src/PROCERGS/LoginCidadao/CpfVerificationBundle/PROCERGSCpfVerificationBundle.php @@ -4,6 +4,6 @@ use Symfony\Component\HttpKernel\Bundle\Bundle; -class PROCERGSLoginCidadaoCpfVerificationBundle extends Bundle +class PROCERGSCpfVerificationBundle extends Bundle { } diff --git a/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Resources/config/routing.yml b/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Resources/config/routing.yml index 4af17c2c5..379f60290 100644 --- a/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Resources/config/routing.yml +++ b/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Resources/config/routing.yml @@ -1,4 +1,4 @@ lc_cpf_verification: - resource: "@PROCERGSLoginCidadaoCpfVerificationBundle/Controller/" + resource: "@PROCERGSCpfVerificationBundle/Controller/" type: annotation prefix: / diff --git a/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Resources/config/services.yml b/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Resources/config/services.yml index 8287f32b5..4c52d9086 100644 --- a/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Resources/config/services.yml +++ b/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Resources/config/services.yml @@ -1,2 +1,16 @@ services: + procergs.nfg.cpf_verification.http_client: + class: GuzzleHttp\Client + arguments: + - base_uri: "%procergs.nfg.cpf_verification.base_uri%" + procergs.nfg.cpf_verification.http_service: + class: PROCERGS\LoginCidadao\CpfVerificationBundle\Service\CpfVerificationHttpService + arguments: + - "@procergs.nfg.cpf_verification.http_client" + - + + procergs.nfg.cpf_verification.service: + class: PROCERGS\LoginCidadao\CpfVerificationBundle\Service\CpfVerificationService + arguments: + - "@procergs.nfg.cpf_verification.http_service" diff --git a/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Service/CpfVerificationHttpService.php b/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Service/CpfVerificationHttpService.php index 359c5eaab..19536cbd4 100644 --- a/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Service/CpfVerificationHttpService.php +++ b/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Service/CpfVerificationHttpService.php @@ -20,16 +20,26 @@ class CpfVerificationHttpService { + private const DEFAULT_ENDPOINTS = [ + 'listChallenges' => 'cpf/:cpf/challenges', + 'challenge' => 'cpf/:cpf/challenges/:challengeName', + ]; + /** @var Client */ private $client; + /** @var array */ + private $options; + /** * CpfVerificationHttpService constructor. * @param Client $client + * @param array $endpoints */ - public function __construct(Client $client) + public function __construct(Client $client, array $endpoints = []) { $this->client = $client; + $this->options['endpoints'] = array_merge(self::DEFAULT_ENDPOINTS, $endpoints); } /** @@ -113,11 +123,15 @@ private function getTooManyRequestsException($message = null): TooManyRequestsHt public function getListChallengesPath(string $cpf): string { - return "cpf/{$cpf}/challenges"; + return str_replace(':cpf', $cpf, $this->options['endpoints']['listChallenges']); } public function getChallengePath(ChallengeInterface $challenge): string { - return "cpf/{$challenge->getCpf()}/challenges/{$challenge->getName()}"; + return str_replace( + [':cpf', ':challengeName'], + [$challenge->getCpf(), $challenge->getName()], + $this->options['endpoints']['challenge'] + ); } } diff --git a/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Tests/DependencyInjection/ConfigurationTest.php b/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Tests/DependencyInjection/ConfigurationTest.php new file mode 100644 index 000000000..916a7b62b --- /dev/null +++ b/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Tests/DependencyInjection/ConfigurationTest.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace PROCERGS\LoginCidadao\CpfVerificationBundle\Tests\DependencyInjection; + +use PHPUnit\Framework\TestCase; +use PROCERGS\LoginCidadao\CpfVerificationBundle\DependencyInjection\Configuration; +use Symfony\Component\Config\Definition\Builder\TreeBuilder; +use Symfony\Component\Config\Definition\Processor; + +class ConfigurationTest extends TestCase +{ + private const FULL_CONFIG = [ + 'base_uri' => 'https://example.com', + 'list_challenges_path' => 'challenges/list', + 'challenge_path' => 'challenges/get/:challenge', + ]; + + public function testGetConfigTreeBuilder() + { + $configuration = new Configuration(); + $this->assertInstanceOf(TreeBuilder::class, $configuration->getConfigTreeBuilder()); + } + + public function testEmptyConfig() + { + $this->expectException(\Exception::class); + + $processor = new Processor(); + $processor->processConfiguration(new Configuration(), []); + } + + public function testMinimalConfig() + { + $processor = new Processor(); + $config = $processor->processConfiguration(new Configuration(), [['base_uri' => 'https://example.com']]); + + $this->assertEquals([ + 'base_uri' => 'https://example.com', + 'list_challenges_path' => null, + 'challenge_path' => null, + ], $config); + } + + public function testFullConfig() + { + $processor = new Processor(); + $config = $processor->processConfiguration(new Configuration(), [self::FULL_CONFIG]); + + $this->assertEquals(self::FULL_CONFIG, $config); + } +} diff --git a/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Tests/DependencyInjection/PROCERGSCpfVerificationExtensionTest.php b/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Tests/DependencyInjection/PROCERGSCpfVerificationExtensionTest.php new file mode 100644 index 000000000..4170fe75f --- /dev/null +++ b/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Tests/DependencyInjection/PROCERGSCpfVerificationExtensionTest.php @@ -0,0 +1,68 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace PROCERGS\LoginCidadao\CpfVerificationBundle\Tests\DependencyInjection; + +use PHPUnit\Framework\TestCase; +use PROCERGS\LoginCidadao\CpfVerificationBundle\DependencyInjection\PROCERGSCpfVerificationExtension; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; + +class PROCERGSCpfVerificationExtensionTest extends TestCase +{ + private function createContainer() + { + $container = new ContainerBuilder( + new ParameterBag( + [ + 'kernel.cache_dir' => __DIR__, + 'kernel.root_dir' => __DIR__.'/Fixtures', + 'kernel.charset' => 'UTF-8', + 'kernel.debug' => false, + ] + ) + ); + + return $container; + } + + private function compileContainer(ContainerBuilder $container) + { + $container->getCompilerPassConfig()->setOptimizationPasses([]); + $container->getCompilerPassConfig()->setRemovingPasses([]); + $container->compile(); + } + + public function testParametersLoaded() + { + $container = $this->createContainer(); + $container->registerExtension(new PROCERGSCpfVerificationExtension()); + $container->loadFromExtension('procergs_cpf_verification', [ + 'base_uri' => 'https://example.com', + 'list_challenges_path' => 'challenges', + 'challenge_path' => 'challenges/challenge', + ]); + $this->compileContainer($container); + + $endpoints = [ + 'listChallenges' => 'challenges', + 'challenge' => 'challenges/challenge', + ]; + + $this->assertEquals( + 'https://example.com', + $container->getParameter('procergs.nfg.cpf_verification.base_uri') + ); + $this->assertEquals( + $endpoints, + $container->getParameter('procergs.nfg.cpf_verification.options.endpoints') + ); + } +} diff --git a/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Tests/Service/CpfVerificationHttpServiceTest.php b/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Tests/Service/CpfVerificationHttpServiceTest.php index eb3a917f8..cb29dfcb2 100644 --- a/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Tests/Service/CpfVerificationHttpServiceTest.php +++ b/src/PROCERGS/LoginCidadao/CpfVerificationBundle/Tests/Service/CpfVerificationHttpServiceTest.php @@ -175,6 +175,23 @@ public function testGetChallengePath() $this->assertSame('cpf/12345678901/challenges/my_challenge', $service->getChallengePath($challenge)); } + public function testCustomChallengePath() + { + $service = new CpfVerificationHttpService($this->getHttpClient(), ['challenge' => $path = 'my/challenge']); + $challenge = $this->createMock(ChallengeInterface::class); + $challenge->expects($this->once())->method('getCpf')->willReturn('12345678901'); + $challenge->expects($this->once())->method('getName')->willReturn('my_challenge'); + + $this->assertSame($path, $service->getChallengePath($challenge)); + } + + public function testCustomListChallengesPath() + { + $service = new CpfVerificationHttpService($this->getHttpClient(), ['listChallenges' => $path = 'challenges/']); + + $this->assertSame($path, $service->getListChallengesPath('12345678901')); + } + /** * @param array $responses * @return Client