From 7a700a2c44a558cbe0783247b8a81bef694d9b32 Mon Sep 17 00:00:00 2001 From: Walmir Silva Date: Thu, 4 Nov 2021 20:21:10 -0300 Subject: [PATCH 1/5] add composer to project --- .gitignore | 41 +---------------------------------------- composer.json | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 40 deletions(-) create mode 100644 composer.json diff --git a/.gitignore b/.gitignore index 3dab634..b2511cc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,40 +1,6 @@ -# Cache and logs (Symfony2) -/app/cache/* -/app/logs/* -!app/cache/.gitkeep -!app/logs/.gitkeep - -# Email spool folder -/app/spool/* - -# Cache, session files and logs (Symfony3) -/var/cache/* -/var/logs/* -/var/sessions/* -!var/cache/.gitkeep -!var/logs/.gitkeep -!var/sessions/.gitkeep - -# Logs (Symfony4) -/var/log/* -!var/log/.gitkeep - -# Parameters -/app/config/parameters.yml -/app/config/parameters.ini - # Managed by Composer -/app/bootstrap.php.cache -/var/bootstrap.php.cache -/bin/* -!bin/console -!bin/symfony_requirements /vendor/ -# Assets and user uploads -/web/bundles/ -/web/uploads/ - # PHPUnit /app/phpunit.xml /phpunit.xml @@ -44,9 +10,4 @@ # Composer PHAR /composer.phar - -# Backup entities generated with doctrine:generate:entities command -**/Entity/*~ - -# Embedded web-server pid file -/.web-server-pid +/composer.lock diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..71a30e9 --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "growthdev/design-patterns", + "description": "Exemplos em PHP dos 23 Padrões de Projetos (Design Patterns) relacionados ao Livro GOF", + "type": "php", + "require": { + "php": "8.0.12" + }, + "require-dev": { + "phpunit/phpunit": "^9.5" + }, + "license": "MIT", + "autoload": { + "psr-4": { + "Growthdev\\DesignPatterns\\": "src/" + } + }, + "authors": [ + { + "name": "Walmir Silva", + "email": "git@growthdev.com.br" + } + ] +} From 35aa338e88ad686c8e1ac428bbef41bd9eb0e318 Mon Sep 17 00:00:00 2001 From: Walmir Silva Date: Thu, 4 Nov 2021 20:37:14 -0300 Subject: [PATCH 2/5] create-singleton-design-pattern --- composer.json | 5 +++ src/Creational/Singleton/Singleton.php | 35 ++++++++++++++++ tests/Creational/Singleton/SingletonTest.php | 42 ++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 src/Creational/Singleton/Singleton.php create mode 100644 tests/Creational/Singleton/SingletonTest.php diff --git a/composer.json b/composer.json index 71a30e9..ac955fb 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,11 @@ "Growthdev\\DesignPatterns\\": "src/" } }, + "autoload-dev": { + "psr-4": { + "Growthdev\\DesignPatterns\\Tests\\": "tests/" + } + }, "authors": [ { "name": "Walmir Silva", diff --git a/src/Creational/Singleton/Singleton.php b/src/Creational/Singleton/Singleton.php new file mode 100644 index 0000000..986e140 --- /dev/null +++ b/src/Creational/Singleton/Singleton.php @@ -0,0 +1,35 @@ +assertSame($firstInstance, $secondInstance); + } + + public function testThrowErrorWhenTryToCreateInstance(): void + { + $this->expectException(Error::class); + + $instance = new Singleton(); + } + + public function testThrowExceptionWhenTryToClone(): void + { + $this->expectException(Error::class); + + $instance = Singleton::getInstance(); + $clone = clone $instance; + } + + public function testThrowExceptionWhenTryToSerialize(): void + { + $this->expectException(Error::class); + + $instance = Singleton::getInstance(); + $serialize = serialize($instance); + } +} \ No newline at end of file From e28919707c9ec346a926fb380ae5f323a7d0ebdb Mon Sep 17 00:00:00 2001 From: Walmir Silva Date: Thu, 4 Nov 2021 20:43:32 -0300 Subject: [PATCH 3/5] create singleton design pattern --- src/Creational/Singleton/Singleton.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Creational/Singleton/Singleton.php b/src/Creational/Singleton/Singleton.php index 986e140..44ecf40 100644 --- a/src/Creational/Singleton/Singleton.php +++ b/src/Creational/Singleton/Singleton.php @@ -4,6 +4,8 @@ namespace DesignPatterns\Creational\Singleton; +use LogicException; + final class Singleton { private static ?Singleton $instance = null; @@ -25,8 +27,9 @@ private function __clone() { } - private function __wakeup() + public function __wakeup() { + throw new LogicException('Cannot unserialize a singleton.'); } private function __sleep() From de09eea898ba9afe56dd3b55fab25b38e109e555 Mon Sep 17 00:00:00 2001 From: Walmir Silva Date: Thu, 4 Nov 2021 21:03:43 -0300 Subject: [PATCH 4/5] Normalize test method names --- Makefile | 4 ++++ src/Creational/Singleton/Singleton.php | 15 ++++++++------- tests/Creational/Singleton/SingletonTest.php | 15 +++++++++------ 3 files changed, 21 insertions(+), 13 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cf6e78d --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +.PHONY: test +test: + @echo "Design Pattern Tests" + php ./vendor/bin/phpunit --bootstrap ./vendor/autoload.php ./tests/ --testdox --colors diff --git a/src/Creational/Singleton/Singleton.php b/src/Creational/Singleton/Singleton.php index 44ecf40..8e5f95c 100644 --- a/src/Creational/Singleton/Singleton.php +++ b/src/Creational/Singleton/Singleton.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace DesignPatterns\Creational\Singleton; +namespace Growthdev\DesignPatterns\Creational\Singleton; use LogicException; @@ -23,16 +23,17 @@ protected function __construct() { } - private function __clone() + private function __clone(): void { } - public function __wakeup() + public function __sleep() { - throw new LogicException('Cannot unserialize a singleton.'); + throw new LogicException('Cannot serialize a singleton.'); } - - private function __sleep() + + public function __wakeup(): void { + throw new LogicException('Cannot unserialize a singleton.'); } -} \ No newline at end of file +} diff --git a/tests/Creational/Singleton/SingletonTest.php b/tests/Creational/Singleton/SingletonTest.php index 6f4498b..4cd87c0 100644 --- a/tests/Creational/Singleton/SingletonTest.php +++ b/tests/Creational/Singleton/SingletonTest.php @@ -4,12 +4,14 @@ namespace Growthdev\DesignPatterns\Creational\Singleton; -use DesignPatterns\Creational\Singleton\Singleton; +use Growthdev\DesignPatterns\Creational\Singleton\Singleton; +use Error; +use LogicException; use PHPUnit\Framework\TestCase; final class SingletonTest extends TestCase { - public function testShoudBeTheSameInstanceForTwoObjets(): void + public function testShouldBeTheSameInstanceForTwoObjets(): void { $firstInstance = Singleton::getInstance(); $secondInstance = SingleTon::getInstance(); @@ -17,14 +19,14 @@ public function testShoudBeTheSameInstanceForTwoObjets(): void $this->assertSame($firstInstance, $secondInstance); } - public function testThrowErrorWhenTryToCreateInstance(): void + public function testShouldThrowErrorWhenTryToCreateInstance(): void { $this->expectException(Error::class); $instance = new Singleton(); } - public function testThrowExceptionWhenTryToClone(): void + public function testShouldThrowErrorWhenTryToClone(): void { $this->expectException(Error::class); @@ -32,9 +34,10 @@ public function testThrowExceptionWhenTryToClone(): void $clone = clone $instance; } - public function testThrowExceptionWhenTryToSerialize(): void + public function testShouldThrowExceptionWhenTryToSerialize(): void { - $this->expectException(Error::class); + $this->expectException(LogicException::class); + $this->expectExceptionMessage('Cannot serialize a singleton.'); $instance = Singleton::getInstance(); $serialize = serialize($instance); From f9c3f5c1be797949096e307b8e776a2f5bef2e44 Mon Sep 17 00:00:00 2001 From: Walmir Silva Date: Thu, 4 Nov 2021 21:31:45 -0300 Subject: [PATCH 5/5] Make readme file --- README.md | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 829709b..5e4f01e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,45 @@ -# design-patterns -Exemplos em PHP dos 23 Padrões de Projetos (Design Patterns) relacionados ao Livro GOF +# Padrões de Projetos (Design Patterns) + +Exemplos em PHP dos 23 Padrões de Projetos (Design Patterns) relacionados ao Livro GOF. + +Procurei organizar todos os padrões segundo a sua categoria, separados por pastas e em cada pasta coloquei o diarama UML para ajudar no entendimento. + +Casos você queira se aprofundar no tema, não deixe de conheceu o site Growth Dev: +[https://growthdev.com.br](https://growthdev.com.br/) + +## Organização do Projeto: + +- `src/` + - `Behavioral/` + - `Creational/` + - `Structural/` + +A estrutura da pastas de `tests/` segue a mesma estrutura + +## Instruções + +Este projetos tem um arquivo `Makefile` para a execução dos testes + +1. Faça clone deste projeto: + + `git clone https://github.com/growthdev-repo/design-patterns.git` + +2. Entre no na pasta do projeto: + + `cd design-patterns` + +3. Execute a instalação dos pacotes do `ccomposer`: + + `composer intall` + +4. Para executar os testes dos padrões basta executar no terminal: + + `make test` + + +## Sobre nós + +Este projeto foi desenvoldido por Walmir Silva autor do blog [https://growthdev.com.br](https://growthdev.com.br/) + + +