Skip to content

Commit

Permalink
bug #277 [DependencyInjection] Move registering resources to prepend …
Browse files Browse the repository at this point in the history
…method (GSadee)

This PR was merged into the 1.0-dev branch.

Discussion
----------

Before this change, resources and their parameters were loaded too late and they cannot be used properly for example in a grid or state machine configuration.

Commits
-------

0887444 [DependencyInjection] Move setting parameters and registering resources to prepend method
e8a6bcf [DependencyInjection] Revert moving setting parameters to prepend method
  • Loading branch information
AdamKasp authored Apr 25, 2022
2 parents b2c69c8 + e8a6bcf commit 32d02a7
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 28 deletions.
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ parameters:
- 'tests/Application/src/**.php'

ignoreErrors:
- '/Parameter #1 \$configuration of method Symfony\\Component\\DependencyInjection\\Extension\\Extension::processConfiguration\(\) expects Symfony\\Component\\Config\\Definition\\ConfigurationInterface, Symfony\\Component\\Config\\Definition\\ConfigurationInterface\|null given\./'
- '/Method Sylius\\InvoicingPlugin\\Entity\\\w+::id\(\) has no return typehint specified\./'
- '/Method Sylius\\InvoicingPlugin\\Entity\\\w+::getId\(\) has no return typehint specified\./'
- '/expects string, string\|null given\.$/'
23 changes: 19 additions & 4 deletions src/DependencyInjection/SyliusInvoicingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Sylius\Bundle\CoreBundle\DependencyInjection\PrependDoctrineMigrationsTrait;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Extension\AbstractResourceExtension;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
Expand All @@ -26,18 +27,22 @@ final class SyliusInvoicingExtension extends AbstractResourceExtension implement

public function load(array $config, ContainerBuilder $container): void
{
$config = $this->processConfiguration($this->getConfiguration([], $container), $config);
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));

$this->registerResources('sylius_invoicing_plugin', 'doctrine/orm', $config['resources'], $container);

$loader->load('services.xml');

/** @var ConfigurationInterface $configuration */
$configuration = $this->getConfiguration([], $container);

$config = $this->processConfiguration($configuration, $config);
$container->setParameter('sylius_invoicing.pdf_generator.allowed_files', $config['pdf_generator']['allowed_files']);
}

public function prepend(ContainerBuilder $container): void
{
$config = $this->getCurrentConfiguration($container);

$this->registerResources('sylius_invoicing_plugin', 'doctrine/orm', $config['resources'], $container);

$this->prependDoctrineMigrations($container);
}

Expand All @@ -55,4 +60,14 @@ protected function getNamespacesOfMigrationsExecutedBefore(): array
{
return ['Sylius\Bundle\CoreBundle\Migrations'];
}

private function getCurrentConfiguration(ContainerBuilder $container): array
{
/** @var ConfigurationInterface $configuration */
$configuration = $this->getConfiguration([], $container);

$configs = $container->getExtensionConfig($this->getAlias());

return $this->processConfiguration($configuration, $configs);
}
}
2 changes: 1 addition & 1 deletion src/Resources/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ sylius_grid:
driver:
name: doctrine/orm
options:
class: 'expr:parameter("sylius_invoicing_plugin.model.invoice.class")'
class: '%sylius_invoicing_plugin.model.invoice.class%'
sorting:
issuedAt: desc
fields:
Expand Down
141 changes: 119 additions & 22 deletions tests/DependencyInjection/SyliusInvoicingExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,26 @@
namespace Tests\Sylius\InvoicingPlugin\DependencyInjection;

use Doctrine\Bundle\MigrationsBundle\DependencyInjection\DoctrineMigrationsExtension;
use FOS\OAuthServerBundle\DependencyInjection\FOSOAuthServerExtension;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\InvoicingPlugin\DependencyInjection\SyliusInvoicingExtension;
use Sylius\InvoicingPlugin\Entity\BillingData;
use Sylius\InvoicingPlugin\Entity\Invoice;
use Sylius\InvoicingPlugin\Entity\InvoiceSequence;
use Sylius\InvoicingPlugin\Entity\InvoiceShopBillingData;
use Sylius\InvoicingPlugin\Entity\LineItem;
use Sylius\InvoicingPlugin\Entity\TaxItem;
use SyliusLabs\DoctrineMigrationsExtraBundle\DependencyInjection\SyliusLabsDoctrineMigrationsExtraExtension;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;

