Skip to content

Commit

Permalink
Add string tester integration
Browse files Browse the repository at this point in the history
No selector yet, just make the first two testers
available through the bundle / dependency injection.
  • Loading branch information
iquito committed Nov 17, 2019
1 parent 56be00e commit ee14f6d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
8 changes: 4 additions & 4 deletions captainhook.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@
"conditions": []
},
{
"action": "vendor/bin/phpunit",
"action": "composer phpunit",
"options": [],
"conditions": []
},
{
"action": "vendor/bin/phpstan analyse src --level=7",
"action": "composer phpstan",
"options": [],
"conditions": []
},
{
"action": "vendor/bin/psalm",
"action": "composer psalm",
"options": [],
"conditions": []
},
{
"action": "vendor/bin/phpcs --standard=ruleset.xml --extensions=php --cache src tests",
"action": "composer phpcs",
"options": [],
"conditions": []
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"require": {
"php": "^7.2",
"symfony/dependency-injection": "^4.0",
"squirrelphp/strings": "^0.8"
"squirrelphp/strings": "^0.8.2"
},
"require-dev": {
"captainhook/plugin-composer": "^4.0",
Expand Down
41 changes: 41 additions & 0 deletions src/DependencyInjection/Compiler/StringTesterPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Squirrel\StringsBundle\DependencyInjection\Compiler;

use Squirrel\Strings\Tester\ValidDateTimeTester;
use Squirrel\Strings\Tester\ValidUTF8Tester;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

/**
* Add string testers to service container
*/
class StringTesterPass implements CompilerPassInterface
{
/**
* @var array
*/
private $defaultTesters = [
ValidUTF8Tester::class,
ValidDateTimeTester::class,
];

public function process(ContainerBuilder $container): void
{
// Add all default filters to service container
foreach ($this->defaultTesters as $name => $class) {
if (\is_int($name)) {
$name = \str_replace('Squirrel\\Strings\\Tester\\', '', $class);
$name = \preg_replace('/Tester$/si', '', $name);
}

$definition = new Definition($class);
$definition->addTag('squirrel.strings.tester', [
'tester' => $name,
]);

$container->setDefinition($class, $definition);
}
}
}
2 changes: 2 additions & 0 deletions src/SquirrelStringsBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Squirrel\StringsBundle\DependencyInjection\Compiler\ExtensionPass;
use Squirrel\StringsBundle\DependencyInjection\Compiler\RandomStringGeneratorPass;
use Squirrel\StringsBundle\DependencyInjection\Compiler\StringFilterPass;
use Squirrel\StringsBundle\DependencyInjection\Compiler\StringTesterPass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
Expand All @@ -19,6 +20,7 @@ public function build(ContainerBuilder $container): void
// Set priorities in order and make it above zero, so our compiler passes are run before the ones
// from Symfony (form component and twig component) are run
$container->addCompilerPass(new StringFilterPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 50);
$container->addCompilerPass(new StringTesterPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 50);
$container->addCompilerPass(new RandomStringGeneratorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 49);
$container->addCompilerPass(new ExtensionPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 48);
}
Expand Down
32 changes: 32 additions & 0 deletions tests/StringTesterPassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Squirrel\StringsBundle\Tests;

use Squirrel\Strings\Tester\ValidDateTimeTester;
use Squirrel\Strings\Tester\ValidUTF8Tester;
use Squirrel\StringsBundle\DependencyInjection\Compiler\StringTesterPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class StringTesterPassTest extends \PHPUnit\Framework\TestCase
{
public function testDefault()
{
$container = new ContainerBuilder();

$this->processCompilerPass($container);

// service container + 2 default testers
$this->assertEquals(3, \count($container->getDefinitions()));

// Make sure all definitions exist that we expect
$this->assertTrue($container->hasDefinition(ValidUTF8Tester::class));
$this->assertTrue($container->hasDefinition(ValidDateTimeTester::class));

$container->compile();
}

protected function processCompilerPass(ContainerBuilder $container)
{
(new StringTesterPass())->process($container);
}
}

0 comments on commit ee14f6d

Please sign in to comment.