diff --git a/CHANGELOG.md b/CHANGELOG.md index d091d35..b738c18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ # Changelog -## Unreleased +## 1.0.0 * Initial version of the `rdrenth/twig-extension-bundle` bundle. \ No newline at end of file diff --git a/README.md b/README.md index 59adbf1..f6d069c 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,125 @@ RdrenthTwigExtensionBundle ============= +## About +This is a Symfony2 Bundle that provides you with some extensions to Twig! + +## Twig extensions + +### Stringy +This extension provides the following filters (provided by the [Stringy](https://github.com/danielstjules/Stringy) package). + +For more information about each filter, please check the links. + +#### [ascii](https://github.com/danielstjules/Stringy#toascii) + +```twig +{{ 'fòôbàř'|ascii }} {# foobar #} +``` + +#### [camelize](https://github.com/danielstjules/Stringy#camelize) + +```twig +{{ 'Camel-Case'|camelize }} {# camelCase #} +``` + +#### [dasherize](https://github.com/danielstjules/Stringy#dasherize) + +```twig +{{ 'fooBar'|dasherize }} {# foo-bar #} +``` + +#### [delimit](https://github.com/danielstjules/Stringy#delimit) + +```twig +{{ 'fooBar'|delimit('::') }} {# foo::bar #} +``` + +#### [humanize](https://github.com/danielstjules/Stringy#humanize) + +```twig +{{ 'author_id'|humanize }} {# Author #} +``` + +#### [slugify](https://github.com/danielstjules/Stringy#slugify-string-replacement----) + +```twig +{{ 'Using strings like fòô bàř'| slugify }} {# using-strings-like-foo-bar #} +``` + +#### [titleize](https://github.com/danielstjules/Stringy#titleize-array-ignore) + +```twig +{{ 'i like to watch television'|titleize(['to']) }} {# I Like to Watch Television #} +``` + +#### [underscored](https://github.com/danielstjules/Stringy#underscored) + +```twig +{{ 'TestUCase'|underscored }} {# test_u_case #} +``` + + +## Installation +### Step 1: Install RdrenthTwigExtensionBundle using [Composer](http://getcomposer.org) + +```bash +$ composer require rdrenth/twig-extension-bundle +``` +### Step 2: Enable the bundle +```php + + * @license http://opensource.org/licenses/MIT The MIT License (MIT) + * @link https://github.com/rdrenth/twig-extension-bundle + */ +class ConfigurationTest extends \PHPUnit_Framework_TestCase +{ + /** + * @return array + */ + public function provideStringyFilters() + { + return array( + 'ascii' => 'toAscii', + 'camelize' => 'camelize', + 'dasherize' => 'dasherize', + 'delimit' => 'delimit', + 'humanize' => 'humanize', + 'slugify' => 'slugify', + 'titleize' => 'titleize', + 'underscored' => 'underscored', + ); + } + + /** + * Test to make sure the default configuration is correctly processed + */ + public function testDefaultConfigurationProcessing() + { + $rawConfig = Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/default.yml')); + $config = $this->processConfiguration($rawConfig); + + $this->assertArrayHasKey('stringy', $config); + } + + /** + * Test to make sure the stringy section of the configuration is correctly processed + */ + public function testStringyConfigurationProcessing() + { + $rawConfig = Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/stringy.yml')); + $config = $this->processConfiguration($rawConfig); + + $this->assertArrayHasKey('stringy', $config); + $config = $config['stringy']; + + $this->assertArrayHasKey('enabled', $config); + $this->assertArrayHasKey('encoding', $config); + $this->assertArrayHasKey('filters', $config); + $this->assertArrayHasKey('extra_filters', $config); + + $filters = $config['filters']; + foreach ($this->provideStringyFilters() as $filterName => $methodName) { + $this->assertStringyFilter($filters, $filterName, $filterName, $methodName, true); + } + + $this->assertStringyFilter($config['extra_filters'], 0, 'swap_case', 'swapCase'); + } + + /** + * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException + */ + public function testInvalidStringyMethod() + { + $rawConfig = Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/stringy-invalid.yml')); + $this->processConfiguration($rawConfig); + } + + /** + * @param array $filters + * @param mixed $indexName + * @param string $filterName + * @param string $methodName + * @param bool|null $enabled + */ + private function assertStringyFilter(array $filters, $indexName, $filterName, $methodName, $enabled = null) + { + if ($enabled !== null) { + $this->assertEquals($enabled, $filters[$indexName]['enabled']); + } + + $this->assertEquals($filterName, $filters[$indexName]['filter']); + $this->assertEquals($methodName, $filters[$indexName]['method']); + } + + /** + * Processes an array of raw configuration and returns a compiled version. + * + * @param array $config + * @return array + */ + private function processConfiguration(array $config) + { + $processor = new Processor(); + + return $processor->processConfiguration(new Configuration(), $config); + } +} diff --git a/Tests/DependencyInjection/ExtensionTest.php b/Tests/DependencyInjection/ExtensionTest.php new file mode 100644 index 0000000..c063d46 --- /dev/null +++ b/Tests/DependencyInjection/ExtensionTest.php @@ -0,0 +1,47 @@ + + * @license http://opensource.org/licenses/MIT The MIT License (MIT) + * @link https://github.com/rdrenth/twig-extension-bundle + */ +class ExtenionTest extends \PHPUnit_Framework_TestCase +{ + /** + * Test DI container with default config + */ + public function testDefaultContainer() + { + $config = Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/default.yml')); + $container = $this->getContainer($config); + + $this->assertTrue($container->hasDefinition('rdrenth_twig_extension.stringy')); + + $this->assertEquals( + $container->getParameterBag()->resolveValue('%rdrenth_twig_extension.stringy.class%'), + $container->getDefinition('rdrenth_twig_extension.stringy')->getClass() + ); + } + + /** + * @param array $config + * @return ContainerBuilder + */ + private function getContainer(array $config) + { + $container = new ContainerBuilder(); + $loader = new RdrenthTwigExtensionExtension(); + $loader->load($config, $container); + $container->compile(); + + return $container; + } +} diff --git a/Tests/Resources/config/default.yml b/Tests/Resources/config/default.yml new file mode 100644 index 0000000..6fc3f9e --- /dev/null +++ b/Tests/Resources/config/default.yml @@ -0,0 +1 @@ +rdrenth_twig_extension: ~ \ No newline at end of file diff --git a/Tests/Resources/config/stringy-invalid.yml b/Tests/Resources/config/stringy-invalid.yml new file mode 100644 index 0000000..dc1babd --- /dev/null +++ b/Tests/Resources/config/stringy-invalid.yml @@ -0,0 +1,5 @@ +rdrenth_twig_extension: + stringy: + filters: + ascii: + method: toAsciiMethodThatDoesNotExistAtAll \ No newline at end of file diff --git a/Tests/Resources/config/stringy.yml b/Tests/Resources/config/stringy.yml new file mode 100644 index 0000000..96ae9f1 --- /dev/null +++ b/Tests/Resources/config/stringy.yml @@ -0,0 +1,13 @@ +rdrenth_twig_extension: + stringy: + filters: + ascii: ~ + camelize: ~ + dasherize: ~ + delimit: ~ + humanize: ~ + slugify: ~ + titleize: ~ + underscored: ~ + extra_filters: + - { filter: swap_case, method: swapCase } \ No newline at end of file diff --git a/Tests/Twig/StringyExtensionTest.php b/Tests/Twig/StringyExtensionTest.php new file mode 100644 index 0000000..d5f2105 --- /dev/null +++ b/Tests/Twig/StringyExtensionTest.php @@ -0,0 +1,139 @@ + + * @license http://opensource.org/licenses/MIT The MIT License (MIT) + * @link https://github.com/rdrenth/twig-extension-bundle + */ +class StringyExtensionTest extends \PHPUnit_Framework_TestCase +{ + /** + * @type \Twig_Environment + */ + private $twig; + + /** + * @type array + */ + private $twigTemplates = array( + 'ascii' => "{{ value|ascii(true) }}", + 'camelize' => "{{ value|camelize }}", + 'dasherize' => "{{ value|dasherize }}", + 'delimit' => "{{ value|delimit('::') }}", + 'humanize' => "{{ value|humanize }}", + 'slugify' => "{{ value|slugify('-') }}", + 'titleize' => "{{ value|titleize(['to', 'at']) }}", + 'underscored' => "{{ value|underscored }}", + 'swap_case' => "{{ value|swap_case }}", + ); + + /** + * {@inheritdoc} + */ + protected function setUp() + { + $rawConfig = Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/stringy.yml')); + $container = $this->getContainer($rawConfig); + + /** @type StringyExtension $twigExtension */ + $twigExtension = $container->get('rdrenth_twig_extension.stringy'); + + $this->twig = new \Twig_Environment(new \Twig_Loader_Array($this->twigTemplates)); + $this->twig->addExtension($twigExtension); + } + + public function testAsciiFilter() + { + $this->assertSame( + $this->twig->render('ascii', array('value' => 'fòôbàř')), + 'foobar' + ); + } + + public function testCamelizeFilter() + { + $this->assertSame( + $this->twig->render('camelize', array('value' => 'Camel-Case')), + 'camelCase' + ); + } + + public function testDasherizeFilter() + { + $this->assertSame( + $this->twig->render('dasherize', array('value' => 'fooBar')), + 'foo-bar' + ); + } + + public function testDelimitFilter() + { + $this->assertSame( + $this->twig->render('delimit', array('value' => 'fooBar')), + 'foo::bar' + ); + } + + public function testHumanizeFilter() + { + $this->assertSame( + $this->twig->render('humanize', array('value' => 'author_id')), + 'Author' + ); + } + + public function testSlugifyFilter() + { + $this->assertSame( + $this->twig->render('slugify', array('value' => 'Using strings like fòô bàř')), + 'using-strings-like-foo-bar' + ); + } + + public function testTitleizeFilter() + { + $this->assertSame( + $this->twig->render('titleize', array('value' => 'i like to watch television')), + 'I Like to Watch Television' + ); + } + + public function testUnderscoredFilter() + { + $this->assertSame( + $this->twig->render('underscored', array('value' => 'TestUCase')), + 'test_u_case' + ); + } + + public function testSwapCaseExtraFilter() + { + $this->assertSame( + $this->twig->render('swap_case', array('value' => 'SwapCase')), + 'sWAPcASE' + ); + } + + /** + * @param array $config + * @return ContainerBuilder + */ + private function getContainer(array $config) + { + $container = new ContainerBuilder(); + $loader = new RdrenthTwigExtensionExtension(); + $loader->load($config, $container); + $container->compile(); + + return $container; + } +} diff --git a/Twig/StringyExtension.php b/Twig/StringyExtension.php new file mode 100644 index 0000000..9611a64 --- /dev/null +++ b/Twig/StringyExtension.php @@ -0,0 +1,60 @@ + + * @license http://opensource.org/licenses/MIT The MIT License (MIT) + * @link https://github.com/rdrenth/twig-extension-bundle + */ +class StringyExtension extends \Twig_Extension +{ + /** + * @type array + */ + private $filters; + + /** + * @type string|null + */ + private $encoding; + + /** + * StringyExtension constructor. + * + * @param array $filters + * @param null|string $encoding + */ + public function __construct(array $filters = array(), $encoding = null) + { + $this->filters = $filters; + $this->encoding = $encoding; + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return 'StringyExtension'; + } + + /** + * {@inheritdoc} + */ + public function getFilters() + { + if (empty($this->filters)) { + return array(); + } + + $filters = array(); + foreach ($this->filters as $filterName => $methodName) { + $filters[$filterName] = new \Twig_SimpleFilter($filterName, array('Stringy\StaticStringy', $methodName)); + } + + return $filters; + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index e3ceff6..1bb1a1c 100644 --- a/composer.json +++ b/composer.json @@ -19,9 +19,7 @@ "danielstjules/stringy": "~2.0" }, "require-dev": { - "phpunit/phpunit": "^4.8", - "symfony/finder": "~2.3", - "symfony/browser-kit": "~2.3" + "phpunit/phpunit": "^4.8" }, "autoload": { "psr-0": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a12cd44..5e2e2db 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -3,7 +3,7 @@ - Tests + ./Tests