Skip to content

Commit

Permalink
renamed bundle and refactored services #804
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermednt committed Aug 21, 2018
1 parent 6613b46 commit c079af9
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))) {
Expand Down
3 changes: 3 additions & 0 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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%"
2 changes: 1 addition & 1 deletion app/config/routing.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

use Symfony\Component\HttpKernel\Bundle\Bundle;

class PROCERGSLoginCidadaoCpfVerificationBundle extends Bundle
class PROCERGSCpfVerificationBundle extends Bundle
{
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
lc_cpf_verification:
resource: "@PROCERGSLoginCidadaoCpfVerificationBundle/Controller/"
resource: "@PROCERGSCpfVerificationBundle/Controller/"
type: annotation
prefix: /
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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']
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* This file is part of the login-cidadao project or it's bundles.
*
* (c) Guilherme Donato <guilhermednt on github>
*
* 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* This file is part of the login-cidadao project or it's bundles.
*
* (c) Guilherme Donato <guilhermednt on github>
*
* 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')
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c079af9

Please sign in to comment.