Skip to content

Commit

Permalink
Tests: Mockery, better structure, constants, ContainerHelperTest
Browse files Browse the repository at this point in the history
  • Loading branch information
josefbenjac committed Feb 28, 2018
1 parent 0986091 commit ebeb953
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 29 deletions.
9 changes: 5 additions & 4 deletions composer.json
Expand Up @@ -19,7 +19,8 @@
},
"require-dev": {
"ninjify/qa": "~0.7.0",
"phpunit/phpunit": "^7.0.2"
"phpunit/phpunit": "^7.0.2",
"mockery/mockery": "^1.0"
},
"autoload": {
"psr-4": {
Expand All @@ -28,13 +29,13 @@
},
"autoload-dev": {
"psr-4": {
"Tests\\Nettrine\\Migrations\\": "tests/"
"Tests\\Nettrine\\Migrations\\": "tests/cases/"
}
},
"scripts": {
"qa": [
"linter src tests",
"codesniffer src tests"
"linter src tests/cases",
"codesniffer src tests/cases"
],
"phpunit": [
"phpunit tests --colors=always"
Expand Down
2 changes: 1 addition & 1 deletion ruleset.xml
Expand Up @@ -7,7 +7,7 @@
<properties>
<property name="rootNamespaces" type="array" value="
src=>Nettrine\Migrations,
tests=>Tests\Nettrine\Migrations,
tests/cases=>Tests\Nettrine\Migrations,
"/>
</properties>
</rule>
Expand Down
11 changes: 5 additions & 6 deletions src/DI/MigrationsExtension.php
Expand Up @@ -96,13 +96,12 @@ public function beforeCompile(): void

// Register console helper only if console is provided
$application = $builder->getByType(Application::class, FALSE);
if (!$application) {
return;
if ($application) {
$applicationDef = $builder->getDefinition($application);
$applicationDef->addSetup(
new Statement('$service->getHelperSet()->set(?)', [$this->prefix('@configurationHelper')])
);
}
$applicationDef = $builder->getDefinition($application);
$applicationDef->addSetup(
new Statement('$service->getHelperSet()->set(?)', [$this->prefix('@configurationHelper')])
);
}

}
11 changes: 0 additions & 11 deletions tests/bootstrap.php

This file was deleted.

Expand Up @@ -8,8 +8,8 @@
use Nette\DI\ContainerLoader;
use Nettrine\Migrations\ContainerAwareConfiguration;
use Nettrine\Migrations\DI\MigrationsExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Tests\Nettrine\Migrations\TestCase;

final class MigrationsExtensionTest extends TestCase
{
Expand All @@ -19,13 +19,13 @@ final class MigrationsExtensionTest extends TestCase
*/
public function testConsole(): void
{
$loader = new ContainerLoader(__DIR__ . '/temp', TRUE);
$loader = new ContainerLoader(TEMP_PATH, TRUE);
$class = $loader->load(function (Compiler $compiler): void {
// Required services and params
$compiler->loadConfig(__DIR__ . '/config/services.neon');
$compiler->loadConfig(FIXTURES_PATH . '/config/services.neon');
// Migrations
$compiler->addExtension('migrations', new MigrationsExtension());
$compiler->loadConfig(__DIR__ . '/config/default.neon');
$compiler->loadConfig(FIXTURES_PATH . '/config/default.neon');
}, 1);

/** @var Container $container */
Expand All @@ -45,13 +45,13 @@ public function testConsole(): void
*/
public function testWithoutConsole(): void
{
$loader = new ContainerLoader(__DIR__ . '/temp', TRUE);
$loader = new ContainerLoader(TEMP_PATH, TRUE);
$class = $loader->load(function (Compiler $compiler): void {
// Required services and params
$compiler->loadConfig(__DIR__ . '/config/services_without_console.neon');
$compiler->loadConfig(FIXTURES_PATH . '/config/services_without_console.neon');
// Migrations
$compiler->addExtension('migrations', new MigrationsExtension());
$compiler->loadConfig(__DIR__ . '/config/default.neon');
$compiler->loadConfig(FIXTURES_PATH . '/config/default.neon');
}, 2);

/** @var Container $container */
Expand Down
39 changes: 39 additions & 0 deletions tests/cases/Helper/ConfigurationHelperTest.php
@@ -0,0 +1,39 @@
<?php declare(strict_types = 1);

namespace Tests\Nettrine\Migrations\Helper;

use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\DBAL\Migrations\OutputWriter;
use Mockery;
use Mockery\MockInterface;
use Nettrine\Migrations\Helper\ConfigurationHelper;
use Symfony\Component\Console\Input\InputInterface;
use Tests\Nettrine\Migrations\TestCase;

final class ConfigurationHelperTest extends TestCase
{

/**
* @return void
*/
public function testGetMigrationConfig(): void
{
/** @var InputInterface|MockInterface $input */
$input = Mockery::mock(InputInterface::class);
/** @var OutputWriter|MockInterface $outputWriter */
$outputWriter = Mockery::mock(OutputWriter::class);

$helper = new ConfigurationHelper();
self::assertNull($helper->getMigrationConfig($input, $outputWriter));

/** @var Configuration|MockInterface $configuration */
$configuration = Mockery::mock(Configuration::class)
->shouldReceive('setOutputWriter')
->withArgs([$outputWriter])
->getMock();

$helper = new ConfigurationHelper(NULL, $configuration);
self::assertSame($configuration, $helper->getMigrationConfig($input, $outputWriter));
}

}
33 changes: 33 additions & 0 deletions tests/cases/TestCase.php
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

namespace Tests\Nettrine\Migrations;

use Mockery;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;

abstract class TestCase extends PHPUnitTestCase
{

/**
* @return void
*/
protected function setUp(): void
{
parent::setUp();
if (!defined('TEMP_PATH')) {
define('TEMP_PATH', __DIR__ . '/../tmp');
}
if (!defined('FIXTURES_PATH')) {
define('FIXTURES_PATH', __DIR__ . '/../fixtures');
}
}

/**
* @return void
*/
protected function tearDown(): void
{
Mockery::close();
}

}
File renamed without changes.
File renamed without changes.

0 comments on commit ebeb953

Please sign in to comment.