Skip to content
This repository has been archived by the owner on Mar 9, 2023. It is now read-only.

Commit

Permalink
Update of Bundle for Symfony 3.4
Browse files Browse the repository at this point in the history
* Update composer dependencies and PHP dependencies

* Adapt tests to PHPUnit 6

* Update readme file for suggesting dependency injection

* Update service definition for autowire

* Adapt tests for different ICU versions
  • Loading branch information
flagbird committed Jul 23, 2018
1 parent 8fc890f commit 42d4508
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 81 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
@@ -1,9 +1,9 @@
language: php

php:
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- nightly

env:
Expand Down
10 changes: 4 additions & 6 deletions DependencyInjection/FlagbitCurrencyExtension.php
Expand Up @@ -10,12 +10,10 @@
class FlagbitCurrencyExtension extends Extension
{
/**
* Loads a specific configuration.
* @param array $config
* @param ContainerBuilder $container
*
* @param array $config An array of configuration values
* @param ContainerBuilder $container A ContainerBuilder instance
*
* @throws \InvalidArgumentException When provided tag is not defined in this extension
* @throws \Exception
*
* @api
*/
Expand All @@ -28,6 +26,6 @@ public function load(array $config, ContainerBuilder $container)
$loader->load('services.xml');

$container->findDefinition('flagbit_currency')
->addArgument($config['default_currency']);
->setArgument(1, $config['default_currency']);
}
}
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -39,6 +39,9 @@ $container->get('flagbit_currency')->getCurrencySymbol('EUR'); // €
$container->get('flagbit_currency')->getCurrencySymbol('USD'); // $
```

**It is recommended, that you don't fetch a service directly from a container and use
dependency injection instead.**

## Twig

### Functions
Expand Down
20 changes: 9 additions & 11 deletions Resources/config/services.xml
Expand Up @@ -4,24 +4,22 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="flagbit_currency.class">Flagbit\Bundle\CurrencyBundle\Service\Currency</parameter>
<parameter key="flagbit_currency.twig.class">Flagbit\Bundle\CurrencyBundle\Twig\FlagbitCurrencyExtension</parameter>
</parameters>

<services>
<service id="flagbit_currency.intl_bundle" class="Symfony\Component\Intl\ResourceBundle\CurrencyBundleInterface">
<defaults public="false" autowire="true" autoconfigure="true" />

<prototype namespace="Flagbit\Bundle\CurrencyBundle\Service\" resource="../../Service/*" />
<prototype namespace="Flagbit\Bundle\CurrencyBundle\Twig\" resource="../../Twig/*" />

<service id="flagbit_currency.intl_bundle" class="Symfony\Component\Intl\ResourceBundle\CurrencyBundleInterface" public="true">
<factory class="Symfony\Component\Intl\Intl" method="getCurrencyBundle"/>
</service>

<service id="flagbit_currency" class="%flagbit_currency.class%">
<service id="Flagbit\Bundle\CurrencyBundle\Service\Currency">
<argument type="service" id="flagbit_currency.intl_bundle"/>
<argument />
</service>

<service id="flagbit_currency.twig" class="%flagbit_currency.twig.class%" public="false">
<argument type="service" id="flagbit_currency"/>
<tag name="twig.extension"/>
</service>
<service id="flagbit_currency" alias="Flagbit\Bundle\CurrencyBundle\Service\Currency" public="true" />
</services>

</container>
4 changes: 3 additions & 1 deletion Service/Currency.php
Expand Up @@ -19,7 +19,7 @@ class Currency
/**
*
* @param CurrencyBundleInterface $currencyBundle
* @param string $defaultCurrency ISO 4217 currency code
* @param string $defaultCurrency ISO 4217 currency code
*/
public function __construct(CurrencyBundleInterface $currencyBundle, $defaultCurrency)
{
Expand All @@ -29,6 +29,7 @@ public function __construct(CurrencyBundleInterface $currencyBundle, $defaultCur

/**
* @param string $currency ISO 4217 currency code
*
* @return string Name like "Euro" or "Schweizer Franken"
*/
public function getCurrencyName($currency = null)
Expand All @@ -42,6 +43,7 @@ public function getCurrencyName($currency = null)

/**
* @param string $currency ISO 4217 currency code
*
* @return string Symbol like "€" or "CHF"
*/
public function getCurrencySymbol($currency = null)
Expand Down
39 changes: 20 additions & 19 deletions Tests/DependencyInjection/ConfigurationTest.php
Expand Up @@ -3,9 +3,10 @@
namespace Flagbit\Bundle\CurrencyBundle\Tests;

use Flagbit\Bundle\CurrencyBundle\DependencyInjection\Configuration;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Processor;

class ConfigurationTest extends \PHPUnit_Framework_TestCase
class ConfigurationTest extends TestCase
{
protected function process($config)
{
Expand All @@ -16,19 +17,19 @@ protected function process($config)

public function provideValidCurrency()
{
return array(
array('EUR'),
array('USD'),
array('CHF'),
);
return [
['EUR'],
['USD'],
['CHF'],
];
}

/**
* Test Default Currency to prevent BC breaks
*/
public function testProcessedDefaultCurrency()
{
$config = array(array());
$config = [[]];

$config = $this->process($config);

Expand All @@ -40,11 +41,11 @@ public function testProcessedDefaultCurrency()
*/
public function testValidDefaultCurrency($currency)
{
$config = array(
array(
$config = [
[
'default_currency' => $currency,
)
);
]
];

$config = $this->process($config);

Expand All @@ -53,10 +54,10 @@ public function testValidDefaultCurrency($currency)

public function provideInvalidCurrency()
{
return array(
array('foobar'),
array('eur'),
);
return [
['foobar'],
['eur'],
];
}

/**
Expand All @@ -65,11 +66,11 @@ public function provideInvalidCurrency()
*/
public function testInvalidDefaultCurrency($currency)
{
$config = array(
array(
$config = [
[
'default_currency' => $currency,
)
);
]
];

$this->process($config);
}
Expand Down
36 changes: 28 additions & 8 deletions Tests/DependencyInjection/FlagbitCurrencyExtensionTest.php
Expand Up @@ -3,29 +3,49 @@
namespace Flagbit\Bundle\CurrencyBundle\Tests;

use Flagbit\Bundle\CurrencyBundle\DependencyInjection\FlagbitCurrencyExtension;
use Flagbit\Bundle\CurrencyBundle\Service\Currency;
use Flagbit\Bundle\CurrencyBundle\Twig\FlagbitCurrencyExtension as TwigExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Intl\ResourceBundle\CurrencyBundleInterface;

class FlagbitCurrencyExtensionTest extends \PHPUnit_Framework_TestCase
class FlagbitCurrencyExtensionTest extends TestCase
{
public function testCurrencyServiceIntegration()
{
$container = new ContainerBuilder();
$config = array();
$config = [];

$extension = new FlagbitCurrencyExtension();
$extension->load($config, $container);

$definition = $container->findDefinition('flagbit_currency.twig');
$definition->setPublic(true);
$this->setDefinitionPublic(TwigExtension::class, $container);
$this->setDefinitionPublic(Currency::class, $container);

$container->compile();

$currency = $container->get(Currency::class);
$currencyExtension = $container->get(TwigExtension::class);

$this->assertInstanceOf(Currency::class, $currency);
$this->assertInstanceOf(TwigExtension::class, $currencyExtension);

$currencyBundle = $container->get('flagbit_currency.intl_bundle');
$currency = $container->get('flagbit_currency');
$currencyExtension = $container->get('flagbit_currency.twig');

$this->assertInstanceOf('Symfony\Component\Intl\ResourceBundle\CurrencyBundleInterface', $currencyBundle);
$this->assertInstanceOf('Flagbit\Bundle\CurrencyBundle\Service\Currency', $currency);
$this->assertInstanceOf('Flagbit\Bundle\CurrencyBundle\Twig\FlagbitCurrencyExtension', $currencyExtension);
$this->assertInstanceOf(CurrencyBundleInterface::class, $currencyBundle);
$this->assertInstanceOf(Currency::class, $currency);
$this->assertInstanceOf(TwigExtension::class, $currencyExtension);
}

/**
* @param string $serviceName
* @param ContainerBuilder $container
*/
private function setDefinitionPublic(string $serviceName, ContainerBuilder $container)
{
$container->findDefinition($serviceName)
->setPublic(true);
}
}
3 changes: 2 additions & 1 deletion Tests/Service/CurrencyTest.php
Expand Up @@ -3,9 +3,10 @@
namespace Flagbit\Bundle\CurrencyBundle\Tests\Service;

use Flagbit\Bundle\CurrencyBundle\Service\Currency;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;

class CurrencyTest extends \PHPUnit_Framework_TestCase
class CurrencyTest extends TestCase
{
/**
* @var PHPUnit_Framework_MockObject_MockObject
Expand Down
12 changes: 6 additions & 6 deletions Tests/Twig/Fixtures/functions/currency_name.test
@@ -1,12 +1,12 @@
--TEST--
"currency_name" function
--TEMPLATE--
{{ currency_name() == 'Euro' ? 'OK' : 'KO' }}
{{ currency_name('EUR') == 'Euro' ? 'OK' : 'KO' }}
{{ currency_name('USD') == 'US Dollar' ? 'OK' : 'KO' }}
{{ currency_name() }}
{{ currency_name('EUR') }}
{{ currency_name('USD') == 'US Dollar' or currency_name('USD') == 'US-Dollar' ? 'OK' : 'KO' }}
--DATA--
return array()
return []
--EXPECT--
OK
OK
Euro
Euro
OK
18 changes: 9 additions & 9 deletions Tests/Twig/Fixtures/functions/currency_symbol.test
@@ -1,14 +1,14 @@
--TEST--
"currency_symbol" function
--TEMPLATE--
{{ currency_symbol() == '€' ? 'OK' : 'KO' }}
{{ currency_symbol('EUR') == '€' ? 'OK' : 'KO' }}
{{ currency_symbol('USD') == '$' ? 'OK' : 'KO' }}
{{ currency_symbol('CHF') == 'CHF' ? 'OK' : 'KO' }}
{{ currency_symbol() }}
{{ currency_symbol('EUR') }}
{{ currency_symbol('USD') }}
{{ currency_symbol('CHF') }}
--DATA--
return array()
return []
--EXPECT--
OK
OK
OK
OK
$
CHF
2 changes: 1 addition & 1 deletion Tests/Twig/Fixtures/globals/currency.default.test
Expand Up @@ -3,6 +3,6 @@
--TEMPLATE--
{{ currency.default }}
--DATA--
return array()
return []
--EXPECT--
EUR
6 changes: 3 additions & 3 deletions Tests/Twig/FlagbitCurrencyExtensionTest.php
Expand Up @@ -11,13 +11,13 @@ class FlagbitCurrencyExtensionTest extends Twig_Test_IntegrationTestCase
{
public function getExtensions()
{
return array(
return [
new FlagbitCurrencyExtension(new Currency(Intl::getCurrencyBundle(), 'EUR')),
);
];
}

public function getFixturesDir()
{
return dirname(__FILE__) . '/Fixtures/';
return __DIR__ . '/Fixtures/';
}
}
12 changes: 6 additions & 6 deletions Twig/FlagbitCurrencyExtension.php
Expand Up @@ -38,25 +38,25 @@ public function getFunctions()
{
$currencyService = $this->currency;

return array(
return [
new \Twig_SimpleFunction('currency_name', function ($currency = null) use ($currencyService) {
return $currencyService->getCurrencyName($currency);
}),
new \Twig_SimpleFunction('currency_symbol', function ($currency = null) use ($currencyService) {
return $currencyService->getCurrencySymbol($currency);
}),
);
];
}

/**
* @return array
*/
public function getGlobals()
{
return array(
'currency' => array(
return [
'currency' => [
'default' => $this->currency->getDefaultCurrency(),
),
);
],
];
}
}
16 changes: 8 additions & 8 deletions composer.json
Expand Up @@ -14,19 +14,19 @@
}
],
"require": {
"php": ">=5.5.0",
"php": "^7.0",

"symfony/dependency-injection": "^2.6|^3.0",
"symfony/config": "^2.6|^3.0",
"symfony/http-kernel": "^2.6|^3.0",
"symfony/intl": "^2.6|^3.0"
"symfony/dependency-injection": "^3.4|^4.0",
"symfony/config": "^3.4|^4.0",
"symfony/http-kernel": "^3.4|^4.0",
"symfony/intl": "^3.4|^4.0"
},
"require-dev": {
"ext-intl": "*",

"phpunit/phpunit": "~4.0",
"squizlabs/php_codesniffer": "~1.5",
"twig/twig": "~1.23"
"phpunit/phpunit": "^6.0",
"squizlabs/php_codesniffer": "^3.0",
"twig/twig": "^1.35|^2.4.4"
},
"autoload": {
"psr-0": {
Expand Down

0 comments on commit 42d4508

Please sign in to comment.