Skip to content

Commit

Permalink
Merge pull request #78 from chives/2.1
Browse files Browse the repository at this point in the history
PHP 8 and symfony 5 components support
  • Loading branch information
szymach authored Feb 11, 2021
2 parents 40166d3 + dc4f03c commit 388c4fb
Show file tree
Hide file tree
Showing 27 changed files with 655 additions and 377 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/bin
/vendor
/tests/temp
composer.lock
composer.phar
autoload.php
.phpcs-cache
.phpunit.result.cache
14 changes: 0 additions & 14 deletions .scrutinizer.yml

This file was deleted.

8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ matrix:
- php: 7.1
env:
- COMPOSER_FLAGS='--prefer-lowest'
- php: 7.3
- php: 7.4
- php: 8.0

env:
global:
Expand All @@ -15,4 +16,7 @@ before_script:
- phpenv config-rm xdebug.ini
- COMPOSER_MEMORY_LIMIT=-1 composer update $COMPOSER_FLAGS

script: vendor/bin/phpunit
script:
- vendor/bin/phpcs
- vendor/bin/phpstan analyze -c phpstan.neon
- vendor/bin/phpunit
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static function getSubscribedEvents()
public function preBindParameters(DataSourceEvent\ParametersEventArgs $event)
{
$parameters = $event->getParameters();
if ($parameters instanceof Request) {
if (true === $parameters instanceof Request) {
$event->setParameters($parameters->query->all());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public function hasFieldType($type)
public function getFieldType($type)
{
if (false === array_key_exists($type, $this->fieldTypes)) {
throw new InvalidArgumentException(sprintf('The field type "%s" is not registered within the service container.', $type));
throw new InvalidArgumentException(
sprintf('The field type "%s" is not registered within the service container.', $type)
);
}

return $this->fieldTypes[$type];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function initOptions(FieldTypeInterface $field)
'form_filter' => true,
'form_options' => [],
'form_from_options' => [],
'form_to_options' =>[]
'form_to_options' => []
])
->setDefined([
'form_type',
Expand Down Expand Up @@ -337,7 +337,7 @@ private function getParameterValue(array $array, FieldTypeInterface $field)
}

/**
* @param array &$array
* @param array $array
* @param FieldTypeInterface $field
* @param mixed $value
*/
Expand Down
9 changes: 6 additions & 3 deletions DependencyInjection/Compiler/DataSourcePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public function process(ContainerBuilder $container)
$driverType = $container->getDefinition($driverExtension)->getArgument(0);

$fields = [];
foreach ($container->findTaggedServiceIds('datasource.driver.'.$driverType.'.field') as $serviceId => $tag) {
$fieldTag = 'datasource.driver.' . $driverType . '.field';
foreach ($container->findTaggedServiceIds($fieldTag) as $serviceId => $tag) {
$alias = isset($tag[0]['alias']) ? $tag[0]['alias'] : $serviceId;

$fields[$alias] = new Reference($serviceId);
Expand All @@ -61,7 +62,8 @@ public function process(ContainerBuilder $container)
$container->getDefinition($driverExtension)->replaceArgument(1, $fields);

$fieldSubscribers = [];
foreach ($container->findTaggedServiceIds('datasource.driver.'.$driverType.'.field.subscriber') as $serviceId => $tag) {
$fieldSubscriberTag = 'datasource.driver.' . $driverType . '.field.subscriber';
foreach ($container->findTaggedServiceIds($fieldSubscriberTag) as $serviceId => $tag) {
$alias = isset($tag[0]['alias']) ? $tag[0]['alias'] : $serviceId;

$fieldSubscribers[$alias] = new Reference($serviceId);
Expand All @@ -70,7 +72,8 @@ public function process(ContainerBuilder $container)
$container->getDefinition($driverExtension)->replaceArgument(2, $fieldSubscribers);

$subscribers = [];
foreach ($container->findTaggedServiceIds('datasource.driver.'.$driverType.'.subscriber') as $serviceId => $tag) {
$driverSubscriberTag = 'datasource.driver.' . $driverType . '.subscriber';
foreach ($container->findTaggedServiceIds($driverSubscriberTag) as $serviceId => $tag) {
$alias = isset($tag[0]['alias']) ? $tag[0]['alias'] : $serviceId;

$subscribers[$alias] = new Reference($serviceId);
Expand Down
5 changes: 1 addition & 4 deletions DependencyInjection/Compiler/TemplatePathPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ final class TemplatePathPass implements CompilerPassInterface
public function process(ContainerBuilder $container)
{
$loaderDefinition = $container->getDefinition('twig.loader.filesystem');
if (null === $loaderDefinition) {
return;
}

$reflection = new ReflectionClass(DataSourceBundle::class);
$loaderDefinition->addMethodCall(
'addPath',
[dirname($reflection->getFileName()).'/Resources/views']
[dirname($reflection->getFileName()) . '/Resources/views']
);
}
}
8 changes: 5 additions & 3 deletions DependencyInjection/FSIDataSourceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('datasource.xml');

$this->registerDrivers($loader);
Expand Down Expand Up @@ -65,8 +65,10 @@ private function registerDrivers(LoaderInterface $loader): void
private function registerForAutoconfiguration(ContainerBuilder $container): void
{
$container->registerForAutoconfiguration(DriverFactoryInterface::class)->addTag('datasource.driver.factory');
$container->registerForAutoconfiguration(DriverExtensionInterface::class)->addTag('datasource.driver.extension');
$container->registerForAutoconfiguration(CollectionAbstractField::class)->addTag('datasource.driver.collection.field');
$container->registerForAutoconfiguration(DriverExtensionInterface::class)
->addTag('datasource.driver.extension');
$container->registerForAutoconfiguration(CollectionAbstractField::class)
->addTag('datasource.driver.collection.field');
$container->registerForAutoconfiguration(Collection\FieldEventSubscriberInterface::class)
->addTag('datasource.driver.collection.field.subscriber')
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,158 +23,162 @@
class ConfigurationBuilderTest extends TestCase
{
/**
* @var MockObject
* @var Kernel&MockObject
*/
private $kernel;

/**
* @var MockObject
* @var ConfigurationBuilder
*/
private $subscriber;

public function testSubscribedEvents()
public function testSubscribedEvents(): void
{
$this->assertEquals(
self::assertEquals(
ConfigurationBuilder::getSubscribedEvents(),
[DataSourceEvents::PRE_BIND_PARAMETERS => ['readConfiguration', 1024]]
);
}

public function testReadConfigurationFromOneBundle()
public function testReadConfigurationFromOneBundle(): void
{
$container = $this->createMock(ContainerInterface::class);
$container->expects($this->once())
$container->expects(self::once())
->method('getParameter')
->with('datasource.yaml.main_config')
->willReturn(null)
;
$this->kernel->expects($this->once())->method('getContainer')->willReturn($container);
$this->kernel->expects($this->once())
$this->kernel->expects(self::once())->method('getContainer')->willReturn($container);
$this->kernel->expects(self::once())
->method('getBundles')
->will($this->returnCallback(function(): array {
$bundle = $this->createMock(BundleInterface::class, ['getPath']);
$bundle->expects($this->any())
->method('getPath')
->will($this->returnValue(__DIR__ . '/../../../../Fixtures/FooBundle'));
->willReturnCallback(
function (): array {
$bundle = $this->createMock(BundleInterface::class);
$bundle->method('getPath')->willReturn(__DIR__ . '/../../../../Fixtures/FooBundle');

return [$bundle];
}));
return [$bundle];
}
);

$dataSource = $this->createMock(DataSourceInterface::class);
$dataSource->expects($this->any())->method('getName')->will($this->returnValue('news'));
$dataSource->expects($this->once())->method('addField')->with('title', 'text', 'like', ['label' => 'Title']);
$dataSource->method('getName')->willReturn('news');
$dataSource->expects(self::once())->method('addField')->with('title', 'text', 'like', ['label' => 'Title']);

$this->subscriber->readConfiguration(new ParametersEventArgs($dataSource, []));
}

public function testReadConfigurationFromManyBundles()
public function testReadConfigurationFromManyBundles(): void
{
$container = $this->createMock(ContainerInterface::class);
$container->expects($this->once())
$container->expects(self::once())
->method('getParameter')
->with('datasource.yaml.main_config')
->willReturn(null)
;

$this->kernel->expects($this->once())->method('getContainer')->willReturn($container);
$this->kernel->expects($this->once())
$this->kernel->expects(self::once())->method('getContainer')->willReturn($container);
$this->kernel->expects(self::once())
->method('getBundles')
->will($this->returnCallback(function(): array {
$fooBundle = $this->createMock(BundleInterface::class);
$fooBundle->expects($this->any())
->method('getPath')
->willReturn(__DIR__ . '/../../../../Fixtures/FooBundle');
->willReturnCallback(
function (): array {
$fooBundle = $this->createMock(BundleInterface::class);
$fooBundle->method('getPath')->willReturn(__DIR__ . '/../../../../Fixtures/FooBundle');

$barBundle = $this->createMock(BundleInterface::class);
$barBundle->expects($this->any())
->method('getPath')
->willReturn(__DIR__ . '/../../../../Fixtures/BarBundle')
;
$barBundle = $this->createMock(BundleInterface::class);
$barBundle->method('getPath')->willReturn(__DIR__ . '/../../../../Fixtures/BarBundle');

return [$fooBundle, $barBundle];
}));
return [$fooBundle, $barBundle];
}
);

$dataSource = $this->createMock(DataSourceInterface::class);
$dataSource->expects($this->any())->method('getName')->will($this->returnValue('news'));
$dataSource->method('getName')->willReturn('news');

// 0 - 1 getName() is called
$dataSource->expects($this->at(2))->method('addField')->with('title', 'text', 'like', ['label' => 'News Title']);
$dataSource->expects($this->at(3))->method('addField')->with('author', null, null, []);
$dataSource->expects(self::at(2))->method('addField')->with('title', 'text', 'like', ['label' => 'News Title']);
$dataSource->expects(self::at(3))->method('addField')->with('author', null, null, []);

$this->subscriber->readConfiguration(new ParametersEventArgs($dataSource, []));
}

public function testMainConfigurationOverridesBundles()
public function testMainConfigurationOverridesBundles(): void
{
$container = $this->createMock(ContainerInterface::class);
$container->expects($this->once())
$container->expects(self::once())
->method('getParameter')
->with('datasource.yaml.main_config')
->willReturn(sprintf('%s/../../../../Resources/config/main_directory', __DIR__))
;

$this->kernel->expects($this->once())->method('getContainer')->willReturn($container);
$this->kernel->expects($this->never())->method('getBundles');
$this->kernel->expects(self::once())->method('getContainer')->willReturn($container);
$this->kernel->expects(self::never())->method('getBundles');

$dataSource = $this->createMock(DataSourceInterface::class);
$dataSource->expects($this->any())->method('getName')->will($this->returnValue('news'));
$dataSource->method('getName')->willReturn('news');

// 0 is when getName() is called
$dataSource->expects($this->at(1))->method('addField')->with('title_short', 'text', null, ['label' => 'Short title']);
$dataSource->expects($this->at(2))->method('addField')->with('created_at', 'date', null, ['label' => 'Created at']);
$dataSource->expects(self::at(1))
->method('addField')
->with('title_short', 'text', null, ['label' => 'Short title'])
;

$dataSource->expects(self::at(2))
->method('addField')
->with('created_at', 'date', null, ['label' => 'Created at'])
;

$this->subscriber->readConfiguration(new ParametersEventArgs($dataSource, []));
}

public function testBundleConfigUsedWhenNoFileFoundInMainDirectory()
public function testBundleConfigUsedWhenNoFileFoundInMainDirectory(): void
{
$container = $this->createMock(ContainerInterface::class);
$container->expects($this->once())
$container->expects(self::once())
->method('getParameter')
->with('datasource.yaml.main_config')
->willReturn(sprintf('%s/../../../../Resources/config/main_directory', __DIR__))
;

$this->kernel->expects($this->once())->method('getContainer')->willReturn($container);
$this->kernel->expects($this->once())
$this->kernel->expects(self::once())->method('getContainer')->willReturn($container);
$this->kernel->expects(self::once())
->method('getBundles')
->will($this->returnCallback(function() {
$bundle = $this->createMock(BundleInterface::class);
$bundle->expects($this->any())
->method('getPath')
->willReturn(__DIR__ . '/../../../../Fixtures/FooBundle');
->willReturnCallback(
function (): array {
$bundle = $this->createMock(BundleInterface::class);
$bundle->method('getPath')->willReturn(__DIR__ . '/../../../../Fixtures/FooBundle');

return [$bundle];
}));
return [$bundle];
}
);

$dataSource = $this->createMock(DataSourceInterface::class);
$dataSource->expects($this->any())->method('getName')->will($this->returnValue('user'));
$dataSource->expects($this->once())->method('addField')->with('username', 'text', null, []);
$dataSource->method('getName')->willReturn('user');
$dataSource->expects(self::once())->method('addField')->with('username', 'text', null, []);

$this->subscriber->readConfiguration(new ParametersEventArgs($dataSource, []));
}

public function testExceptionThrownWhenMainConfigPathIsNotADirectory()
public function testExceptionThrownWhenMainConfigPathIsNotADirectory(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('"non existant directory" is not a directory!');
$this->expectExceptionMessage('"non existent directory" is not a directory!');

$container = $this->createMock(ContainerInterface::class);
$container->expects($this->once())
$container->expects(self::once())
->method('getParameter')
->with('datasource.yaml.main_config')
->willReturn('non existant directory')
->willReturn('non existent directory')
;

$this->kernel->expects($this->once())->method('getContainer')->willReturn($container);
$this->kernel->expects(self::once())->method('getContainer')->willReturn($container);

$dataSource = $this->createMock(DataSourceInterface::class);
$dataSource->expects($this->any())->method('getName')->will($this->returnValue('news'));
$dataSource->method('getName')->willReturn('news');

$this->subscriber->readConfiguration(new ParametersEventArgs($dataSource, []));
}

protected function setUp()
protected function setUp(): void
{
$kernelMockBuilder = $this->getMockBuilder(Kernel::class)->setConstructorArgs(['dev', true]);
$kernelMockBuilder->setMethods(
Expand Down
Loading

0 comments on commit 388c4fb

Please sign in to comment.