Skip to content

Commit

Permalink
refactor: Adds Improvements in testing, model collections. (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavofreze authored Sep 27, 2024
1 parent 77aea7e commit 4d182f4
Show file tree
Hide file tree
Showing 30 changed files with 231 additions and 425 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LABEL author="Gustavo Freze" \

ARG FLYWAY_VERSION=10.10.0

RUN apk --no-cache add tar curl mysql-client openjdk21-jre \
RUN apk --no-cache add curl mysql-client openjdk21-jre tar \
&& mkdir -p /opt/flyway \
&& curl -L "https://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/${FLYWAY_VERSION}/flyway-commandline-${FLYWAY_VERSION}-linux-x64.tar.gz" | tar -xz --strip-components=1 -C /opt/flyway \
&& rm -f /opt/flyway/jre/bin/java \
Expand Down
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@
"slim/slim": "^4.13",
"doctrine/dbal": "^3.8",
"tiny-blocks/http": "^3.1",
"tiny-blocks/collection": "^1",
"php-di/slim-bridge": "^3.4",
"ext-pdo": "*",
"ext-bcmath": "*"
},
"require-dev": {
"phpmd/phpmd": "^2.15",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.5",
"infection/infection": "^0.27",
"squizlabs/php_codesniffer": "^3.9",
"dg/bypass-finals": "^1.6"
"phpstan/phpstan": "^1",
"phpunit/phpunit": "^11",
"dg/bypass-finals": "^1.6",
"infection/infection": "^0.29",
"squizlabs/php_codesniffer": "^3.10"
},
"config": {
"sort-packages": true,
Expand All @@ -41,7 +42,7 @@
}
},
"scripts": {
"test": "phpunit -c phpunit.xml --log-junit=report/coverage/junit.xml --coverage-xml=report/coverage/coverage-xml --coverage-html=report/coverage/coverage-html tests",
"test": "phpunit -c phpunit.xml --log-junit=report/coverage/junit.xml --coverage-xml=report/coverage/coverage-xml --coverage-html=report/coverage/coverage-html --testsuite all",
"phpcs": "phpcs --standard=PSR12 --extensions=php ./src",
"phpmd": "phpmd ./src text phpmd.xml --suffixes php --exclude --ignore-violations-on-exit",
"phpstan": "phpstan analyse -c phpstan.neon.dist --quiet --no-progress",
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ parameters:
level: 9
tmpDir: report/phpstan
ignoreErrors:
- '#Parameter ...#'
- '#with generic#'
- '#does not accept mixed#'
- '#extends generic class#'
- '#expects string\|null, mixed given#'
- '#with no value type specified in iterable type array#'
- '#expects class-string<T of object>\|T of object, string given#'
Expand Down
50 changes: 30 additions & 20 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
bootstrap="tests/bootstrap.php"
backupGlobals="false"
stopOnFailure="false"
executionOrder="default"
cacheResultFile="report/.phpunit.result.cache"
processIsolation="false"
backupStaticProperties="false">
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true"
cacheDirectory=".phpunit.cache"
beStrictAboutOutputDuringTests="true">

<source>
<include>
<directory>src</directory>
</include>
<exclude>
<file>src/Routes.php</file>
<file>src/Dependencies.php</file>
</exclude>
</source>

<testsuites>
<testsuite name="all">
<directory suffix="Test.php">tests</directory>
<directory>tests</directory>
</testsuite>
<testsuite name="unit">
<directory suffix="Test.php">tests/Unit</directory>
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="integration">
<directory suffix="Test.php">tests/Integration</directory>
<directory>tests/Integration</directory>
</testsuite>
</testsuites>

<source>
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<file>src/Routes.php</file>
<file>src/Dependencies.php</file>
</exclude>
</source>
<coverage>
<report>
<text outputFile="report/coverage.txt"/>
<html outputDirectory="report/html/"/>
<clover outputFile="report/coverage-clover.xml"/>
</report>
</coverage>

<logging>
<junit outputFile="report/execution-result.xml"/>
</logging>

<php>
<env name="CHEAP_DELIVERY_HOST" value="cheap-delivery.localhost"/>
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Domain/Events/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CheapDelivery\Application\Domain\Events;

use CheapDelivery\Application\Domain\Models\Commons\Collection;
use TinyBlocks\Collection\Collection;

final class Events extends Collection
{
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Domain/Models/Carriers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CheapDelivery\Application\Domain\Models;

use CheapDelivery\Application\Domain\Models\Commons\Collection;
use TinyBlocks\Collection\Collection;

final class Carriers extends Collection
{
Expand Down
75 changes: 0 additions & 75 deletions src/Application/Domain/Models/Commons/Collectible.php

This file was deleted.

83 changes: 0 additions & 83 deletions src/Application/Domain/Models/Commons/Collection.php

This file was deleted.

21 changes: 13 additions & 8 deletions src/Application/Domain/Models/Dispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,35 @@
use CheapDelivery\Application\Domain\Events\Events;
use CheapDelivery\Application\Domain\Exceptions\NoEligibleCarriers;
use CheapDelivery\Application\Domain\Models\Commons\Utc;
use TinyBlocks\Collection\Collectible;
use TinyBlocks\Collection\Internal\Operations\Order\Order;

final class Dispatch
{
private Events $events;
private Events|Collectible $events;

public function __construct(public DispatchId $id, public ?Shipment $shipment = null)
{
$this->events = new Events();
$this->events = Events::createFromEmpty();
}

public static function create(): Dispatch
{
return new Dispatch(id: DispatchId::create());
}

public function dispatchWithLowestCost(Weight $weight, Distance $distance, Carriers $carriers): Dispatch
public function dispatchWithLowestCost(Weight $weight, Distance $distance, Carriers|Collectible $carriers): Dispatch
{
$shipments = Shipments::from(weight: $weight, distance: $distance, carriers: $carriers);

/** @var Shipment|null $shipment */
$shipment = $shipments
->filter()
->map(callback: fn(Shipment $shipment) => $shipment)
->minBy(callback: fn(Shipment $shipment) => $shipment->cost->value);
->map(transformations: fn(Shipment $shipment) => $shipment)
->sort(order: Order::ASCENDING_VALUE, predicate: fn(
Shipment $first,
Shipment $second
) => $first->cost->value <=> $second->cost->value)
->first();

if (is_null($shipment)) {
throw new NoEligibleCarriers();
Expand All @@ -43,8 +48,8 @@ public function dispatchWithLowestCost(Weight $weight, Distance $distance, Carri
return $this;
}

public function occurredEvents(): Events
public function occurredEvents(): Events|Collectible
{
return new Events(elements: $this->events->all());
return Events::createFrom(elements: $this->events);
}
}
14 changes: 9 additions & 5 deletions src/Application/Domain/Models/Shipments.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@
namespace CheapDelivery\Application\Domain\Models;

use CheapDelivery\Application\Domain\Exceptions\NoCarriersAvailable;
use CheapDelivery\Application\Domain\Models\Commons\Collection;
use TinyBlocks\Collection\Collectible;
use TinyBlocks\Collection\Collection;

final class Shipments extends Collection
{
public static function from(Weight $weight, Distance $distance, Carriers $carriers): Shipments
{
public static function from(
Weight $weight,
Distance $distance,
Carriers|Collectible $carriers
): Shipments|Collectible {
if ($carriers->isEmpty()) {
throw new NoCarriersAvailable();
}

$shipments = $carriers->map(callback: fn(Carrier $carrier) => $carrier->shipment(
$shipments = $carriers->map(transformations: fn(Carrier $carrier) => $carrier->shipment(
weight: $weight,
distance: $distance
));

return new Shipments(elements: $shipments);
return Shipments::createFrom(elements: $shipments);
}
}
5 changes: 3 additions & 2 deletions src/Application/Ports/Outbound/Carriers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace CheapDelivery\Application\Ports\Outbound;

use CheapDelivery\Application\Domain\Models\Carriers as CarriersCollection;
use TinyBlocks\Collection\Collectible;

interface Carriers
{
/**
* Retrieves all carriers from the repository.
*
* @return CarriersCollection The collection of carriers.
* @return CarriersCollection|Collectible The collection of carriers.
*/
public function findAll(): CarriersCollection;
public function findAll(): CarriersCollection|Collectible;
}
Loading

0 comments on commit 4d182f4

Please sign in to comment.