Skip to content

Commit e088ad4

Browse files
authored
Merge pull request #1 from growthdev-repo/feature/setup
Feature/setup
2 parents 5501628 + f9c3f5c commit e088ad4

File tree

6 files changed

+162
-42
lines changed

6 files changed

+162
-42
lines changed

.gitignore

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,6 @@
1-
# Cache and logs (Symfony2)
2-
/app/cache/*
3-
/app/logs/*
4-
!app/cache/.gitkeep
5-
!app/logs/.gitkeep
6-
7-
# Email spool folder
8-
/app/spool/*
9-
10-
# Cache, session files and logs (Symfony3)
11-
/var/cache/*
12-
/var/logs/*
13-
/var/sessions/*
14-
!var/cache/.gitkeep
15-
!var/logs/.gitkeep
16-
!var/sessions/.gitkeep
17-
18-
# Logs (Symfony4)
19-
/var/log/*
20-
!var/log/.gitkeep
21-
22-
# Parameters
23-
/app/config/parameters.yml
24-
/app/config/parameters.ini
25-
261
# Managed by Composer
27-
/app/bootstrap.php.cache
28-
/var/bootstrap.php.cache
29-
/bin/*
30-
!bin/console
31-
!bin/symfony_requirements
322
/vendor/
333

34-
# Assets and user uploads
35-
/web/bundles/
36-
/web/uploads/
37-
384
# PHPUnit
395
/app/phpunit.xml
406
/phpunit.xml
@@ -44,9 +10,4 @@
4410

4511
# Composer PHAR
4612
/composer.phar
47-
48-
# Backup entities generated with doctrine:generate:entities command
49-
**/Entity/*~
50-
51-
# Embedded web-server pid file
52-
/.web-server-pid
13+
/composer.lock

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.PHONY: test
2+
test:
3+
@echo "Design Pattern Tests"
4+
php ./vendor/bin/phpunit --bootstrap ./vendor/autoload.php ./tests/ --testdox --colors

README.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,45 @@
1-
# design-patterns
2-
Exemplos em PHP dos 23 Padrões de Projetos (Design Patterns) relacionados ao Livro GOF
1+
# Padrões de Projetos (Design Patterns)
2+
3+
Exemplos em PHP dos 23 Padrões de Projetos (Design Patterns) relacionados ao Livro GOF.
4+
5+
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.
6+
7+
Casos você queira se aprofundar no tema, não deixe de conheceu o site Growth Dev:
8+
[https://growthdev.com.br](https://growthdev.com.br/)
9+
10+
## Organização do Projeto:
11+
12+
- `src/`
13+
- `Behavioral/`
14+
- `Creational/`
15+
- `Structural/`
16+
17+
A estrutura da pastas de `tests/` segue a mesma estrutura
18+
19+
## Instruções
20+
21+
Este projetos tem um arquivo `Makefile` para a execução dos testes
22+
23+
1. Faça clone deste projeto:
24+
25+
`git clone https://github.com/growthdev-repo/design-patterns.git`
26+
27+
2. Entre no na pasta do projeto:
28+
29+
`cd design-patterns`
30+
31+
3. Execute a instalação dos pacotes do `ccomposer`:
32+
33+
`composer intall`
34+
35+
4. Para executar os testes dos padrões basta executar no terminal:
36+
37+
`make test`
38+
39+
40+
## Sobre nós
41+
42+
Este projeto foi desenvoldido por Walmir Silva autor do blog [https://growthdev.com.br](https://growthdev.com.br/)
43+
44+
45+

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "growthdev/design-patterns",
3+
"description": "Exemplos em PHP dos 23 Padrões de Projetos (Design Patterns) relacionados ao Livro GOF",
4+
"type": "php",
5+
"require": {
6+
"php": "8.0.12"
7+
},
8+
"require-dev": {
9+
"phpunit/phpunit": "^9.5"
10+
},
11+
"license": "MIT",
12+
"autoload": {
13+
"psr-4": {
14+
"Growthdev\\DesignPatterns\\": "src/"
15+
}
16+
},
17+
"autoload-dev": {
18+
"psr-4": {
19+
"Growthdev\\DesignPatterns\\Tests\\": "tests/"
20+
}
21+
},
22+
"authors": [
23+
{
24+
"name": "Walmir Silva",
25+
"email": "git@growthdev.com.br"
26+
}
27+
]
28+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Growthdev\DesignPatterns\Creational\Singleton;
6+
7+
use LogicException;
8+
9+
final class Singleton
10+
{
11+
private static ?Singleton $instance = null;
12+
13+
public static function getInstance(): Singleton
14+
{
15+
if (null === static::$instance) {
16+
static::$instance = new static();
17+
}
18+
19+
return static::$instance;
20+
}
21+
22+
protected function __construct()
23+
{
24+
}
25+
26+
private function __clone(): void
27+
{
28+
}
29+
30+
public function __sleep()
31+
{
32+
throw new LogicException('Cannot serialize a singleton.');
33+
}
34+
35+
public function __wakeup(): void
36+
{
37+
throw new LogicException('Cannot unserialize a singleton.');
38+
}
39+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Growthdev\DesignPatterns\Creational\Singleton;
6+
7+
use Growthdev\DesignPatterns\Creational\Singleton\Singleton;
8+
use Error;
9+
use LogicException;
10+
use PHPUnit\Framework\TestCase;
11+
12+
final class SingletonTest extends TestCase
13+
{
14+
public function testShouldBeTheSameInstanceForTwoObjets(): void
15+
{
16+
$firstInstance = Singleton::getInstance();
17+
$secondInstance = SingleTon::getInstance();
18+
19+
$this->assertSame($firstInstance, $secondInstance);
20+
}
21+
22+
public function testShouldThrowErrorWhenTryToCreateInstance(): void
23+
{
24+
$this->expectException(Error::class);
25+
26+
$instance = new Singleton();
27+
}
28+
29+
public function testShouldThrowErrorWhenTryToClone(): void
30+
{
31+
$this->expectException(Error::class);
32+
33+
$instance = Singleton::getInstance();
34+
$clone = clone $instance;
35+
}
36+
37+
public function testShouldThrowExceptionWhenTryToSerialize(): void
38+
{
39+
$this->expectException(LogicException::class);
40+
$this->expectExceptionMessage('Cannot serialize a singleton.');
41+
42+
$instance = Singleton::getInstance();
43+
$serialize = serialize($instance);
44+
}
45+
}

0 commit comments

Comments
 (0)