Skip to content

Commit

Permalink
feat: Updates PHP version, refactor models, add event emission and ot…
Browse files Browse the repository at this point in the history
…her adjustments.
  • Loading branch information
gustavofreze committed Feb 24, 2024
1 parent 6431dfd commit dcfac94
Show file tree
Hide file tree
Showing 20 changed files with 107 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LABEL author="Gustavo Freze" \
org.label-schema.vcs-url="https://github.com/gustavofreze/cheap-delivery/blob/main/Dockerfile" \
org.label-schema.schema-version="1.0"

ARG FLYWAY_VERSION=10.6.0
ARG FLYWAY_VERSION=10.8.1

RUN docker-php-ext-install pdo_mysql

Expand Down
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ IMAGE = gustavofreze/php:8.2
DOCKER_RUN = docker run -u root --rm -it --net=host -v ${PWD}:/app -w /app ${IMAGE}
DOCKER_EXEC = docker exec -it cheap-delivery

FLYWAY = docker run --rm -v ${PWD}/db/mysql/migrations:/flyway/sql --env-file=config/local.env --link cheap-delivery-adm --network=cheap-delivery_default -e FLYWAY_EDITION="community" flyway/flyway:10.6.0
FLYWAY = docker run --rm -v ${PWD}/db/mysql/migrations:/flyway/sql --env-file=config/local.env --link cheap-delivery-adm --network=cheap-delivery_default -e FLYWAY_EDITION="community" flyway/flyway:10.8.1
MIGRATE_DB = ${FLYWAY} -locations=filesystem:/flyway/sql -schemas=cheap_delivery_adm

.PHONY: configure run test test-no-coverage review show-coverage clean migrate-database clean-database
.PHONY: configure run test test-no-coverage review fix-style show-coverage clean migrate-database clean-database

configure:
@docker-compose up -d --build
Expand All @@ -14,7 +14,7 @@ configure-local:
@${DOCKER_RUN} composer update --optimize-autoloader

test:
@${DOCKER_RUN} composer run test
@${DOCKER_RUN} composer run tests

test-no-coverage:
@${DOCKER_RUN} composer run test-no-coverage
Expand All @@ -28,12 +28,15 @@ test-integration:
review:
@${DOCKER_RUN} composer review

fix-style:
@${DOCKER_RUN} composer fix-style

show-coverage:
@sensible-browser report/coverage/coverage-html/index.html
@sensible-browser report/coverage/coverage-html/index.html report/coverage/mutation-report.html

clean:
@sudo chown -R ${USER}:${USER} ${PWD}
@rm -rf report vendor *.phpunit *.lock
@rm -rf report vendor *.lock .phpunit.cache

