diff --git a/README.md b/README.md index 5e4f01e..adeded3 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,14 @@ Casos você queira se aprofundar no tema, não deixe de conheceu o site Growth ## Organização do Projeto: - `src/` + - `Behavioral/` + - `Creational/` + - `Structural/` +- `tests/` - `Behavioral/` - `Creational/` - `Structural/` + A estrutura da pastas de `tests/` segue a mesma estrutura @@ -36,7 +41,9 @@ Este projetos tem um arquivo `Makefile` para a execução dos testes `make test` +5. Para executar os testes de uma classe especídica: + `make test NomeDaClass` ## Sobre nós Este projeto foi desenvoldido por Walmir Silva autor do blog [https://growthdev.com.br](https://growthdev.com.br/) diff --git a/src/Behavioral/Strategy/Payment/CashPaymentMethod.php b/src/Behavioral/Strategy/Payment/CashPaymentMethod.php new file mode 100644 index 0000000..7c3575f --- /dev/null +++ b/src/Behavioral/Strategy/Payment/CashPaymentMethod.php @@ -0,0 +1,15 @@ +paymentMethod = $paymentMethod; + } + + public function processPayment(float $amount): void + { + $this->amount = $this->paymentMethod->pay($amount); + } + + public function getAmount(): float + { + return $this->amount; + } +} diff --git a/src/Behavioral/Strategy/Payment/PaymentProcessorWithoutStrategy.php b/src/Behavioral/Strategy/Payment/PaymentProcessorWithoutStrategy.php new file mode 100644 index 0000000..1c2b54c --- /dev/null +++ b/src/Behavioral/Strategy/Payment/PaymentProcessorWithoutStrategy.php @@ -0,0 +1,45 @@ +paymentMethod = $paymentMethod; + } + + public function processPayment(float $amount): void + { + switch ($this->paymentMethod) { + case self::PAYMENT_TYPE_CREDIT_CARD: + $creditCard = new CreditCardPaymentMethod(); + $this->amount = $creditCard->pay($amount); + break; + case self::PAYMENT_TYPE_DEBIT_CARD: + $debitCard = new DebitCardPaymentMethod(); + $this->amount = $debitCard->pay($amount); + break; + case self::PAYMENT_TYPE_CASH: + $cash = new CashPaymentMethod(); + $this->amount = $cash->pay($amount); + break; + default: + throw new \InvalidArgumentException('Invalid payment method'); + } + } + + public function getAmount(): float + { + return $this->amount; + } +} diff --git a/src/Behavioral/Strategy/WithMethodFactory/AcousticGuitarStrategy.php b/src/Behavioral/Strategy/WithMethodFactory/AcousticGuitarStrategy.php new file mode 100644 index 0000000..07005bd --- /dev/null +++ b/src/Behavioral/Strategy/WithMethodFactory/AcousticGuitarStrategy.php @@ -0,0 +1,26 @@ +brand = $brand; + } + + public function build(): MusicalInstrument + { + return (new AcousticGuitarFactory()) + ->createMusicalInstrument($this->brand); + } +} diff --git a/src/Behavioral/Strategy/WithMethodFactory/ElectricGuitarStrategy.php b/src/Behavioral/Strategy/WithMethodFactory/ElectricGuitarStrategy.php new file mode 100644 index 0000000..c0f2c63 --- /dev/null +++ b/src/Behavioral/Strategy/WithMethodFactory/ElectricGuitarStrategy.php @@ -0,0 +1,25 @@ +brand = $brand; + } + + public function build(): MusicalInstrument + { + return (new ElectricGuitarFactory()) + ->createMusicalInstrument($this->brand); + } +} diff --git a/src/Behavioral/Strategy/WithMethodFactory/MusicalInstrumentFactory.php b/src/Behavioral/Strategy/WithMethodFactory/MusicalInstrumentFactory.php new file mode 100644 index 0000000..b5920fc --- /dev/null +++ b/src/Behavioral/Strategy/WithMethodFactory/MusicalInstrumentFactory.php @@ -0,0 +1,19 @@ +build(); + } +} + + + diff --git a/src/Behavioral/Strategy/WithMethodFactory/MusicalInstrumentStrategy.php b/src/Behavioral/Strategy/WithMethodFactory/MusicalInstrumentStrategy.php new file mode 100644 index 0000000..72955af --- /dev/null +++ b/src/Behavioral/Strategy/WithMethodFactory/MusicalInstrumentStrategy.php @@ -0,0 +1,12 @@ +createMusicalInstrument($brand); + return (new AcousticGuitarFactory()) + ->createMusicalInstrument($brand); case self::ELECTRIC_GUITAR: - return (new ElectricGuitarFactory())->createMusicalInstrument($brand); + return (new ElectricGuitarFactory()) + ->createMusicalInstrument($brand); default: throw new \InvalidArgumentException('Invalid musical instrument type'); } diff --git a/tests/Behavioral/Strategy/Payment/PaymentProcessorTest.php b/tests/Behavioral/Strategy/Payment/PaymentProcessorTest.php new file mode 100644 index 0000000..8f58ca7 --- /dev/null +++ b/tests/Behavioral/Strategy/Payment/PaymentProcessorTest.php @@ -0,0 +1,39 @@ +processPayment(100.00); + + $this->assertEquals(90.00, $paymentProcessor->getAmount()); + } + + public function testCanProcessPaymentWithDebitCardPaymentMethod(): void + { + $paymentProcessor = new PaymentProcessor(new DebitCardPaymentMethod()); + $paymentProcessor->processPayment(100.00); + + $this->assertEquals(95.00, $paymentProcessor->getAmount()); + } + + public function testCanProcessPaymentWithCreditCardPaymentMethod(): void + { + $paymentProcessor = new PaymentProcessor(new CreditCardPaymentMethod()); + $paymentProcessor->processPayment(100.00); + + $this->assertEquals(100.00, $paymentProcessor->getAmount()); + } + +} \ No newline at end of file diff --git a/tests/Behavioral/Strategy/Payment/PaymentProcessorWithoutStrategyTest b/tests/Behavioral/Strategy/Payment/PaymentProcessorWithoutStrategyTest new file mode 100644 index 0000000..1986246 --- /dev/null +++ b/tests/Behavioral/Strategy/Payment/PaymentProcessorWithoutStrategyTest @@ -0,0 +1,44 @@ +processPayment(100.00); + + $this->assertEquals(90.00, $paymentProcessor->getAmount()); + } + + public function testCanProcessPaymentWithDebitCardPaymentMethod(): void + { + $paymentProcessor = new PaymentProcessorWithoutStrategy( + PaymentProcessorWithoutStrategy::PAYMENT_TYPE_DEBIT_CARD + ); + $paymentProcessor->processPayment(100.00); + + $this->assertEquals(95.00, $paymentProcessor->getAmount()); + } + + public function testCanProcessPaymentWithCreditCardPaymentMethod(): void + { + $paymentProcessor = new PaymentProcessorWithoutStrategy( + PaymentProcessorWithoutStrategy::PAYMENT_TYPE_CREDIT_CARD + ); + $paymentProcessor->processPayment(100.00); + + $this->assertEquals(100.00, $paymentProcessor->getAmount()); + } +} diff --git a/tests/Behavioral/Strategy/WithMethodFactory/MusicalInstrumentStrategyWithMethodFactoryTest.php b/tests/Behavioral/Strategy/WithMethodFactory/MusicalInstrumentStrategyWithMethodFactoryTest.php new file mode 100644 index 0000000..d6647dd --- /dev/null +++ b/tests/Behavioral/Strategy/WithMethodFactory/MusicalInstrumentStrategyWithMethodFactoryTest.php @@ -0,0 +1,39 @@ +createMusicalInstrument( + new AcousticGuitarStrategy('Giannini Model') + ); + + $acousticGuitar->make(); + + $this->assertInstanceOf(AcousticGuitar::class, $acousticGuitar); + } + + public function testCanCreateEletricGuitar(): void + { + $factory = new MusicalInstrumentFactory(); + $eletricGuitar = $factory->createMusicalInstrument( + new ElectricGuitarStrategy('Tagima Model') + ); + + $eletricGuitar->make(); + + $this->assertInstanceOf(ElectricGuitar::class, $eletricGuitar); + } +} diff --git a/tests/Creational/FactoryMethod/MusicalInstrumentFactoryTest.php b/tests/Creational/FactoryMethod/MusicalInstrumentFactoryTest.php index 52a3bcb..29f3682 100644 --- a/tests/Creational/FactoryMethod/MusicalInstrumentFactoryTest.php +++ b/tests/Creational/FactoryMethod/MusicalInstrumentFactoryTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace DesignPatterns\Creational\FactoryMethod; +namespace Growthdev\DesignPatterns\Tests\Creational\FactoryMethod; use Growthdev\DesignPatterns\Creational\FactoryMethod\MusicalInstrumentFactory; use Growthdev\DesignPatterns\Creational\FactoryMethod\Product\AcousticGuitar;