File tree Expand file tree Collapse file tree 6 files changed +162
-42
lines changed
tests/Creational/Singleton Expand file tree Collapse file tree 6 files changed +162
-42
lines changed Original file line number Diff line number Diff line change 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
-
26
1
# Managed by Composer
27
- /app /bootstrap.php.cache
28
- /var /bootstrap.php.cache
29
- /bin /*
30
- ! bin /console
31
- ! bin /symfony_requirements
32
2
/vendor /
33
3
34
- # Assets and user uploads
35
- /web /bundles /
36
- /web /uploads /
37
-
38
4
# PHPUnit
39
5
/app /phpunit.xml
40
6
/phpunit.xml
44
10
45
11
# Composer PHAR
46
12
/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
Original file line number Diff line number Diff line change
1
+ .PHONY : test
2
+ test :
3
+ @echo " Design Pattern Tests"
4
+ php ./vendor/bin/phpunit --bootstrap ./vendor/autoload.php ./tests/ --testdox --colors
Original file line number Diff line number Diff line change 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
+
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments