Skip to content

Commit

Permalink
Merge pull request #5 from 21TORR/next
Browse files Browse the repository at this point in the history
Add proper autogeneration of aliases in `BundleExtension`
  • Loading branch information
apfelbox authored Nov 26, 2020
2 parents 4cae8a3 + 49a2623 commit dc78c92
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2.1.0
=====

* (feature) Add proper autogeneration of aliases in `BundleExtension`.


2.0.0
=====

Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
"Torr\\BundleHelpers\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\Torr\\BundleHelpers\\": "tests/"
}
},
"scripts": {
"post-install-cmd": [
"@composer bin all install --ansi"
Expand Down
26 changes: 26 additions & 0 deletions phpunit.xml
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="https://schema.phpunit.de/9.3/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
<server name="SYMFONY_PHPUNIT_VERSION" value="9.3" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[direct]=0"/>
</php>

<testsuites>
<testsuite name="Bundle tests">
<directory>tests</directory>
<exclude>tests/fixtures</exclude>
</testsuite>
</testsuites>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
</phpunit>
20 changes: 19 additions & 1 deletion src/Bundle/BundleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace Torr\BundleHelpers\Bundle;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Torr\BundleHelpers\Exception\BundleHelpersException;

/**
* Base bundle extension, that can be used without needing a custom class.
Expand Down Expand Up @@ -47,6 +49,22 @@ public function load (array $configs, ContainerBuilder $container) : void
*/
public function getAlias () : string
{
return $this->alias ?? parent::getAlias();
if (null !== $this->alias)
{
return $this->alias;
}

$className = \get_class($this->bundle);

if ('Bundle' !== \substr($className, -6))
{
throw new BundleHelpersException(\sprintf(
"The bundle does not follow the naming convention; you must pass an explicit alias. Its name should end on 'Bundle', but it is '%s'.",
$className
));
}
$classBaseName = \substr(\strrchr($className, '\\'), 1, -6);

return Container::underscore($classBaseName);
}
}
7 changes: 7 additions & 0 deletions src/Exception/BundleHelpersException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types=1);

namespace Torr\BundleHelpers\Exception;

final class BundleHelpersException extends \Exception
{
}
36 changes: 36 additions & 0 deletions tests/Bundle/BundleExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types=1);

namespace Tests\Torr\BundleHelpers\Bundle;

use PHPUnit\Framework\TestCase;
use Tests\Torr\BundleHelpers\Fixtures\ExampleBundle;
use Tests\Torr\BundleHelpers\Fixtures\OtherExample;
use Torr\BundleHelpers\Bundle\BundleExtension;
use Torr\BundleHelpers\Exception\BundleHelpersException;

final class BundleExtensionTest extends TestCase
{
/**
*
*/
public function testAliasGeneration () : void
{
$extension = new BundleExtension(new ExampleBundle());
$extensionOverwritten = new BundleExtension(new ExampleBundle(), "overwritten");

self::assertSame("example", $extension->getAlias());
self::assertSame("overwritten", $extensionOverwritten->getAlias());
}

/**
*
*/
public function testInvalidAliasGeneration () : void
{
$this->expectException(BundleHelpersException::class);
$this->expectExceptionMessage("The bundle does not follow the naming convention; you must pass an explicit alias.");

$extension = new BundleExtension(new OtherExample());
$extension->getAlias();
}
}
9 changes: 9 additions & 0 deletions tests/Fixtures/ExampleBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace Tests\Torr\BundleHelpers\Fixtures;

use Symfony\Component\HttpKernel\Bundle\Bundle;

final class ExampleBundle extends Bundle
{
}
12 changes: 12 additions & 0 deletions tests/Fixtures/OtherExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

namespace Tests\Torr\BundleHelpers\Fixtures;

use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* Should specifically not end in "bundle"
*/
final class OtherExample extends Bundle
{
}

0 comments on commit dc78c92

Please sign in to comment.