Skip to content

Commit

Permalink
Merge pull request #8 from stefk/twig-extension
Browse files Browse the repository at this point in the history
Avoid adding twig extension in constructor
  • Loading branch information
ngodfraind committed Mar 20, 2015
2 parents ae3adc0 + 18ee443 commit 28146bd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
7 changes: 6 additions & 1 deletion Generator/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Writer
private $fileSystem;
private $twigEnvironment;
private $twigEngine;
private $hasSqlExtension = false;

/**
* Constructor.
Expand All @@ -42,7 +43,6 @@ public function __construct(
$this->fileSystem = $fileSystem;
$this->twigEnvironment = $environment;
$this->twigEngine = $engine;
$this->twigEnvironment->addExtension(new SqlFormatterExtension());
}

/**
Expand All @@ -55,6 +55,11 @@ public function __construct(
*/
public function writeMigrationClass(Bundle $bundle, $driverName, $version, array $queries)
{
if (!$this->hasSqlExtension) {
$this->twigEnvironment->addExtension(new SqlFormatterExtension());
$this->hasSqlExtension = true;
}

$targetDir = "{$bundle->getPath()}/Migrations/{$driverName}";
$class = "Version{$version}";
$namespace = "{$bundle->getNamespace()}\\Migrations\\{$driverName}";
Expand Down
20 changes: 7 additions & 13 deletions Tests/Generator/WriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,6 @@ protected function setUp()
$this->fileSystem = m::mock('Symfony\Component\Filesystem\Filesystem');
}

public function testWriterAddsTheSqlFormatterExtension()
{
$this->twigEnvironment->shouldReceive('addExtension')->once()->with(
m::on(
function ($argument) {
return $argument instanceof SqlFormatterExtension;
}
)
);
$writer = new Writer($this->fileSystem, $this->twigEnvironment, $this->twigEngine);
}

public function testWriteMigrationClasses()
{
vfsStream::setup(
Expand All @@ -59,7 +47,13 @@ public function testWriteMigrationClasses()
)
)
);
$this->twigEnvironment->shouldReceive('addExtension');
$this->twigEnvironment->shouldReceive('addExtension')->once()->with(
m::on(
function ($argument) {
return $argument instanceof SqlFormatterExtension;
}
)
);
$bundle = m::mock('Symfony\Component\HttpKernel\Bundle\Bundle');
$bundle->shouldReceive('getPath')->once()->andReturn(vfsStream::url('root') . '/bundle/path');
$bundle->shouldReceive('getNamespace')->once()->andReturn('Bundle\Namespace');
Expand Down

0 comments on commit 28146bd

Please sign in to comment.