Skip to content

Commit

Permalink
Created initial bundle (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddypouw authored and FreddyOffenga committed Nov 28, 2017
1 parent ceb5c7a commit 5645621
Show file tree
Hide file tree
Showing 14 changed files with 348 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
composer.lock
/var
29 changes: 29 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
language: php

matrix:
include:
# PHP tests
- language: php
php: 7.1
env: COMPOSER_FLAGS="--prefer-lowest"
before_script:
- composer update $COMPOSER_FLAGS --prefer-dist
script: vendor/bin/phpunit
- language: php
php: 7.1
env: COMPOSER_FLAGS="--prefer-stable"
before_script:
- composer update $COMPOSER_FLAGS --prefer-dist
script: vendor/bin/phpunit
- language: php
php: 7.1
env: COMPOSER_FLAGS=""
before_script:
- composer update $COMPOSER_FLAGS --prefer-dist
script: vendor/bin/phpunit
- language: php
php: 7.1
env: SYMFONY_VERSION="4.0.*"
before_script:
- composer update $COMPOSER_FLAGS --prefer-dist
script: vendor/bin/phpunit
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,52 @@
# financial-twig-extension-bundle
A simple Symfony Bundle that contains twig extensions for displaying financial information
A simple Symfony Bundle that contains twig extensions for
displaying financial information

Installation
------------
Install the latest version via [composer](https://getcomposer.org/):
```bash
php composer.phar require hostnet/financial-twig-extension-bundle
```

Then add the bundle to your AppKernel bundles:
```php
<?php
// app/AppKernel.php

public function registerBundles()
{
$bundles = array(
// ...
new \Hostnet\Bundle\FinancialTwigExtensionBundle\Bundle\HostnetFinancialTwigExtensionBundle(),
// ...
);
}
```

Usage
-----
Currently, we support formatting International Bank Account
Numbers (IBAN) for displaying on a page (a space for every four characters):
```twig
{{ "NL85INGB0008523141"|formatIban }}
```

This will result in the following output:
```text
NL85 INGB 0008 5231 41
```

Requirements
------------

PHP 7.1.x or above.

License
-------

This library is licensed under the MIT License, meaning you can reuse the code
within proprietary software provided that all copies of the licensed software
include a copy of the MIT License terms and the copyright notice.

For more information, see the [LICENSE](LICENSE) file.
35 changes: 35 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "hostnet/financial-twig-extension-bundle",
"type": "symfony-bundle",
"description": "A simple Symfony Bundle that contains twig extensions for displaying financial information.",
"license": "MIT",
"require": {
"php": "^7.1",
"symfony/framework-bundle": "^3.3|^4.0"
},
"require-dev": {
"hostnet/phpcs-tool": "^5.2",
"phpunit/phpunit": "^6.3",
"symfony/twig-bundle": "^3.3|^4.0",
"symfony/yaml": "^3.3|^4.0",
"twig/twig": "^1.26|^2.0"
},
"conflict": {
"twig/twig": "<1.26"
},
"autoload": {
"psr-4": {
"Hostnet\\Bundle\\FinancialTwigExtensionBundle\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Hostnet\\Bundle\\FinancialTwigExtensionBundle\\": "test/"
}
},
"archive": {
"exclude": [
"/test"
]
}
}
26 changes: 26 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation = "http://schema.phpunit.de/4.7/phpunit.xsd"
colors = "true"
forceCoversAnnotation = "true"
bootstrap = "vendor/autoload.php"
>
<php>
<server name="KERNEL_CLASS" value="Hostnet\Bundle\FinancialTwigExtensionBundle\Functional\Fixtures\TestKernel"/>
</php>
<testsuites>
<testsuite name="unit">
<directory>./test</directory>
<exclude>./test/Functional</exclude>
</testsuite>
<testsuite name="functional">
<directory>./test/Functional</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
</phpunit>
20 changes: 20 additions & 0 deletions src/Bundle/HostnetFinancialTwigExtensionBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* @copyright 2017 Hostnet B.V.
*/
declare(strict_types=1);
namespace Hostnet\Bundle\FinancialTwigExtensionBundle\Bundle;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class HostnetFinancialTwigExtensionBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/Resources'));
$loader->load('services.yml');
}
}
5 changes: 5 additions & 0 deletions src/Bundle/Resources/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
Hostnet\Bundle\FinancialTwigExtensionBundle\Twig\FormatIbanExtension:
public: false
tags:
- twig.extension
24 changes: 24 additions & 0 deletions src/Twig/FormatIbanExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* @copyright 2017 Hostnet B.V.
*/
declare(strict_types=1);
namespace Hostnet\Bundle\FinancialTwigExtensionBundle\Twig;

