Skip to content

Commit

Permalink
Style fix plus removed phpmock
Browse files Browse the repository at this point in the history
  • Loading branch information
massimilianobraglia committed Jan 18, 2019
1 parent 12dfa62 commit 7e348a1
Show file tree
Hide file tree
Showing 18 changed files with 119 additions and 105 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ cache:
matrix:
fast_finish: true
include:
- php: 7.0
env: SYMFONY_VERSION='^2.8'
- php: 7.0
env: SYMFONY_VERSION='^3.0'
- php: 7.1
Expand Down
4 changes: 2 additions & 2 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
<?php declare(strict_types=1);

require 'vendor/autoload.php';

class_alias(\PHPUnit\Framework\TestCase::class, \PHPUnit_Framework_TestCase::class);
\class_alias(\PHPUnit\Framework\TestCase::class, \PHPUnit_Framework_TestCase::class);
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
"symfony/asset": "^2.8|^3.0|^4.0",
"symfony/browser-kit": "^2.8|^3.0|^4.0",
"symfony/css-selector": "^2.3|^3.0|^4.0",
"symfony/swiftmailer-bundle": "^2.3.3|^3.0",
"symfony/swiftmailer-bundle": "^2.5|^3.0",
"symfony/templating": "^2.8|^3.0|^4.0",
"symfony/twig-bundle": "^2.8|^3.0|^4.0",
"twilio/sdk": "^5.0",
"php-mock/php-mock-prophecy": "^0.0.2"
"twilio/sdk": "^5.0"
},
"conflict": {
"symfony/swiftmailer-bundle": "<2.3.3",
Expand All @@ -34,7 +33,8 @@
}
},
"config": {
"bin-dir": "bin/"
"bin-dir": "bin/",
"sort-packages": true
},
"extra": {
"branch-alias": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function process(ContainerBuilder $container)
}

