From 8cec0808762c4ed6ad1a06e9763046b13a13b296 Mon Sep 17 00:00:00 2001 From: dmason30 Date: Sun, 24 May 2020 16:02:36 +0100 Subject: [PATCH 1/2] Add method to swap registered generators --- src/Blueprint.php | 11 +++++++++++ tests/Feature/BlueprintTest.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/Blueprint.php b/src/Blueprint.php index c6708b18..f18d6229 100644 --- a/src/Blueprint.php +++ b/src/Blueprint.php @@ -91,4 +91,15 @@ public function registerGenerator(Generator $generator) { $this->generators[] = $generator; } + + public function swapGenerator(string $concrete, Generator $generator) + { + foreach($this->generators as $key => $registeredGenerator) { + if (get_class($registeredGenerator) === $concrete) { + unset($this->generators[$key]); + } + } + + $this->registerGenerator($generator); + } } diff --git a/tests/Feature/BlueprintTest.php b/tests/Feature/BlueprintTest.php index c5bccf8a..e4a379ed 100644 --- a/tests/Feature/BlueprintTest.php +++ b/tests/Feature/BlueprintTest.php @@ -5,6 +5,7 @@ use Blueprint\Blueprint; use Blueprint\Contracts\Generator; use Blueprint\Contracts\Lexer; +use Illuminate\Support\Facades\Log; use Symfony\Component\Yaml\Exception\ParseException; use Tests\TestCase; @@ -355,6 +356,34 @@ public function generate_uses_registered_generators_and_returns_generated_files( ], $this->subject->generate($tree)); } + /** + * @test + */ + public function generate_uses_swapped_generator_and_returns_generated_files() + { + $generatorOne = \Mockery::mock(Generator::class); + $tree = ['branch' => ['code', 'attributes']]; + $generatorOne->expects('output')->never(); + + $generatorSwap = \Mockery::mock(Generator::class); + $generatorSwap->expects('output') + ->with($tree) + ->andReturn([ + 'created' => ['swapped/new.php'], + 'updated' => ['swapped/existing.php'], + 'deleted' => ['swapped/trashed.php'] + ]); + + $this->subject->registerGenerator($generatorOne); + $this->subject->swapGenerator(get_class($generatorOne), $generatorSwap); + + $this->assertEquals([ + 'created' => ['swapped/new.php'], + 'updated' => ['swapped/existing.php'], + 'deleted' => ['swapped/trashed.php'], + ], $this->subject->generate($tree)); + } + /** * @test * @dataProvider namespacesDataProvider From 5917c46ac58e535a91cc8dabf90a77eb04654c5e Mon Sep 17 00:00:00 2001 From: dmason30 Date: Sun, 24 May 2020 16:09:28 +0100 Subject: [PATCH 2/2] Remove unused import --- src/Blueprint.php | 2 +- tests/Feature/BlueprintTest.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Blueprint.php b/src/Blueprint.php index f18d6229..43d094cd 100644 --- a/src/Blueprint.php +++ b/src/Blueprint.php @@ -94,7 +94,7 @@ public function registerGenerator(Generator $generator) public function swapGenerator(string $concrete, Generator $generator) { - foreach($this->generators as $key => $registeredGenerator) { + foreach ($this->generators as $key => $registeredGenerator) { if (get_class($registeredGenerator) === $concrete) { unset($this->generators[$key]); } diff --git a/tests/Feature/BlueprintTest.php b/tests/Feature/BlueprintTest.php index e4a379ed..58e87b28 100644 --- a/tests/Feature/BlueprintTest.php +++ b/tests/Feature/BlueprintTest.php @@ -5,7 +5,6 @@ use Blueprint\Blueprint; use Blueprint\Contracts\Generator; use Blueprint\Contracts\Lexer; -use Illuminate\Support\Facades\Log; use Symfony\Component\Yaml\Exception\ParseException; use Tests\TestCase;