diff --git a/src/DI/MigrationsExtension.php b/src/DI/MigrationsExtension.php index 8b71779..17dae6a 100644 --- a/src/DI/MigrationsExtension.php +++ b/src/DI/MigrationsExtension.php @@ -38,7 +38,7 @@ public function loadConfiguration() $config = $this->validateConfig($this->defaults); $config = Helpers::expand($config, $builder->parameters); - //Register configuration + // Register configuration $configuration = $builder->addDefinition($this->prefix('configuration')); $configuration ->setClass(ContainerAwareConfiguration::class) @@ -53,7 +53,7 @@ public function loadConfiguration() elseif ($config['versionsOrganization'] === ContainerAwareConfiguration::VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH) $configuration->addSetup('setMigrationsAreOrganizedByYearAndMonth'); - //Register commands + // Register commands $builder->addDefinition($this->prefix('diffCommand')) ->setClass(DiffCommand::class) ->setAutowired(FALSE); @@ -79,7 +79,7 @@ public function loadConfiguration() ->setClass(VersionCommand::class) ->setAutowired(FALSE); - //Register configuration helper + // Register configuration helper $builder->addDefinition($this->prefix('configurationHelper')) ->setClass(ConfigurationHelper::class) ->setAutowired(FALSE); @@ -93,13 +93,15 @@ public function loadConfiguration() public function beforeCompile() { $builder = $this->getContainerBuilder(); + + // Register console helper only if console is provided $application = $builder->getByType(Application::class, FALSE); - if (!$application) - return; - $def = $builder->getDefinition($application); - // Register helpers - $configurationHelper = '@' . $this->prefix('configurationHelper'); - $def->addSetup(new Statement('$service->getHelperSet()->set(?)', [$configurationHelper])); + if ($application) { + $applicationDef = $builder->getDefinition($application); + $applicationDef->addSetup( + new Statement('$service->getHelperSet()->set(?)', ['@' . $this->prefix('configurationHelper')]) + ); + } } } diff --git a/tests/cases/DI/MigrationsExtension.phpt b/tests/cases/DI/MigrationsExtension.phpt index 28b72f7..910394d 100644 --- a/tests/cases/DI/MigrationsExtension.phpt +++ b/tests/cases/DI/MigrationsExtension.phpt @@ -10,15 +10,17 @@ use Nette\DI\Container; use Nette\DI\ContainerLoader; use Nettrine\Migrations\ContainerAwareConfiguration; use Nettrine\Migrations\DI\MigrationsExtension; +use Symfony\Component\Console\Application; use Tester\Assert; use Tester\FileMock; require_once __DIR__ . '/../../bootstrap.php'; +// With console test(function () { $loader = new ContainerLoader(TEMP_DIR, TRUE); $class = $loader->load(function (Compiler $compiler) { - //Required services and params + // Required services and params $compiler->loadConfig(FileMock::create(' parameters: appDir: "/srv/app" @@ -29,7 +31,7 @@ test(function () { - Doctrine\DBAL\Connection([]) ', 'neon')); - //Migrations + // Migrations $compiler->addExtension('migrations', new MigrationsExtension()); $compiler->loadConfig(FileMock::create(' migrations: @@ -39,7 +41,45 @@ test(function () { namespace: Migrations versionsOrganization: null ', 'neon')); - }, '1a'); + }, 1); + + /** @var Container $container */ + $container = new $class; + + /** @var ContainerAwareConfiguration $awareConfiguration */ + $awareConfiguration = $container->getByType(ContainerAwareConfiguration::class); + Assert::equal('Migrations', $awareConfiguration->getMigrationsNamespace()); + Assert::equal('/srv/app/../migrations', $awareConfiguration->getMigrationsDirectory()); + Assert::count(8, $container->findByType(AbstractCommand::class)); + // 4 default helpers + configurationHelper + Assert::count(5, iterator_to_array($container->getByType(Application::class)->getHelperSet())); +}); + +// Without console +test(function () { + $loader = new ContainerLoader(TEMP_DIR, TRUE); + $class = $loader->load(function (Compiler $compiler) { + // Required services and params + $compiler->loadConfig(FileMock::create(' + parameters: + appDir: "/srv/app" + + services: + - Doctrine\DBAL\Driver\Mysqli\Driver + - Doctrine\DBAL\Connection([]) + ', 'neon')); + + // Migrations + $compiler->addExtension('migrations', new MigrationsExtension()); + $compiler->loadConfig(FileMock::create(' + migrations: + table: doctrine_migrations + column: version + directory: %appDir%/../migrations + namespace: Migrations + versionsOrganization: null + ', 'neon')); + }, 2); /** @var Container $container */ $container = new $class;