Skip to content

Commit

Permalink
Extension: added more tests, typo
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Nov 15, 2017
1 parent c4c51d5 commit 523fd2a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
20 changes: 11 additions & 9 deletions src/DI/MigrationsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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')])
);
}
}

}
46 changes: 43 additions & 3 deletions tests/cases/DI/MigrationsExtension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -29,7 +31,7 @@ test(function () {
- Doctrine\DBAL\Connection([])
', 'neon'));

//Migrations
// Migrations
$compiler->addExtension('migrations', new MigrationsExtension());
$compiler->loadConfig(FileMock::create('
migrations:
Expand All @@ -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;
Expand Down

0 comments on commit 523fd2a

Please sign in to comment.