From f67886cb5c4b66c7d1bea64deb86ffb16ce101a6 Mon Sep 17 00:00:00 2001 From: Walmir Silva Date: Sat, 6 Nov 2021 18:16:49 -0300 Subject: [PATCH 1/5] create method factories class --- .../Factory/AcousticGuitarFactory.php | 16 ++++++++ .../Factory/ElectricGuitarFactory.php | 16 ++++++++ .../Factory/MusicalInstrumentFactory.php | 12 ++++++ .../MusicalInstrumentFactory.php | 27 +++++++++++++ .../FactoryMethod/Product/AcousticGuitar.php | 13 +++++++ .../FactoryMethod/Product/ElectricGuitar.php | 13 +++++++ .../Product/MusicalInstrument.php | 20 ++++++++++ .../Product/MusicalInstrumentProduct.php | 10 +++++ .../MusicalInstrumentFactoryTest.php | 38 +++++++++++++++++++ 9 files changed, 165 insertions(+) create mode 100644 src/Creational/FactoryMethod/Factory/AcousticGuitarFactory.php create mode 100644 src/Creational/FactoryMethod/Factory/ElectricGuitarFactory.php create mode 100644 src/Creational/FactoryMethod/Factory/MusicalInstrumentFactory.php create mode 100644 src/Creational/FactoryMethod/MusicalInstrumentFactory.php create mode 100644 src/Creational/FactoryMethod/Product/AcousticGuitar.php create mode 100644 src/Creational/FactoryMethod/Product/ElectricGuitar.php create mode 100644 src/Creational/FactoryMethod/Product/MusicalInstrument.php create mode 100644 src/Creational/FactoryMethod/Product/MusicalInstrumentProduct.php create mode 100644 tests/Creational/FactoryMethod/MusicalInstrumentFactoryTest.php diff --git a/src/Creational/FactoryMethod/Factory/AcousticGuitarFactory.php b/src/Creational/FactoryMethod/Factory/AcousticGuitarFactory.php new file mode 100644 index 0000000..b6c48e3 --- /dev/null +++ b/src/Creational/FactoryMethod/Factory/AcousticGuitarFactory.php @@ -0,0 +1,16 @@ +createMusicalInstrument($brand); + case self::ELECTRIC_GUITAR: + return (new ElectricGuitarFactory())->createMusicalInstrument($brand); + default: + throw new \InvalidArgumentException('Invalid musical instrument type'); + } + } +} diff --git a/src/Creational/FactoryMethod/Product/AcousticGuitar.php b/src/Creational/FactoryMethod/Product/AcousticGuitar.php new file mode 100644 index 0000000..ce47b76 --- /dev/null +++ b/src/Creational/FactoryMethod/Product/AcousticGuitar.php @@ -0,0 +1,13 @@ +getBrand()); + } +} diff --git a/src/Creational/FactoryMethod/Product/ElectricGuitar.php b/src/Creational/FactoryMethod/Product/ElectricGuitar.php new file mode 100644 index 0000000..45618d4 --- /dev/null +++ b/src/Creational/FactoryMethod/Product/ElectricGuitar.php @@ -0,0 +1,13 @@ +getBrand()); + } +} diff --git a/src/Creational/FactoryMethod/Product/MusicalInstrument.php b/src/Creational/FactoryMethod/Product/MusicalInstrument.php new file mode 100644 index 0000000..6a4c725 --- /dev/null +++ b/src/Creational/FactoryMethod/Product/MusicalInstrument.php @@ -0,0 +1,20 @@ +brand = $brand; + } + + public function getBrand(): string + { + return $this->brand; + } +} diff --git a/src/Creational/FactoryMethod/Product/MusicalInstrumentProduct.php b/src/Creational/FactoryMethod/Product/MusicalInstrumentProduct.php new file mode 100644 index 0000000..439d5c0 --- /dev/null +++ b/src/Creational/FactoryMethod/Product/MusicalInstrumentProduct.php @@ -0,0 +1,10 @@ +createMusicalInstrument( + MusicalInstrumentFactory::ACOUSTIC_GUITAR, + 'Giannini Model' + ); + + $acousticGuitar->make(); + + $this->assertInstanceOf(AcousticGuitar::class, $acousticGuitar); + } + + public function testCanCreateEletricGuitar(): void + { + $factory = new MusicalInstrumentFactory(); + $eletricGuitar = $factory->createMusicalInstrument( + MusicalInstrumentFactory::ELECTRIC_GUITAR, + 'Giannini Model' + ); + + $eletricGuitar->make(); + + $this->assertInstanceOf(ElectricGuitar::class, $eletricGuitar); + } +} From 5e95ab47106f07f73ac2a2b0fcac4ee58930f1ac Mon Sep 17 00:00:00 2001 From: Walmir Silva Date: Sat, 6 Nov 2021 18:20:24 -0300 Subject: [PATCH 2/5] add test for Unknow Musical Instrument --- .../FactoryMethod/MusicalInstrumentFactoryTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/Creational/FactoryMethod/MusicalInstrumentFactoryTest.php b/tests/Creational/FactoryMethod/MusicalInstrumentFactoryTest.php index 8ab3b5f..9602889 100644 --- a/tests/Creational/FactoryMethod/MusicalInstrumentFactoryTest.php +++ b/tests/Creational/FactoryMethod/MusicalInstrumentFactoryTest.php @@ -35,4 +35,15 @@ public function testCanCreateEletricGuitar(): void $this->assertInstanceOf(ElectricGuitar::class, $eletricGuitar); } + + public function testCanCreateUnknownMusicalInstrument(): void + { + $this->expectException(\InvalidArgumentException::class); + + $factory = new MusicalInstrumentFactory(); + $unknownMusicalInstrument = $factory->createMusicalInstrument( + 'Unknown', + 'Giannini Model' + ); + } } From 6b1cd3fe0d74e5803f61694d420c53cd0ac3ea1d Mon Sep 17 00:00:00 2001 From: Walmir Silva Date: Sat, 6 Nov 2021 18:27:01 -0300 Subject: [PATCH 3/5] change makefile for allow the test by class name --- Makefile | 6 ++++++ .../FactoryMethod/MusicalInstrumentFactoryTest.php | 7 ++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index cf6e78d..418e03e 100644 --- a/Makefile +++ b/Makefile @@ -2,3 +2,9 @@ test: @echo "Design Pattern Tests" php ./vendor/bin/phpunit --bootstrap ./vendor/autoload.php ./tests/ --testdox --colors + +.PHONY: test-class +test-class: + @echo "Design Pattern Tests for test class name" + php ./vendor/bin/phpunit --bootstrap ./vendor/autoload.php ./tests/ --testdox --colors --filter $(filter-out $@,$(MAKECMDGOALS)) + diff --git a/tests/Creational/FactoryMethod/MusicalInstrumentFactoryTest.php b/tests/Creational/FactoryMethod/MusicalInstrumentFactoryTest.php index 9602889..681ddc3 100644 --- a/tests/Creational/FactoryMethod/MusicalInstrumentFactoryTest.php +++ b/tests/Creational/FactoryMethod/MusicalInstrumentFactoryTest.php @@ -36,14 +36,11 @@ public function testCanCreateEletricGuitar(): void $this->assertInstanceOf(ElectricGuitar::class, $eletricGuitar); } - public function testCanCreateUnknownMusicalInstrument(): void + public function testShouldThrowExceptionWhenUnknownInstrument(): void { $this->expectException(\InvalidArgumentException::class); $factory = new MusicalInstrumentFactory(); - $unknownMusicalInstrument = $factory->createMusicalInstrument( - 'Unknown', - 'Giannini Model' - ); + $factory->createMusicalInstrument('Unknown', 'Giannini Model'); } } From 0582248c9ddac1046fae74718841eaf5436c64c6 Mon Sep 17 00:00:00 2001 From: Walmir Silva Date: Sat, 6 Nov 2021 21:07:20 -0300 Subject: [PATCH 4/5] Ajust visibility of method --- src/Creational/FactoryMethod/Product/MusicalInstrument.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Creational/FactoryMethod/Product/MusicalInstrument.php b/src/Creational/FactoryMethod/Product/MusicalInstrument.php index 6a4c725..b9eb014 100644 --- a/src/Creational/FactoryMethod/Product/MusicalInstrument.php +++ b/src/Creational/FactoryMethod/Product/MusicalInstrument.php @@ -13,7 +13,7 @@ public function __construct(string $brand) $this->brand = $brand; } - public function getBrand(): string + protected function getBrand(): string { return $this->brand; } From 4d61d1ce4c11a1f89066cfe2eddbf5fd22f092ed Mon Sep 17 00:00:00 2001 From: Walmir Silva Date: Sat, 6 Nov 2021 21:20:36 -0300 Subject: [PATCH 5/5] ajust namaspace of test --- .../FactoryMethod/MusicalInstrumentFactoryTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/Creational/FactoryMethod/MusicalInstrumentFactoryTest.php b/tests/Creational/FactoryMethod/MusicalInstrumentFactoryTest.php index 681ddc3..52a3bcb 100644 --- a/tests/Creational/FactoryMethod/MusicalInstrumentFactoryTest.php +++ b/tests/Creational/FactoryMethod/MusicalInstrumentFactoryTest.php @@ -7,8 +7,9 @@ use Growthdev\DesignPatterns\Creational\FactoryMethod\MusicalInstrumentFactory; use Growthdev\DesignPatterns\Creational\FactoryMethod\Product\AcousticGuitar; use Growthdev\DesignPatterns\Creational\FactoryMethod\Product\ElectricGuitar; +use PHPUnit\Framework\TestCase; -final class MusicalInstrumentFactoryTest extends \PHPUnit\Framework\TestCase +final class MusicalInstrumentFactoryTest extends TestCase { public function testCanCreateAcousticGuitar(): void { @@ -28,7 +29,7 @@ public function testCanCreateEletricGuitar(): void $factory = new MusicalInstrumentFactory(); $eletricGuitar = $factory->createMusicalInstrument( MusicalInstrumentFactory::ELECTRIC_GUITAR, - 'Giannini Model' + 'Tagima Model' ); $eletricGuitar->make();