Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toggle doctrine migration #265

Merged
merged 3 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
"friends-of-behat/variadic-extension": "^1.3",
"friendsofsymfony/oauth-server-bundle": "^1.6 || >2.0.0-alpha.0 ^2.0@dev",
"lakion/mink-debug-extension": "^1.2.3",
"matthiasnoback/symfony-config-test": "^4.0",
"matthiasnoback/symfony-dependency-injection-test": "^4.1",
"phpspec/phpspec": "^6.0",
"phpstan/phpstan": "0.12.29",
"phpstan/phpstan-webmozart-assert": "0.12.6",
Expand Down
51 changes: 23 additions & 28 deletions src/DependencyInjection/SyliusRefundExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sylius\RefundPlugin\DependencyInjection;

use Sylius\Bundle\CoreBundle\DependencyInjection\PrependDoctrineMigrationsTrait;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
Expand All @@ -12,42 +13,36 @@

final class SyliusRefundExtension extends Extension implements PrependExtensionInterface
{
/**
* {@inheritdoc}
*/
use PrependDoctrineMigrationsTrait;

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

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

public function prepend(ContainerBuilder $container): void
{
if (!$container->hasExtension('doctrine_migrations') || !$container->hasExtension('sylius_labs_doctrine_migrations_extra')) {
return;
}

$doctrineConfig = $container->getExtensionConfig('doctrine_migrations');

$migrationsPath = (array) \array_pop($doctrineConfig)['migrations_paths'];
$container->prependExtensionConfig('doctrine_migrations', [
'migrations_paths' => \array_merge(
$migrationsPath ?? [],
[
'Sylius\RefundPlugin\Migrations' => '@SyliusRefundPlugin/Migrations',
]
),
]);

$container->prependExtensionConfig('sylius_labs_doctrine_migrations_extra', [
'migrations' => [
'Sylius\RefundPlugin\Migrations' => [
'Sylius\Bundle\CoreBundle\Migrations',
'Sylius\Bundle\AdminApiBundle\Migrations',
],
],
]);
$this->prependDoctrineMigrations($container);
}

protected function getMigrationsNamespace(): string
{
return 'Sylius\RefundPlugin\Migrations';
}

protected function getMigrationsDirectory(): string
{
return '@SyliusRefundPlugin/Migrations';
}

protected function getNamespacesOfMigrationsExecutedBefore(): array
{
return [
'Sylius\Bundle\CoreBundle\Migrations',
'Sylius\Bundle\AdminApiBundle\Migrations',
];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
];
];

}
}
2 changes: 1 addition & 1 deletion tests/Application/config/packages/doctrine_migrations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ doctrine_migrations:
table_storage:
table_name: sylius_migrations
migrations_paths:
'App\Migrations': '%kernel.project_dir%/src/Migrations/'
'DoctrineMigrations': 'src/Migrations'
119 changes: 119 additions & 0 deletions tests/DependencyInjection/SyliusRefundExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Tests\Sylius\RefundPlugin\DependencyInjection;

use Doctrine\Bundle\MigrationsBundle\DependencyInjection\DoctrineMigrationsExtension;
use FOS\OAuthServerBundle\DependencyInjection\FOSOAuthServerExtension;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use Sylius\RefundPlugin\DependencyInjection\SyliusRefundExtension;
use SyliusLabs\DoctrineMigrationsExtraBundle\DependencyInjection\SyliusLabsDoctrineMigrationsExtraExtension;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;

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

$this->customLoad();

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

self::assertTrue(isset(
$doctrineMigrationsExtensionConfig[0]['migrations_paths']['Sylius\RefundPlugin\Migrations']
));
self::assertSame(
'@SyliusRefundPlugin/Migrations',
$doctrineMigrationsExtensionConfig[0]['migrations_paths']['Sylius\RefundPlugin\Migrations']
);

$syliusLabsDoctrineMigrationsExtraExtensionConfig = $this
->container
->getExtensionConfig('sylius_labs_doctrine_migrations_extra')
;

self::assertTrue(isset(
$syliusLabsDoctrineMigrationsExtraExtensionConfig[0]['migrations']['Sylius\RefundPlugin\Migrations']
));
self::assertSame(
'Sylius\Bundle\CoreBundle\Migrations',
$syliusLabsDoctrineMigrationsExtraExtensionConfig[0]['migrations']['Sylius\RefundPlugin\Migrations'][0]
);
self::assertSame(
'Sylius\Bundle\AdminApiBundle\Migrations',
$syliusLabsDoctrineMigrationsExtraExtensionConfig[0]['migrations']['Sylius\RefundPlugin\Migrations'][1]
);
}

/**
* @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();

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

self::assertEmpty($doctrineMigrationsExtensionConfig);

$syliusLabsDoctrineMigrationsExtraExtensionConfig = $this
->container
->getExtensionConfig('sylius_labs_doctrine_migrations_extra')
;

self::assertEmpty($syliusLabsDoctrineMigrationsExtraExtensionConfig);
}

protected function getContainerExtensions(): array
{
return [new SyliusRefundExtension()];
}

private function configureContainer(): void
{
$this->container->setParameter('kernel.environment', 'test');
$this->container->setParameter('kernel.debug', true);

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

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

$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);
}
}
}
}