migrate-database:
@${MIGRATE_DB} migrate
Expand Down
26 changes: 18 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
"license": "MIT",
"minimum-stability": "stable",
"prefer-stable": true,
"config": {
"sort-packages": true
},
"autoload": {
"psr-4": {
"CheapDelivery\\": "src/"
Expand All @@ -24,7 +21,7 @@
"require": {
"php": "^8.2",
"slim/psr7": "^1.6",
"slim/slim": "^4.11",
"slim/slim": "^4.12",
"doctrine/dbal": "^3.8",
"tiny-blocks/http": "^3.0",
"php-di/slim-bridge": "^3.4",
Expand All @@ -33,20 +30,33 @@
},
"require-dev": {
"phpmd/phpmd": "^2.15",
"phpunit/phpunit": "^10",
"squizlabs/php_codesniffer": "^3.8",
"dg/bypass-finals": "^1.4"
"phpunit/phpunit": "^10.5",
"infection/infection": "^0.27",
"squizlabs/php_codesniffer": "^3.9",
"dg/bypass-finals": "^1.6"
},
"config": {
"sort-packages": true,
"allow-plugins": {
"infection/extension-installer": true
}
},
"scripts": {
"phpcs": "phpcs --standard=PSR12 --extensions=php ./src",
"phpmd": "phpmd ./src text phpmd.xml --suffixes php --exclude --ignore-violations-on-exit",
"test": "phpunit -c phpunit.xml --testsuite all",
"test": "phpunit -c phpunit.xml --log-junit=report/coverage/junit.xml --coverage-xml=report/coverage/coverage-xml --coverage-html=report/coverage/coverage-html tests",
"fix-style": "phpcbf ./src --extensions=php",
"test-unit": "phpunit -c phpunit.xml --no-coverage --testsuite unit",
"test-mutation": "infection --only-covered --logger-html=report/coverage/mutation-report.html --coverage=report/coverage --min-msi=100 --min-covered-msi=100 --threads=4",
"test-no-coverage": "phpunit -c phpunit.xml --no-coverage --testsuite all",
"test-integration": "phpunit -c phpunit.xml --no-coverage --testsuite integration",
"review": [
"@phpcs",
"@phpmd"
],
"tests": [
"@test",
"@test-mutation"
]
}
}
3 changes: 2 additions & 1 deletion config/local.env
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ MYSQL_DATABASE_USER=root
MYSQL_DATABASE_PASSWORD=root

# Flyway
FLYWAY_URL=jdbc:mysql://cheap-delivery-adm:3306/cheap_delivery_adm?useUnicode=yes&characterEncoding=UTF-8
FLYWAY_URL=jdbc:mysql://cheap-delivery-adm:3306?useUnicode=yes&characterEncoding=UTF-8
FLYWAY_USER=root
FLYWAY_TABLE=schema_history
FLYWAY_SCHEMAS=cheap_delivery_adm
FLYWAY_EDITION=community
FLYWAY_PASSWORD=root
FLYWAY_LOCATIONS=filesystem:/var/www/html/db/mysql/migrations
FLYWAY_CLEAN_DISABLED=false
FLYWAY_VALIDATE_MIGRATION_NAMING=true
29 changes: 29 additions & 0 deletions infection.json.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "vendor/infection/infection/resources/schema.json",
"tmpDir": "report/",
"logs": {
"text": "report/logs/infection-text.log",
"summary": "report/logs/infection-summary.log"
},
"source": {
"directories": [
"src"
],
"excludes": [
"src/Application/Domain/Models/Commons/Uuid.php"
]
},
"timeout": 10,
"mutators": {
"@default": true,
"BCMath": false,
"TrueValue": false,
"InstanceOf_": false,
"CloneRemoval": false,
"MatchArmRemoval": false,
"PublicVisibility": false,
"MethodCallRemoval": false,
"ProtectedVisibility": false
},
"testFramework": "phpunit"
}
17 changes: 3 additions & 14 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true" processIsolation="false"
colors="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
cacheDirectory=".phpunit.cache"
cacheResultFile="report/.phpunit.result.cache"
backupStaticProperties="false">

<coverage>
<report>
<clover outputFile="report/coverage-clover.xml"/>
<html outputDirectory="report/html/"/>
<text outputFile="report/coverage.txt"/>
</report>
</coverage>

<testsuites>
<testsuite name="all">
<directory suffix="Test.php">tests</directory>
Expand All @@ -28,10 +21,6 @@
</testsuite>
</testsuites>

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

<source>
<include>
<directory suffix=".php">src</directory>
Expand Down
1 change: 0 additions & 1 deletion src/Application/Commands/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@
*/
interface Command
{

}
21 changes: 6 additions & 15 deletions src/Application/Domain/Models/Commons/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

namespace CheapDelivery\Application\Domain\Models\Commons;

use BackedEnum;
use Closure;
use Traversable;

trait Collection
{
public function __construct(public mixed $items = [])
public function __construct(public iterable $items = [])
{
$this->items = $this->toArrayFrom(items: $items);
$this->items = $this->normalize(items: $items);
}

public function add(mixed $item): static
Expand All @@ -22,10 +21,7 @@ public function add(mixed $item): static

public function map(Closure $callback): static
{
$keys = array_keys($this->items);
$items = array_map($callback, $this->items, $keys);

return new static(array_combine($keys, $items));
return new static(array_map($callback, $this->items));
}

public function all(): array
Expand All @@ -51,7 +47,7 @@ public function minBy(Closure $callback): mixed
}

$sorted = clone $this;
$sorted->sortByAsc(callback: fn(mixed $first, mixed $second) => $callback($first) <=> $callback($second));
$sorted->sortByAsc(callback: fn(mixed $first, mixed $second) => ($callback($first) <=> $callback($second)));

return $sorted->first();
}
Expand Down Expand Up @@ -80,16 +76,11 @@ public function isEmpty(): bool
return empty($this->items);
}

