Skip to content

Commit

Permalink
Adding tests for the abstractMigration
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeSimonson committed Jun 27, 2015
1 parent 081155d commit ef21fe1
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 27 deletions.
3 changes: 2 additions & 1 deletion lib/Doctrine/DBAL/Migrations/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ public function getDescription()
public function warnIf($condition, $message = '')
{
if ($condition) {
$message = $message?: 'Unknown Reason';
$this->outputWriter->write(sprintf(
' <warning>Warning during %s: %s</warning>',
$this->version->getExecutionState(),
$message ?: 'Unknown Reason'
$message
));
}
}
Expand Down
69 changes: 56 additions & 13 deletions tests/Doctrine/DBAL/Migrations/Tests/AbstractMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ class AbstractMigrationTest extends MigrationTestCase
{
private $config;
private $version;
/** @var AbstractMigrationStub */
private $migration;
private $outputWriter;
protected $outputWriter;
protected $output;

public function setUp()
{
$this->outputWriter = $this->getMockBuilder('Doctrine\DBAL\Migrations\OutputWriter')
->getMock();
$this->outputWriter = $this->getOutputWriter();

$this->config = new Configuration($this->getSqliteConnection(), $this->outputWriter);
$this->config->setMigrationsDirectory(\sys_get_temp_dir());
Expand All @@ -37,23 +38,32 @@ public function testGetDescriptionReturnsEmptyString()
$this->assertSame('', $this->migration->getDescription());
}

public function testWarnIfInvokesOutputWriter()
public function testWarnIfOutputMessage()
{
$this->outputWriter
->expects($this->once())
->method('write');

$this->migration->warnIf(true, 'Warning was thrown');
$this->assertContains('<warning>Warning during No State: Warning was thrown</warning>'
, $this->getOutputStreamContent($this->output));
}

public function testWriteInvokesOutputWriter()
public function testWarnIfAddDefaultMessage()
{
$this->outputWriter
->expects($this->once())
->method('write')
->with('Message');
$this->migration->warnIf(true);
$this->assertContains('<warning>Warning during No State: Unknown Reason</warning>'
, $this->getOutputStreamContent($this->output));
}

public function testWarnIfDontOutputMessageIfFalse()
{
$this->migration->warnIf(false, 'trallala');
$this->assertEquals('', $this->getOutputStreamContent($this->output));
}

public function testWriteInvokesOutputWriter()
{
$this->migration->exposed_Write('Message');
$this->assertContains('Message'
, $this->getOutputStreamContent($this->output));

}

public function testAbortIfThrowsException()
Expand All @@ -62,15 +72,48 @@ public function testAbortIfThrowsException()
$this->migration->abortIf(true, 'Something failed');
}

public function testAbortIfDontThrowsException()
{
$this->migration->abortIf(false, 'Something failed');
}

public function testAbortIfThrowsExceptionEvenWithoutMessage()
{
$this->setExpectedException('Doctrine\DBAL\Migrations\AbortMigrationException', 'Unknown Reason');
$this->migration->abortIf(true);
}

public function testSkipIfThrowsException()
{
$this->setExpectedException('Doctrine\DBAL\Migrations\SkipMigrationException', 'Something skipped');
$this->migration->skipIf(true, 'Something skipped');
}

public function testSkipIfDontThrowsException()
{
$this->migration->skipIf(false, 'Something skipped');
}

public function testThrowIrreversibleMigrationException()
{
$this->setExpectedException('Doctrine\DBAL\Migrations\IrreversibleMigrationException', 'Irreversible migration');
$this->migration->exposed_ThrowIrreversibleMigrationException('Irreversible migration');
}

public function testThrowIrreversibleMigrationExceptionWithoutMessage()
{
$this->setExpectedException('Doctrine\DBAL\Migrations\IrreversibleMigrationException', 'This migration is irreversible and cannot be reverted.');
$this->migration->exposed_ThrowIrreversibleMigrationException();
}

public function testIstransactional()
{
$this->assertTrue($this->migration->isTransactional());
}

public function testAddSql()
{
$this->migration->exposed_AddSql('tralala');
}

}
13 changes: 13 additions & 0 deletions tests/Doctrine/DBAL/Migrations/Tests/MigrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\DBAL\Migrations\OutputWriter;
use Symfony\Component\Console\Output\StreamOutput;

abstract class MigrationTestCase extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -68,4 +69,16 @@ public function getInputStream($input)

return $stream;
}

protected function getOutputWriter()
{
if (!$this->outputWriter) {
$this->output = $this->getOutputStream();
$output = $this->output;
$this->outputWriter = new OutputWriter(function ($message) use ($output) {
return $output->writeln($message);
});
}
return $this->outputWriter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public function exposed_ThrowIrreversibleMigrationException($message = null)
{
$this->throwIrreversibleMigrationException($message);
}

public function exposed_AddSql($sql, $params = array(), $types = array())
{
$this->addSql($sql, $params, $types);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ class ConfigurationHelperTest extends MigrationTestCase
/**
* @var OutputWriter
*/
private $outputWriter;
protected $outputWriter;

/**
* @var OutputInterface
*/
private $output;
protected $output;

/**
* @var InputInterface
Expand Down Expand Up @@ -148,15 +148,4 @@ public function testConfigurationHelperWithoutConfigurationFromSetterAndWithoutO
$this->assertStringMatchesFormat("", $this->getOutputStreamContent($this->output));
}

private function getOutputWriter()
{
if (!$this->outputWriter) {
$this->output = $this->getOutputStream();
$output = $this->output;
$this->outputWriter = new OutputWriter(function ($message) use ($output) {
return $output->writeln($message);
});
}
return $this->outputWriter;
}
}

0 comments on commit ef21fe1

Please sign in to comment.