$mailers = $container->hasParameter('fazland.notifire.emails.mailers') ? $container->getParameter('fazland.notifire.emails.mailers') : [];
$mailers = array_merge($swift_mailers, $mailers);
$mailers = \array_merge($swift_mailers, $mailers);
foreach ($mailers as $name => $mailer) {
$serviceName = 'fazland.notifire.handler.email.'.$name;
if ('swiftmailer' === $mailer['provider']) {
Expand Down Expand Up @@ -64,7 +64,7 @@ public function process(ContainerBuilder $container)
}

$strategy = $config['strategy'];
if (in_array($strategy, ['rand'])) {
if (\in_array($strategy, ['rand'])) {
$strategy = 'fazland.notifire.handler_choice_strategy.'.$strategy;
}

Expand Down Expand Up @@ -93,7 +93,7 @@ protected function getSwiftMailers(ContainerBuilder $container): array
$swiftMailerConfig = $processor->processConfiguration($swiftMailerConfiguration, $swiftMailerConfig);

$mailers = [];
foreach (array_keys($swiftMailerConfig['mailers']) as $name) {
foreach (\array_keys($swiftMailerConfig['mailers']) as $name) {
$mailers[$name] = ['provider' => 'swiftmailer', 'mailer_name' => $name];
}

Expand Down
16 changes: 14 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Fazland\NotifireBundle\DependencyInjection;

use Fazland\NotifireBundle\Utils\ClassUtils;
use Fazland\SkebbyRestClient\Constant\SendMethods;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeParentInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand All @@ -12,10 +14,20 @@
*/
class Configuration implements ConfigurationInterface
{
/**
* @var ClassUtils
*/
private $classUtils;

public function __construct(ClassUtils $classUtils)
{
$this->classUtils = $classUtils;
}

/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): NodeParentInterface
{
if (\method_exists(TreeBuilder::class, 'getRootNode')) {
$treeBuilder = new TreeBuilder('fazland_notifire');
Expand Down Expand Up @@ -145,7 +157,7 @@ private function addSmsSection(ArrayNodeDefinition $rootNode)
->end()
;

if (class_exists(SendMethods::class)) {
if ($this->classUtils->exists(SendMethods::class)) {
$service
->enumNode('method')
->info('Skebby send method')
Expand Down
7 changes: 4 additions & 3 deletions src/DependencyInjection/NotifireExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Fazland\NotifireBundle\DependencyInjection;

use Fazland\NotifireBundle\Utils\ClassUtils;
use Fazland\SkebbyRestClient\Client\Client as SkebbyRestClient;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\FileLocator;
Expand All @@ -23,7 +24,7 @@ class NotifireExtension extends Extension
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$configuration = new Configuration(new ClassUtils());

$config = $this->processConfiguration($configuration, $configs);

Expand Down Expand Up @@ -116,7 +117,7 @@ private function processSms(ContainerBuilder $container, array $config)
}

$strategy = $config['strategy'];
if (in_array($strategy, ['rand'])) {
if (\in_array($strategy, ['rand'])) {
$strategy = 'fazland.notifire.handler_choice_strategy.'.$strategy;
}

Expand Down Expand Up @@ -146,7 +147,7 @@ protected function createTwilioService(
string $token
): string {
$definition = new Definition(
! class_exists(Client::class) ? \Services_Twilio::class : Client::class,
! \class_exists(Client::class) ? \Services_Twilio::class : Client::class,
[$sid, $token]
);
$definition->addTag("fazland.notifire.twilio.$name");
Expand Down
1 change: 0 additions & 1 deletion src/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
*/
interface ExceptionInterface
{

}
18 changes: 18 additions & 0 deletions src/Utils/ClassUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace Fazland\NotifireBundle\Utils;

class ClassUtils
{
/**
* Whether the specified class exists or not.
*
* @param string $className
*
* @return bool
*/
public function exists(string $className): bool
{
return \class_exists($className);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class EmailConfigurationPassTest extends TestCase
*/
private $container;

/**
* {@inheritdoc}
*/
public function setUp()
{
$this->pass = new EmailConfigurationPass();
Expand Down Expand Up @@ -66,10 +69,10 @@ public function testShouldAutoregisterSwiftmailerMailers()

$this->pass->process($this->container);

$this->assertTrue($this->container->has('fazland.notifire.handler.email.first'));
$this->assertTrue($this->container->has('fazland.notifire.handler.email.second'));
self::assertTrue($this->container->has('fazland.notifire.handler.email.first'));
self::assertTrue($this->container->has('fazland.notifire.handler.email.second'));

$this->assertEquals(SwiftMailerHandler::class, $this->container->getDefinition('fazland.notifire.handler.email.first')->getClass());
$this->assertEquals(SwiftMailerHandler::class, $this->container->getDefinition('fazland.notifire.handler.email.second')->getClass());
self::assertEquals(SwiftMailerHandler::class, $this->container->getDefinition('fazland.notifire.handler.email.first')->getClass());
self::assertEquals(SwiftMailerHandler::class, $this->container->getDefinition('fazland.notifire.handler.email.second')->getClass());
}
}
11 changes: 9 additions & 2 deletions tests/DependencyInjection/CompilerPass/ExtensionPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@
use Fazland\NotifireBundle\DependencyInjection\CompilerPass\ExtensionPass;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class ExtensionPassTest extends TestCase
{
/**
* @var CompilerPassInterface
*/
private $compilerPass;

/**
* {@inheritdoc}
*/
public function setUp()
{
$this->compilerPass = new ExtensionPass();
Expand All @@ -33,10 +40,10 @@ public function testProcessShouldAddExtensionsCallToTheBuilder()
]);

$definition->addMethodCall('addExtension', Argument::that(function ($arg) {
return is_array($arg) && $arg[0] instanceof Reference && 'extension.one' === (string) $arg[0];
return \is_array($arg) && $arg[0] instanceof Reference && 'extension.one' === (string) $arg[0];
}))->shouldBeCalled();
$definition->addMethodCall('addExtension', Argument::that(function ($arg) {
return is_array($arg) && $arg[0] instanceof Reference && 'extension.two' === (string) $arg[0];
return \is_array($arg) && $arg[0] instanceof Reference && 'extension.two' === (string) $arg[0];
}))->shouldBeCalled();

$this->compilerPass->process($container->reveal());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Fazland\NotifireBundle\DependencyInjection\CompilerPass\VariableRendererPass;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
Expand All @@ -14,8 +15,14 @@
*/
class VariableRendererPassTest extends TestCase
{
/**
* @var CompilerPassInterface
*/
private $compilerPass;

/**
* {@inheritdoc}
*/
public function setUp()
{
$this->compilerPass = new VariableRendererPass();
Expand All @@ -40,10 +47,10 @@ public function testProcessShouldRegisterVariableRenderers()
$container->hasParameter('fazland.notifire.default_variable_renderer')->willReturn(false);

$definition->addMethodCall('addRenderer', Argument::that(function ($arg) {
return is_array($arg) && $arg[0] instanceof Reference && 'renderer.one' === (string) $arg[0];
return \is_array($arg) && $arg[0] instanceof Reference && 'renderer.one' === (string) $arg[0];
}))->shouldBeCalled();
$definition->addMethodCall('addRenderer', Argument::that(function ($arg) {
return is_array($arg) && $arg[0] instanceof Reference && 'renderer.two' === (string) $arg[0];
return \is_array($arg) && $arg[0] instanceof Reference && 'renderer.two' === (string) $arg[0];
}))->shouldBeCalled();

$container->getDefinition('fazland.notifire.variable_renderer.factory')
Expand Down
80 changes: 18 additions & 62 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,85 +3,40 @@
namespace Fazland\NotifierBundle\Tests\DependencyInjection;

use Fazland\NotifireBundle\DependencyInjection\Configuration;
use Fazland\NotifireBundle\Utils\ClassUtils;
use Fazland\SkebbyRestClient\Constant\SendMethods;
use phpmock\prophecy\PHPProphet;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Symfony\Component\Config\Definition\Processor;

class ConfigurationTest extends TestCase
{
/**
* @var ClassUtils|ObjectProphecy
*/
private $classUtils;

/**
* @var Processor
*/
private $processor;

/**
* {@inheritdoc}
*/
public function setUp()
{
$this->classUtils = $this->prophesize(ClassUtils::class);
$this->processor = new Processor();
}

/**
* @runInSeparateProcess
*/
public function testConfigureSmsSkebbyNotPresent()
public function testNotConfigured()
{
$prophet = new PHPProphet();

$namespace = $prophet->prophesize('Fazland\NotifireBundle\DependencyInjection');
$namespace->class_exists(SendMethods::class)
->shouldBeCalled()
->willReturn(false);

$namespace->reveal();

$services = [
'client1' => [
'provider' => 'twilio',
'username' => 'foo_twilio',
'password' => 'bar_twilio',
'sender' => '+393668887789',
],
];
$this->classUtils->exists(SendMethods::class)->willReturn(false);

$configuration = $this->getConfigs([
'sms' => [
'services' => $services,
],
]);

$this->assertEquals([
'email' => [
'enabled' => false,
'auto_configure_swiftmailer' => true,
'mailers' => [],
],
'sms' => [
'enabled' => true,
'services' => [
'client1' => [
'provider' => 'twilio',
'username' => 'foo_twilio',
'password' => 'bar_twilio',
'sender' => '+393668887789',
'twilio_messaging_service_sid' => null,
'composite' => [
'providers' => [],
'strategy' => 'rand',
],
'logger_service' => null,
],
],
],
], $configuration);

$prophet->checkPredictions();
}

public function testUnconfigured()
{
$configuration = $this->getConfigs([]);

$this->assertEquals([
self::assertEquals([
'email' => [
'enabled' => false,
'auto_configure_swiftmailer' => true,
Expand Down Expand Up @@ -118,13 +73,14 @@ public function testConfigureSms()
],
];

$this->classUtils->exists(SendMethods::class)->willReturn(true);
$configuration = $this->getConfigs([
'sms' => [
'services' => $services,
],
]);

$this->assertEquals([
self::assertEquals([
'email' => [
'enabled' => false,
'auto_configure_swiftmailer' => true,
Expand Down Expand Up @@ -177,9 +133,9 @@ public function testConfigureSms()
], $configuration);
}

private function getConfigs(array $configArray)
private function getConfigs(array $configArray): array
{
$configuration = new Configuration();
$configuration = new Configuration($this->classUtils->reveal());

return $this->processor->processConfiguration($configuration, [$configArray]);
}
Expand Down
Loading

0 comments on commit 7e348a1

Please sign in to comment.