Skip to content

Feature/setup #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 1 addition & 40 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.PHONY: test
test:
@echo "Design Pattern Tests"
php ./vendor/bin/phpunit --bootstrap ./vendor/autoload.php ./tests/ --testdox --colors
47 changes: 45 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/)



28 changes: 28 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"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/"
}
},
"autoload-dev": {
"psr-4": {
"Growthdev\\DesignPatterns\\Tests\\": "tests/"
}
},
"authors": [
{
"name": "Walmir Silva",
"email": "git@growthdev.com.br"
}
]
}
39 changes: 39 additions & 0 deletions src/Creational/Singleton/Singleton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Creational\Singleton;

use LogicException;

final class Singleton
{
private static ?Singleton $instance = null;

public static function getInstance(): Singleton
{
if (null === static::$instance) {
static::$instance = new static();
}

return static::$instance;
}

protected function __construct()
{
}

private function __clone(): void
{
}

public function __sleep()
{
throw new LogicException('Cannot serialize a singleton.');
}

public function __wakeup(): void
{
throw new LogicException('Cannot unserialize a singleton.');
}
}
45 changes: 45 additions & 0 deletions tests/Creational/Singleton/SingletonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Creational\Singleton;

use Growthdev\DesignPatterns\Creational\Singleton\Singleton;
use Error;
use LogicException;
use PHPUnit\Framework\TestCase;

final class SingletonTest extends TestCase
{
public function testShouldBeTheSameInstanceForTwoObjets(): void
{
$firstInstance = Singleton::getInstance();
$secondInstance = SingleTon::getInstance();

$this->assertSame($firstInstance, $secondInstance);
}

public function testShouldThrowErrorWhenTryToCreateInstance(): void
{
$this->expectException(Error::class);

$instance = new Singleton();
}

public function testShouldThrowErrorWhenTryToClone(): void
{
$this->expectException(Error::class);

$instance = Singleton::getInstance();
$clone = clone $instance;
}

public function testShouldThrowExceptionWhenTryToSerialize(): void
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Cannot serialize a singleton.');

$instance = Singleton::getInstance();
$serialize = serialize($instance);
}
}