private function toArrayFrom(mixed $items): array
private function normalize(iterable $items): array
{
if (is_array($items)) {
return $items;
}

return match (true) {
$items instanceof BackedEnum => [$items],
$items instanceof Traversable => iterator_to_array($items),
default => (array)$items
default => $items
};
}
}
1 change: 0 additions & 1 deletion src/Application/Domain/Models/Cost.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

final class Cost extends PositiveDecimal
{

}
1 change: 0 additions & 1 deletion src/Application/Domain/Models/Distance.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

final class Distance extends PositiveDecimal
{

}
1 change: 0 additions & 1 deletion src/Application/Domain/Models/Weight.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

final class Weight extends PositiveDecimal
{

}
7 changes: 6 additions & 1 deletion src/Driven/Shared/OutboxEvent/Commons/Snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@

interface Snapshot
{

/**
* Convert the snapshot to an array.
*
* @return array The snapshot as an array.
*/
public function toArray(): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function handle(Throwable $exception): ResponseInterface
return match (get_class($exception)) {
NoEligibleCarriers::class => HttpResponse::badRequest(data: $error),
NoCarriersAvailable::class => HttpResponse::notFound(data: $error),
default => HttpResponse::internalServerError(data: $error),
default => HttpResponse::internalServerError(data: $error)
};
}
}
9 changes: 5 additions & 4 deletions src/Query/Shipment/Database/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ final class Builder
public static function from(array $shipments): array
{
$mapper = fn(array $shipment) => [
'id' => $shipment['id'],
'cost' => floatval($shipment['cost']),
'carrier' => [
'id' => $shipment['id'],
'cost' => floatval($shipment['cost']),
'carrier' => [
'name' => $shipment['carrierName']
]
],
'createdAt' => $shipment['createdAt']
];

return array_map(fn(array $shipment) => $mapper(shipment: $shipment), $shipments);
Expand Down
18 changes: 13 additions & 5 deletions src/Query/Shipment/Database/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@

namespace CheapDelivery\Query\Shipment\Database;

use Doctrine\DBAL\Connection;
use CheapDelivery\Query\QueryBuilder;

final readonly class Facade
{
public function __construct(private Connection $connection)
public function __construct(private QueryBuilder $queryBuilder)
{
}

public function findAll(): array
public function findAll(ShipmentFilters $filters): array
{
$shipments = $this->connection
->executeQuery(Queries::FIND_ALL)
$shipments = $this->queryBuilder
->select([
'cost',
'BIN_TO_UUID(id) AS id',
'created_at AS createdAt',
'carrier_name AS carrierName'
])
->from('shipment')
->applyFilters($filters)
->executeQuery()
->fetchAllAssociative();

return Builder::from(shipments: $shipments);
Expand Down
8 changes: 0 additions & 8 deletions src/Query/Shipment/Database/Queries.php

This file was deleted.

4 changes: 3 additions & 1 deletion src/Query/Shipment/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CheapDelivery\Query\Shipment;

use CheapDelivery\Query\Shipment\Database\Facade;
use CheapDelivery\Query\Shipment\Database\ShipmentFilters;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
Expand All @@ -16,7 +17,8 @@ public function __construct(private Facade $facade)

public function handle(ServerRequestInterface $request): ResponseInterface
{
$shipments = $this->facade->findAll();
$filters = ShipmentFilters::from($request->getQueryParams());
$shipments = $this->facade->findAll($filters);

return HttpResponse::ok(data: $shipments);
}
Expand Down
5 changes: 4 additions & 1 deletion tests/Unit/Application/Domain/Models/CostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function testExceptionWhenNonPositiveValue(float $value): void

public static function invalidValueProvider(): array
{
return [[0.0], [-1.0]];
return [
'zero' => ['value' => 0.0],
'negative' => ['value' => -1.0]
];
}
}
5 changes: 4 additions & 1 deletion tests/Unit/Application/Domain/Models/DistanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function testExceptionWhenNonPositiveValue(float $value): void

public static function invalidValueProvider(): array
{
return [[0.0], [-1.0]];
return [
'zero' => ['value' => 0.0],
'negative' => ['value' => -1.0]
];
}
}
5 changes: 4 additions & 1 deletion tests/Unit/Application/Domain/Models/WeightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function testExceptionWhenNonPositiveValue(float $value): void

public static function invalidValueProvider(): array
{
return [[0.0], [-1.0]];
return [
'zero' => ['value' => 0.0],
'negative' => ['value' => -1.0]
];
}
}

0 comments on commit dcfac94

Please sign in to comment.