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/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..b9eb014 --- /dev/null +++ b/src/Creational/FactoryMethod/Product/MusicalInstrument.php @@ -0,0 +1,20 @@ +brand = $brand; + } + + protected 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, + 'Tagima Model' + ); + + $eletricGuitar->make(); + + $this->assertInstanceOf(ElectricGuitar::class, $eletricGuitar); + } + + public function testShouldThrowExceptionWhenUnknownInstrument(): void + { + $this->expectException(\InvalidArgumentException::class); + + $factory = new MusicalInstrumentFactory(); + $factory->createMusicalInstrument('Unknown', 'Giannini Model'); + } +}