class SyliusInvoicingExtensionTest extends AbstractExtensionTestCase
{
/**
* @test
*/
/** @test */
public function it_autoconfigures_prepending_doctrine_migration_with_proper_migrations_paths(): void
{
$this->configureContainer();

$this->customLoad();
$this->prepend();

$doctrineMigrationsExtensionConfig = $this->container->getExtensionConfig('doctrine_migrations');

Expand All @@ -55,16 +59,14 @@ public function it_autoconfigures_prepending_doctrine_migration_with_proper_migr
);
}

/**
* @test
*/
/** @test */
public function it_does_not_autoconfigure_prepending_doctrine_migrations_if_it_is_disabled(): void
{
$this->configureContainer();

$this->container->setParameter('sylius_core.prepend_doctrine_migrations', false);

$this->customLoad();
$this->prepend();

$doctrineMigrationsExtensionConfig = $this->container->getExtensionConfig('doctrine_migrations');

Expand All @@ -78,6 +80,113 @@ public function it_does_not_autoconfigure_prepending_doctrine_migrations_if_it_i
self::assertEmpty($syliusLabsDoctrineMigrationsExtraExtensionConfig);
}

/** @test */
public function it_loads_allowed_files_for_pdf_generator_configuration(): void
{
$this->load(['pdf_generator' => ['allowed_files' => ['swans.png', 'product.png']]]);

$this->assertContainerBuilderHasParameter(
'sylius_invoicing.pdf_generator.allowed_files',
['swans.png', 'product.png']
);
}

/** @test */
public function it_prepends_configuration_with_invoice_resource_services(): void
{
$this->prepend();

$this->assertContainerBuilderHasParameter(
'sylius_invoicing_plugin.model.invoice.class',
Invoice::class
);

$this->assertContainerBuilderHasService(
'sylius_invoicing_plugin.controller.invoice',
ResourceController::class
);
}

/** @test */
public function it_prepends_configuration_with_billing_data_resource_services(): void
{
$this->prepend();

$this->assertContainerBuilderHasParameter(
'sylius_invoicing_plugin.model.billing_data.class',
BillingData::class
);

$this->assertContainerBuilderHasService(
'sylius_invoicing_plugin.controller.billing_data',
ResourceController::class
);
}

/** @test */
public function it_prepends_configuration_with_shop_billing_data_resource_services(): void
{
$this->prepend();

$this->assertContainerBuilderHasParameter(
'sylius_invoicing_plugin.model.shop_billing_data.class',
InvoiceShopBillingData::class
);

$this->assertContainerBuilderHasService(
'sylius_invoicing_plugin.controller.shop_billing_data',
ResourceController::class
);
}

/** @test */
public function it_prepends_configuration_with_line_item_resource_services(): void
{
$this->prepend();

$this->assertContainerBuilderHasParameter(
'sylius_invoicing_plugin.model.line_item.class',
LineItem::class
);

$this->assertContainerBuilderHasService(
'sylius_invoicing_plugin.controller.line_item',
ResourceController::class
);
}

/** @test */
public function it_prepends_configuration_with_tax_item_resource_services(): void
{
$this->prepend();

$this->assertContainerBuilderHasParameter(
'sylius_invoicing_plugin.model.tax_item.class',
TaxItem::class
);

$this->assertContainerBuilderHasService(
'sylius_invoicing_plugin.controller.tax_item',
ResourceController::class
);
}

/** @test */
public function it_prepends_configuration_with_invoice_sequence_resource_services(): void
{
$this->prepend();

$this->assertContainerBuilderHasParameter(
'sylius_invoicing_plugin.model.invoice_sequence.class',
InvoiceSequence::class
);

$this->assertContainerBuilderHasService(
'sylius_invoicing_plugin.controller.invoice_sequence',
ResourceController::class
);
}

protected function getContainerExtensions(): array
{
return [new SyliusInvoicingExtension()];
Expand All @@ -90,26 +199,14 @@ private function configureContainer(): void

$this->container->registerExtension(new DoctrineMigrationsExtension());
$this->container->registerExtension(new SyliusLabsDoctrineMigrationsExtraExtension());
$this->container->registerExtension(new FOSOAuthServerExtension());
}

private function customLoad(): void
private function prepend(): void
{
$configurationValues = ['sylius_invoicing' => []];

$configs = [$this->getMinimalConfiguration(), $configurationValues];

foreach ($this->container->getExtensions() as $extension) {
if ($extension instanceof PrependExtensionInterface) {
$extension->prepend($this->container);
}
}

foreach ($this->container->getExtensions() as $extension) {
$extensionAlias = $extension->getAlias();
if (isset($configs[$extensionAlias])) {
$extension->load($configs[$extensionAlias], $this->container);
}
}
}
}

0 comments on commit 32d02a7

Please sign in to comment.