-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Updates PHP version, refactor models, add event emission and ot…
…her adjustments.
- Loading branch information
1 parent
787f2cf
commit 7599ecb
Showing
9 changed files
with
254 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...tegration/Query/Shipment/QueryAdapter.php → ...Query/Shipment/Factories/QueryAdapter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Test\Integration\Query\Shipment\Factories; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
use Slim\Psr7\Headers; | ||
use Slim\Psr7\Request as SlimRequest; | ||
use Slim\Psr7\Stream; | ||
use Slim\Psr7\Uri; | ||
|
||
final class Request | ||
{ | ||
public static function getFrom(array $parameters): ServerRequestInterface | ||
{ | ||
$stream = fopen('php://memory', 'r+'); | ||
|
||
$uri = new Uri('https', 'cheap-delivery.localhost', null, '/', http_build_query($parameters)); | ||
$body = new Stream($stream); | ||
$headers = new Headers(['Content-Type' => 'application/json']); | ||
|
||
return new SlimRequest('GET', $uri, $headers, [], [], $body); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
tests/Unit/Driver/Http/Endpoints/CalculateShipment/CalculateShipmentExceptionHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
namespace CheapDelivery\Driver\Http\Endpoints\CalculateShipment; | ||
|
||
use CheapDelivery\Driver\Http\Endpoints\CalculateShipment\Factories\Request; | ||
use CheapDelivery\Driver\Http\Endpoints\CalculateShipment\Mocks\CalculateShipmentHandlerMock; | ||
use CheapDelivery\Driver\Http\Endpoints\CalculateShipment\Mocks\Exceptions; | ||
use CheapDelivery\Driver\Http\Middlewares\ErrorHandling; | ||
use PHPUnit\Framework\TestCase; | ||
use TinyBlocks\Http\HttpCode; | ||
|
||
class CalculateShipmentExceptionHandlerTest extends TestCase | ||
{ | ||
private CalculateShipment $endpoint; | ||
|
||
private ErrorHandling $middleware; | ||
|
||
protected function setUp(): void | ||
{ | ||
$useCase = new CalculateShipmentHandlerMock(); | ||
$this->endpoint = new CalculateShipment(useCase: $useCase); | ||
$exceptionHandler = new CalculateShipmentExceptionHandler(); | ||
$this->middleware = new ErrorHandling(exceptionHandler: $exceptionHandler); | ||
} | ||
|
||
public function testExceptionWhenNoEligibleCarriers(): void | ||
{ | ||
/** @Given that I have data to calculate a shipment */ | ||
$payload = [ | ||
'person' => [ | ||
'name' => 'Gustavo', | ||
'distance' => Exceptions::NO_ELIGIBLE_CARRIERS | ||
], | ||
'product' => [ | ||
'name' => 'Notebook', | ||
'weight' => 1.0 | ||
] | ||
]; | ||
|
||
/** @And that I use this data in the request */ | ||
$request = Request::postFrom(payload: $payload); | ||
|
||
/** @When I process the request with this handler */ | ||
$actual = $this->middleware->process(request: $request, handler: $this->endpoint); | ||
|
||
/** @Then the response should be an error indicating no eligible carriers */ | ||
$expected = ['error' => 'There are no eligible carriers for the shipment.']; | ||
|
||
self::assertEquals(HttpCode::BAD_REQUEST->value, $actual->getStatusCode()); | ||
self::assertEquals(json_encode($expected), $actual->getBody()->__toString()); | ||
} | ||
|
||
public function testExceptionWhenNoCarriersAvailable(): void | ||
{ | ||
/** @Given that I have data to calculate a shipment */ | ||
$payload = [ | ||
'person' => [ | ||
'name' => 'Gustavo', | ||
'distance' => Exceptions::NO_CARRIERS_AVAILABLE | ||
], | ||
'product' => [ | ||
'name' => 'Notebook', | ||
'weight' => 1.0 | ||
] | ||
]; | ||
|
||
/** @And that I use this data in the request */ | ||
$request = Request::postFrom(payload: $payload); | ||
|
||
/** @When I process the request with this handler */ | ||
$actual = $this->middleware->process(request: $request, handler: $this->endpoint); | ||
|
||
/** @Then the response should be an error indicating no carriers available */ | ||
$expected = ['error' => 'No carriers available for shipment.']; | ||
|
||
self::assertEquals(HttpCode::NOT_FOUND->value, $actual->getStatusCode()); | ||
self::assertEquals(json_encode($expected), $actual->getBody()->__toString()); | ||
} | ||
|
||
public function testExceptionWhenUnknownError(): void | ||
{ | ||
/** @Given that I have data to calculate a shipment */ | ||
$payload = [ | ||
'person' => [ | ||
'name' => 'Gustavo', | ||
'distance' => Exceptions::UNKNOWN_ERROR | ||
], | ||
'product' => [ | ||
'name' => 'Notebook', | ||
'weight' => 1.0 | ||
] | ||
]; | ||
|
||
/** @And that I use this data in the request */ | ||
$request = Request::postFrom(payload: $payload); | ||
|
||
/** @When I process the request with this handler */ | ||
$actual = $this->middleware->process(request: $request, handler: $this->endpoint); | ||
|
||
/** @Then the response should be an error indicating error */ | ||
$expected = ['error' => 'Any error.']; | ||
|
||
self::assertEquals(HttpCode::INTERNAL_SERVER_ERROR->value, $actual->getStatusCode()); | ||
self::assertEquals(json_encode($expected), $actual->getBody()->__toString()); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
tests/Unit/Driver/Http/Endpoints/CalculateShipment/CalculateShipmentTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace CheapDelivery\Driver\Http\Endpoints\CalculateShipment; | ||
|
||
use CheapDelivery\Driver\Http\Endpoints\CalculateShipment\Factories\Request; | ||
use CheapDelivery\Driver\Http\Endpoints\CalculateShipment\Mocks\CalculateShipmentHandlerMock; | ||
use PHPUnit\Framework\TestCase; | ||
use TinyBlocks\Http\HttpCode; | ||
|
||
class CalculateShipmentTest extends TestCase | ||
{ | ||
private CalculateShipment $endpoint; | ||
|
||
private CalculateShipmentHandlerMock $useCase; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->useCase = new CalculateShipmentHandlerMock(); | ||
$this->endpoint = new CalculateShipment(useCase: $this->useCase); | ||
} | ||
|
||
public function testCalculateShipment(): void | ||
{ | ||
/** @Given that I have data to calculate a shipment */ | ||
$payload = [ | ||
'person' => [ | ||
'name' => 'Gustavo', | ||
'distance' => 100.0 | ||
], | ||
'product' => [ | ||
'name' => 'Notebook', | ||
'weight' => 1.0 | ||
] | ||
]; | ||
|
||
/** @And that I use this data in the request */ | ||
$request = Request::postFrom(payload: $payload); | ||
|
||
/** @When I execute the request */ | ||
$actual = $this->endpoint->handle(request: $request); | ||
|
||
/** @Then the request should be successful */ | ||
self::assertEquals(HttpCode::NO_CONTENT->value, $actual->getStatusCode()); | ||
|
||
/** @And a command should be registered */ | ||
$person = $this->useCase->lastCommand->person; | ||
$product = $this->useCase->lastCommand->product; | ||
|
||
self::assertEquals($payload['person']['name'], $person->name->value); | ||
self::assertEquals($payload['person']['distance'], $person->distance->value); | ||
self::assertEquals($payload['product']['name'], $product->name->value); | ||
self::assertEquals($payload['product']['weight'], $product->weight->value); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/Unit/Driver/Http/Endpoints/CalculateShipment/Factories/Request.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace CheapDelivery\Driver\Http\Endpoints\CalculateShipment\Factories; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
use Slim\Psr7\Headers; | ||
use Slim\Psr7\Request as SlimRequest; | ||
use Slim\Psr7\Stream; | ||
use Slim\Psr7\Uri; | ||
|
||
final class Request | ||
{ | ||
public static function postFrom(array $payload): ServerRequestInterface | ||
{ | ||
$encode = json_encode($payload); | ||
$stream = fopen('php://memory', 'r+'); | ||
|
||
fwrite($stream, $encode); | ||
rewind($stream); | ||
|
||
$uri = new Uri('https', 'cheap-delivery.localhost'); | ||
$body = new Stream($stream); | ||
$headers = new Headers(['Content-Type' => 'application/json']); | ||
|
||
return new SlimRequest('POST', $uri, $headers, [], [], $body); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/Unit/Driver/Http/Endpoints/CalculateShipment/Mocks/CalculateShipmentHandlerMock.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace CheapDelivery\Driver\Http\Endpoints\CalculateShipment\Mocks; | ||
|
||
use CheapDelivery\Application\Commands\CalculateShipment; | ||
use CheapDelivery\Application\Commands\Command; | ||
use CheapDelivery\Application\Domain\Exceptions\NoCarriersAvailable; | ||
use CheapDelivery\Application\Domain\Exceptions\NoEligibleCarriers; | ||
use CheapDelivery\Application\Ports\Inbound\CommandHandler; | ||
use RuntimeException; | ||
|
||
final class CalculateShipmentHandlerMock implements CommandHandler | ||
{ | ||
public CalculateShipment $lastCommand; | ||
|
||
public function handle(Command|CalculateShipment $command): void | ||
{ | ||
$this->lastCommand = $command; | ||
|
||
match ($command->person->distance->value) { | ||
Exceptions::UNKNOWN_ERROR => throw new RuntimeException('Any error.'), | ||
Exceptions::NO_ELIGIBLE_CARRIERS => throw new NoEligibleCarriers(), | ||
Exceptions::NO_CARRIERS_AVAILABLE => throw new NoCarriersAvailable(), | ||
default => null | ||
}; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/Unit/Driver/Http/Endpoints/CalculateShipment/Mocks/Exceptions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace CheapDelivery\Driver\Http\Endpoints\CalculateShipment\Mocks; | ||
|
||
final class Exceptions | ||
{ | ||
public const UNKNOWN_ERROR = 100000.00; | ||
public const NO_ELIGIBLE_CARRIERS = 200000.00; | ||
public const NO_CARRIERS_AVAILABLE = 300000.00; | ||
} |