diff --git a/.gitignore b/.gitignore index c6fa3e1..b92f090 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ .idea /nbproject /vendor/ -_build/ +/_build/ *.mo .vagrant/ phpunit.xml diff --git a/Creational/AbstractFactory/CsvParser.php b/Creational/AbstractFactory/CsvParser.php deleted file mode 100644 index 5b02697..0000000 --- a/Creational/AbstractFactory/CsvParser.php +++ /dev/null @@ -1,36 +0,0 @@ -skipHeaderLine = $skipHeaderLine; - } - - public function parse(string $input): array - { - $headerWasParsed = false; - $parsedLines = []; - - foreach (explode(PHP_EOL, $input) as $line) { - if (!$headerWasParsed && $this->skipHeaderLine === self::OPTION_CONTAINS_HEADER) { - $headerWasParsed = true; - continue; - } - - $parsedLines[] = str_getcsv($line); - } - - return $parsedLines; - } -} diff --git a/Creational/AbstractFactory/DigitalProduct.php b/Creational/AbstractFactory/DigitalProduct.php new file mode 100644 index 0000000..f4c3ac5 --- /dev/null +++ b/Creational/AbstractFactory/DigitalProduct.php @@ -0,0 +1,21 @@ +price = $price; + } + + public function calculatePrice(): int + { + return $this->price; + } +} diff --git a/Creational/AbstractFactory/JsonParser.php b/Creational/AbstractFactory/JsonParser.php deleted file mode 100644 index 306984b..0000000 --- a/Creational/AbstractFactory/JsonParser.php +++ /dev/null @@ -1,11 +0,0 @@ -productPrice = $productPrice; + $this->shippingCosts = $shippingCosts; + } + + public function calculatePrice(): int + { + return $this->productPrice + $this->shippingCosts; + } +} diff --git a/Creational/AbstractFactory/Tests/AbstractFactoryTest.php b/Creational/AbstractFactory/Tests/AbstractFactoryTest.php index 4ef2433..cd49633 100644 --- a/Creational/AbstractFactory/Tests/AbstractFactoryTest.php +++ b/Creational/AbstractFactory/Tests/AbstractFactoryTest.php @@ -2,59 +2,42 @@ namespace DesignPatterns\Creational\AbstractFactory\Tests; -use DesignPatterns\Creational\AbstractFactory\CsvParser; -use DesignPatterns\Creational\AbstractFactory\JsonParser; -use DesignPatterns\Creational\AbstractFactory\ParserFactory; +use DesignPatterns\Creational\AbstractFactory\DigitalProduct; +use DesignPatterns\Creational\AbstractFactory\ProductFactory; +use DesignPatterns\Creational\AbstractFactory\ShippableProduct; use PHPUnit\Framework\TestCase; class AbstractFactoryTest extends TestCase { - public function testCanCreateCsvParser() + public function testCanCreateDigitalProduct() { - $factory = new ParserFactory(); - $parser = $factory->createCsvParser(CsvParser::OPTION_CONTAINS_HEADER); + $factory = new ProductFactory(); + $product = $factory->createDigitalProduct(150); - $this->assertInstanceOf(CsvParser::class, $parser); + $this->assertInstanceOf(DigitalProduct::class, $product); } - public function testCsvParserCanParse() + public function testCanCreateShippableProduct() { - $factory = new ParserFactory(); - $parser = $factory->createCsvParser(CsvParser::OPTION_CONTAINS_NO_HEADER); + $factory = new ProductFactory(); + $product = $factory->createShippableProduct(150); - $result = $parser->parse("A0,A1,A2\nB0,B1,B2\nC0,C1,C2"); - - $this->assertEquals( - [ - ['A0', 'A1', 'A2'], - ['B0', 'B1', 'B2'], - ['C0', 'C1', 'C2'] - ], - $result - ); + $this->assertInstanceOf(ShippableProduct::class, $product); } - public function testCsvParserCanSkipHeader() + public function testCanCalculatePriceForDigitalProduct() { - $factory = new ParserFactory(); - $parser = $factory->createCsvParser(CsvParser::OPTION_CONTAINS_HEADER); + $factory = new ProductFactory(); + $product = $factory->createDigitalProduct(150); - $result = $parser->parse("A0,A1,A2\nB0,B1,B2\nC0,C1,C2"); - - $this->assertEquals( - [ - ['B0', 'B1', 'B2'], - ['C0', 'C1', 'C2'] - ], - $result - ); + $this->assertEquals(150, $product->calculatePrice()); } - public function testCanCreateJsonParser() + public function testCanCalculatePriceForShippableProduct() { - $factory = new ParserFactory(); - $parser = $factory->createJsonParser(); + $factory = new ProductFactory(); + $product = $factory->createShippableProduct(150); - $this->assertInstanceOf(JsonParser::class, $parser); + $this->assertEquals(200, $product->calculatePrice()); } } diff --git a/Creational/AbstractFactory/uml/AbstractFactory.uml b/Creational/AbstractFactory/uml/AbstractFactory.uml index bcd4efb..71303d2 100644 --- a/Creational/AbstractFactory/uml/AbstractFactory.uml +++ b/Creational/AbstractFactory/uml/AbstractFactory.uml @@ -1,29 +1,29 @@ PHP - \DesignPatterns\Creational\AbstractFactory\CsvParser + \DesignPatterns\Creational\AbstractFactory\DigitalProduct - \DesignPatterns\Creational\AbstractFactory\Parser - \DesignPatterns\Creational\AbstractFactory\ParserFactory - \DesignPatterns\Creational\AbstractFactory\JsonParser - \DesignPatterns\Creational\AbstractFactory\CsvParser + \DesignPatterns\Creational\AbstractFactory\DigitalProduct + \DesignPatterns\Creational\AbstractFactory\ProductFactory + \DesignPatterns\Creational\AbstractFactory\ShippableProduct + \DesignPatterns\Creational\AbstractFactory\Product - - - - - + + + + + - - - - - + + + + + - + Fields diff --git a/Creational/AbstractFactory/uml/uml.png b/Creational/AbstractFactory/uml/uml.png index fc1db5d..1545e2b 100644 Binary files a/Creational/AbstractFactory/uml/uml.png and b/Creational/AbstractFactory/uml/uml.png differ diff --git a/Creational/AbstractFactory/uml/uml.svg b/Creational/AbstractFactory/uml/uml.svg index ecd8021..4b448a1 100644 --- a/Creational/AbstractFactory/uml/uml.svg +++ b/Creational/AbstractFactory/uml/uml.svg @@ -1,82 +1,88 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + + + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + - - - + + + diff --git a/Dockerfile b/Dockerfile index de87d01..c3f3b9e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,6 +3,7 @@ WORKDIR /app RUN apt-get update \ && apt-get install -y libzip-dev wget git-core python-pip \ && docker-php-ext-install zip \ - && pip install Sphinx + && pip install Sphinx sphinx_rtd_theme ADD . /app -CMD [ "make", "cs", "test" ] +RUN cd /app && make cs test html +CMD [ "php", "-S", "0.0.0.0:80", "-t", "_build/html" ] diff --git a/Makefile b/Makefile index ff8edc0..560c62d 100644 --- a/Makefile +++ b/Makefile @@ -200,7 +200,7 @@ vendor: composer.phar php composer.phar install cs: install - ./vendor/bin/phpcs -p --standard=PSR2 --ignore=vendor . + ./vendor/bin/phpcs -p --standard=PSR2 --ignore=vendor,_build . -test: install cs +test: install ./vendor/bin/phpunit diff --git a/README.md b/README.md index cf52c8c..1b2da37 100644 --- a/README.md +++ b/README.md @@ -29,17 +29,20 @@ $ ./vendor/bin/phpunit ## using Docker (optional) -You can optionally run tests using [Docker for Mac](https://www.docker.com/docker-mac) or [Windows](https://www.docker.com/docker-windows) or native one for [Linux](https://www.docker.com/docker-debian). +You can optionally build and browse the documentation using [Docker for Mac, Windows or Linux](https://docs.docker.com/compose/install/). + Just run: ```bash $ docker-compose up ``` +Go to [http://localhost:8080/README.html](http://localhost:8080/README.html) to read the generated documentation. + To only install the dependencies, use `docker-compose` like this: ```bash -$ docker-compose run composer install +$ docker-compose run php php composer.phar install ``` ## Patterns diff --git a/docker-compose.yml b/docker-compose.yml index af6b1cc..bd0338e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,5 +4,7 @@ services: build: context: . dockerfile: Dockerfile + ports: + - "8080:80" volumes: - .:/app diff --git a/docker/install-composer.sh b/docker/install-composer.sh old mode 100644 new mode 100755