Skip to content

Commit

Permalink
Create form extractor in test case setUp method.
Browse files Browse the repository at this point in the history
This is a cleaner way to share the same form extractor.
  • Loading branch information
bvleur committed Aug 15, 2012
1 parent cb4faf3 commit 809327f
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions Tests/Translation/Extractor/File/FormExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@

class FormExtractorTest extends \PHPUnit_Framework_TestCase
{
public function testExtract(FormExtractor $extractor = null)
/**
* @var FormExtractor
*/
private $extractor;

public function testExtract()
{
$expected = new MessageCatalogue();
$path = __DIR__.'/Fixture/MyFormType.php';
Expand Down Expand Up @@ -70,7 +75,7 @@ public function testExtract(FormExtractor $extractor = null)
$message->addSource(new FileSource($path, 47));
$expected->add($message);

$this->assertEquals($expected, $this->extract('MyFormType.php', $extractor));
$this->assertEquals($expected, $this->extract('MyFormType.php'));
}

/**
Expand Down Expand Up @@ -107,7 +112,7 @@ public function testExtractWithInterface()
* This test is used to check if the default 'translation_domain' option
* set for the entire form is extracted correctly
*/
public function testExtractWithDefaultDomain(FormExtractor $extractor = null)
public function testExtractWithDefaultDomain()
{
$expected = new MessageCatalogue();
$path = __DIR__.'/Fixture/MyFormTypeWithDefaultDomain.php';
Expand All @@ -126,7 +131,7 @@ public function testExtractWithDefaultDomain(FormExtractor $extractor = null)
$message->addSource(new FileSource($path, 37));
$expected->add($message);

$this->assertEquals($expected, $this->extract('MyFormTypeWithDefaultDomain.php', $extractor));
$this->assertEquals($expected, $this->extract('MyFormTypeWithDefaultDomain.php'));
}

/**
Expand All @@ -136,13 +141,11 @@ public function testExtractWithDefaultDomain(FormExtractor $extractor = null)
*/
public function testExtractWithNoDefaultDomainAfterDefaultDomainExtraction()
{
$extractor = $this->createFormExtractor();

$this->testExtractWithDefaultDomain($extractor);
$this->testExtract($extractor);
$this->testExtractWithDefaultDomain();
$this->testExtract();
}

private function createFormExtractor()
protected function setUp()
{
$docParser = new DocParser();
$docParser->setImports(array(
Expand All @@ -152,26 +155,22 @@ private function createFormExtractor()
));
$docParser->setIgnoreNotImportedAnnotations(true);

return new FormExtractor($docParser);
$this->extractor = new FormExtractor($docParser);
}

private function extract($file, FormExtractor $extractor = null)
private function extract($file)
{
if (!is_file($file = __DIR__.'/Fixture/'.$file)) {
throw new RuntimeException(sprintf('The file "%s" does not exist.', $file));
}
$file = new \SplFileInfo($file);

if (null === $extractor) {
$extractor = $this->createFormExtractor();
}

$lexer = new \PHPParser_Lexer(file_get_contents($file));
$parser = new \PHPParser_Parser();
$ast = $parser->parse($lexer);

$catalogue = new MessageCatalogue();
$extractor->visitPhpFile($file, $catalogue, $ast);
$this->extractor->visitPhpFile($file, $catalogue, $ast);

return $catalogue;
}
Expand Down

0 comments on commit 809327f

Please sign in to comment.