diff --git a/src/Console/Command/GenerateCommand.php b/src/Console/Command/GenerateCommand.php index 896de6f..40af0e6 100644 --- a/src/Console/Command/GenerateCommand.php +++ b/src/Console/Command/GenerateCommand.php @@ -40,7 +40,7 @@ class GenerateCommand extends Command try { // TODO: Inject this. - new Builder($this->filters($input), $outputFile = $this->outputFile($input)); + new Builder($this->filters($input), $outputFile = $this->outputFile($input), true, $input->getOption('expanded')); $io->success(sprintf('%s file generated.', $outputFile)); } catch (IOException $e) { @@ -61,6 +61,6 @@ class GenerateCommand extends Command throw new \RuntimeException('No input file found.'); } - return require_once $inputFile; + return require $inputFile; } } diff --git a/src/Service/Builder.php b/src/Service/Builder.php index 40bc4c2..c96878d 100644 --- a/src/Service/Builder.php +++ b/src/Service/Builder.php @@ -29,12 +29,16 @@ class Builder */ private $xml; - public function __construct(array $filters, $outputFile = 'filters.xml', $writeFile = true) + /** @var bool */ + private $expanded; + + public function __construct(array $filters, $outputFile = 'filters.xml', $writeFile = true, $expanded = false) { $this->filesystem = new Filesystem(); $this->filters = $filters; $this->outputFile = $outputFile; $this->writeFile = $writeFile; + $this->expanded = $expanded; $this->build(); } @@ -61,14 +65,14 @@ class Builder */ private function build(): void { - $prefix = "" . PHP_EOL . ""; + $prefix = "" . $this->glue() . ""; $suffix = ''; $xml = collect($this->filters)->map(function ($items) { return $this->buildEntry($items); - })->implode(PHP_EOL); + })->implode($this->glue()); - $this->xml = collect([$prefix, $xml, $suffix])->implode(PHP_EOL); + $this->xml = collect([$prefix, $xml, $suffix])->implode($this->glue()); if ($this->writeFile) { $this->filesystem->dumpFile($this->outputFile, $this->xml); @@ -88,9 +92,9 @@ class Builder ->map(function ($value, $key): string { return $this->buildProperty($value, $key); }) - ->implode(PHP_EOL); + ->implode($this->glue()); - return collect(['', $entry, ''])->implode(PHP_EOL); + return collect(['', $entry, ''])->implode($this->glue()); } /** @@ -128,4 +132,9 @@ class Builder return sprintf('(%s)', collect($value)->implode($separator)); } + + private function glue() + { + return $this->expanded ? PHP_EOL : null; + } } diff --git a/tests/Unit/Console/Command/GenerateFiltersTest.php b/tests/Unit/Console/Command/GenerateFiltersTest.php index fb51675..59bca9f 100644 --- a/tests/Unit/Console/Command/GenerateFiltersTest.php +++ b/tests/Unit/Console/Command/GenerateFiltersTest.php @@ -33,6 +33,22 @@ class GenerateFiltersTest extends TestCase $this->fs->remove([self::OUTPUT_FILENAME]); } + /** @test */ + public function it_converts_filters_from_php_to_minified_xml() + { + $this->commandTester->execute([ + '--input-file' => self::INPUT_FILENAME, + '--output-file' => self::OUTPUT_FILENAME, + ]); + + $this->assertTrue($this->fs->exists(self::OUTPUT_FILENAME)); + + $expected = file_get_contents(__DIR__ . '/../../../fixtures/simple/output.xml'); + $result = file_get_contents(self::OUTPUT_FILENAME); + + $this->assertEquals(trim($expected), $result); + } + /** @test */ public function it_converts_filters_from_php_to_expanded_xml() { diff --git a/tests/Unit/Service/BuilderTest.php b/tests/Unit/Service/BuilderTest.php index 4c40fc2..e8f7695 100644 --- a/tests/Unit/Service/BuilderTest.php +++ b/tests/Unit/Service/BuilderTest.php @@ -20,7 +20,7 @@ class BuilderTest extends TestCase ->star() ->important(); - $result = new Builder([$filterA, $filterB], '', false); + $result = new Builder([$filterA, $filterB], '', false, true); $expected = <<