class FormatIbanExtension extends \Twig_Extension
{
/**
* {@inheritdoc}
*/
public function getFilters()
{
return [
new \Twig_SimpleFilter('formatIban', [$this, 'formatIban'])
];
}

public function formatIban(string $iban): string
{
return implode(' ', str_split(preg_replace('/\s+/', '', $iban), 4));
}
}
38 changes: 38 additions & 0 deletions test/Bundle/HostnetFinancialTwigExtensionBundleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* @copyright 2017 Hostnet B.V.
*/
declare(strict_types=1);
namespace Hostnet\Bundle\FinancialTwigExtensionBundle\Bundle;

use Hostnet\Bundle\FinancialTwigExtensionBundle\Twig\FormatIbanExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

/**
* @covers \Hostnet\Bundle\FinancialTwigExtensionBundle\Bundle\HostnetFinancialTwigExtensionBundle
*/
class HostnetFinancialTwigExtensionBundleTest extends TestCase
{
/**
* @var HostnetFinancialTwigExtensionBundle
*/
private $hostnet_financial_twig_extension_bundle;

protected function setUp()
{
$this->hostnet_financial_twig_extension_bundle = new HostnetFinancialTwigExtensionBundle();
}

public function testBuild()
{
$container = new ContainerBuilder();
$this->hostnet_financial_twig_extension_bundle->build($container);

self::assertInstanceOf(
Definition::class,
$container->getDefinition(FormatIbanExtension::class)
);
}
}
33 changes: 33 additions & 0 deletions test/Functional/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* @copyright 2017 Hostnet B.V.
*/
declare(strict_types=1);

namespace Hostnet\Bundle\FinancialTwigExtensionBundle\Functional;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class ConfigurationTest extends KernelTestCase
{
protected function setUp()
{
static::bootKernel();
}

public function testFormatIban()
{
$container = static::$kernel->getContainer();
$twig = $container->get('twig');

self::assertSame(
'NL85 INGB 0008 5231 41',
$twig->render('/ibantest.html.twig', ['iban' => 'NL85INGB0008523141'])
);

self::assertSame(
'NL85 INGB 0008 5231 41',
$twig->render('/ibantest.html.twig', ['iban' => 'NL8 5I NGB00 085 2314 1'])
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ iban|formatIban }}
43 changes: 43 additions & 0 deletions test/Functional/Fixtures/TestKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* @copyright 2017 Hostnet B.V.
*/
declare(strict_types=1);

namespace Hostnet\Bundle\FinancialTwigExtensionBundle\Functional\Fixtures;

use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;

class TestKernel extends Kernel
{
/**
* {@inheritdoc}
*/
public function registerBundles()
{
return [
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \Symfony\Bundle\TwigBundle\TwigBundle(),
new \Hostnet\Bundle\FinancialTwigExtensionBundle\Bundle\HostnetFinancialTwigExtensionBundle()
];
}

/**
* {@inheritdoc}
*/
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__ . '/config/config.yml');
}

public function getLogDir()
{
return __DIR__ . '/../../../var/log';
}

public function getCacheDir()
{
return __DIR__ . '/../../../var/cache';
}
}
2 changes: 2 additions & 0 deletions test/Functional/Fixtures/config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
framework:
secret: test
38 changes: 38 additions & 0 deletions test/Twig/FormatIbanExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* @copyright 2017 Hostnet B.V.
*/
declare(strict_types=1);
namespace Hostnet\Bundle\FinancialTwigExtensionBundle\Twig;

use PHPUnit\Framework\TestCase;

/**
* @covers \Hostnet\Bundle\FinancialTwigExtensionBundle\Twig\FormatIbanExtension
*/
class FormatIbanExtensionTest extends TestCase
{
/**
* @var FormatIbanExtension
*/
private $format_iban_extension;

protected function setUp()
{
$this->format_iban_extension = new FormatIbanExtension();
}

public function testGetFilters()
{
self::assertEquals(
[new \Twig_SimpleFilter('formatIban', [$this->format_iban_extension, 'formatIban'])],
$this->format_iban_extension->getFilters()
);
}

public function testFormatIban()
{
self::assertSame('NL85 INGB 0008 5231 41', $this->format_iban_extension->formatIban('NL85INGB0008523141'));
self::assertSame('NL85 INGB 0008 5231 41', $this->format_iban_extension->formatIban('N L85IN GB00085 23141'));
}
}

0 comments on commit 5645621

Please sign in to comment.