Skip to content

Commit

Permalink
car update controller & laravel test case back to normal
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarcanor committed Oct 15, 2022
1 parent 1dcc365 commit f970e13
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 41 deletions.
1 change: 1 addition & 0 deletions routes/api/car/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

Route::post('/car', Quicktrack\Car\Infrastructure\Controllers\Api\CarPostController::class);
Route::get('/car/{id}', Quicktrack\Car\Infrastructure\Controllers\Api\CarGetController::class);
Route::put('/car/{id}', Quicktrack\Car\Infrastructure\Controllers\Api\CarPutController::class);

/* Route::middleware('auth:api')
->group(function () {
Expand Down
27 changes: 15 additions & 12 deletions src/Quicktrack/Car/Application/Update/CarUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Quicktrack\Car\Domain\ValueObjects\CarStatus;
use Quicktrack\Car\Domain\ValueObjects\CarType;
use Quicktrack\Car\Domain\ValueObjects\CarYear;
use Shared\Domain\Errors;

final class CarUpdater
{
Expand All @@ -35,17 +36,19 @@ public function __invoke(
{
$car = ($this->finder)(new CarId($request->id()));

$car->changeBrand(new CarBrand($request->brand()));
$car->changeModel(new CarModel($request->model()));
$car->changeColor(new CarColor($request->color()));
$car->changeFuel(new CarFuel($request->fuel()));
$car->changeGearbox(new CarGearbox($request->gearbox()));
$car->changeKilometer(new CarKilometer($request->kilometer()));
$car->changePrice(new CarPrice($request->price()));
$car->changeType(new CarType($request->type()));
$car->changeYear(new CarYear($request->year()));
$car->changeStatus(new CarStatus($request->status()));

$this->repository->update($car);
if (! Errors::getInstance()->hasErrors()) {
$car->changeBrand(new CarBrand($request->brand()));
$car->changeModel(new CarModel($request->model()));
$car->changeColor(new CarColor($request->color()));
$car->changeFuel(new CarFuel($request->fuel()));
$car->changeGearbox(new CarGearbox($request->gearbox()));
$car->changeKilometer(new CarKilometer($request->kilometer()));
$car->changePrice(new CarPrice($request->price()));
$car->changeType(new CarType($request->type()));
$car->changeYear(new CarYear($request->year()));
$car->changeStatus(new CarStatus($request->status()));

$this->repository->update($car);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Quicktrack\Car\Infrastructure\Controllers\Api;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Quicktrack\Car\Application\Update\CarUpdater;
use Quicktrack\Car\Application\Update\CarUpdaterRequest;
use Shared\Domain\Errors;

final class CarPutController extends Controller
{
public function __construct(
private CarUpdater $updater
)
{
}

public function __invoke(string $id, Request $request): JsonResponse
{
($this->updater)(new CarUpdaterRequest(
$id,
$request->input('brand'),
$request->input('model'),
$request->input('color'),
$request->input('fuel'),
$request->input('gearbox'),
$request->input('kilometer'),
$request->input('price'),
$request->input('type'),
$request->input('year'),
$request->input('status'),
));

if (Errors::getInstance()->hasErrors()) {
return new JsonResponse(
[
'ok' => false,
'content' => [],
'errors' => Errors::getInstance()->errorsMessage()
],
Errors::getInstance()->errorsCode()
);
}

return new JsonResponse(
[
'ok' => true,
'content' => [],
'errors' => []
],
JsonResponse::HTTP_OK
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ public function create(Car $car): void

public function update(Car $car): void
{
ModelsCar::update($car->toArray());
$car = ModelsCar::find($car->id()->value());

if ($car) {
$car->update($car->toArray());
}
}

public function find(CarId $carId): ?Car
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Quicktrack/Car/CarGetControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Tests\Shared\Infrastructure\Laravel\TestCase;
use Tests\Unit\Quicktrack\Car\Domain\CarMother;
use Tests\Unit\Shared\Domain\UuidMother;

class CarGetControllerTest extends TestCase
{
Expand Down Expand Up @@ -32,15 +33,14 @@ public function itShouldFindAnExistingCar()
*/
public function errorsArrayShouldHaveDomainNotFoundException()
{
$id = 'wrong-id';
$id = UuidMother::random();
$response = $this->getJson("/api/car/{$id}");

$response->assertStatus(400);
$response->assertJson([
'ok' => false,
'content' => [],
'errors' => [
"Invalid uuid",
"There's not any car with ID {$id}"
]
]);
Expand Down
48 changes: 48 additions & 0 deletions tests/Feature/Quicktrack/Car/CarPutControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Tests\Feature\Quicktrack\Car;

use Tests\Shared\Infrastructure\Laravel\TestCase;
use Tests\Unit\Quicktrack\Car\Domain\CarBrandMother;
use Tests\Unit\Quicktrack\Car\Domain\CarMother;

class CarPutControllerTest extends TestCase
{
/**
* @test
*/
public function itShouldFindAnUpdateAnExistingCar()
{
$car = CarMother::random();
$this->postJson('/api/car', $car->toArray());

$car->changeBrand(CarBrandMother::random());

$response = $this->putJson("/api/car/{$car->id()->value()}", $car->toArray());

$response->assertStatus(200);
$response->assertJson([
'ok' => true,
'content' => [],
'errors' => []
]);
}

/**
* @test
*/
public function errorsArrayShouldHaveDomainNotFoundException()
{
$car = CarMother::random();
$response = $this->putJson("/api/car/{$car->id()->value()}", $car->toArray());

$response->assertStatus(400);
$response->assertJson([
'ok' => false,
'content' => [],
'errors' => [
"There's not any car with ID {$car->id()->value()}"
]
]);
}
}
48 changes: 48 additions & 0 deletions tests/Feature/Quicktrack/Car/CarsGetController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Tests\Feature\Quicktrack\Car;

use Tests\Shared\Infrastructure\Laravel\TestCase;
use Tests\Unit\Quicktrack\Car\Domain\CarMother;
use Tests\Unit\Shared\Domain\UuidMother;

class CarsGetControllerTest extends TestCase
{
/**
* @test
*/
public function itShouldSearchAndFindAnExistingCar()
{
$car = CarMother::random();
$this->postJson('/api/car', $car->toArray());

$response = $this->getJson("/api/car/{$car->id()->value()}");

$response->assertStatus(200);
$response->assertJson([
'ok' => true,
'content' => [
'car' => $car->toArray()
],
'errors' => []
]);
}

/**
* @test
*/
public function errorsArrayShouldHaveDomainNotFoundException()
{
$id = UuidMother::random();
$response = $this->getJson("/api/car/{$id}");

$response->assertStatus(400);
$response->assertJson([
'ok' => false,
'content' => [],
'errors' => [
"There's not any car with ID {$id}"
]
]);
}
}
2 changes: 0 additions & 2 deletions tests/Shared/Infrastructure/Laravel/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Shared\Domain\Errors;
use Tests\CreatesApplication;
use Tests\TestCase as LaravelTestCase;

abstract class TestCase extends LaravelTestCase
{
use CreatesApplication;
use DatabaseTransactions;

protected function tearDown(): void
Expand Down
9 changes: 0 additions & 9 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,10 @@

namespace Tests;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Shared\Domain\Errors;

abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use DatabaseTransactions;

protected function tearDown(): void
{
if (Errors::getInstance()->hasErrors()) {
Errors::getInstance()->clear();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
use Quicktrack\Car\Domain\Contract\CarRepository;
use Quicktrack\Car\Domain\Entity\Car;
use Shared\Domain\Errors;
use Tests\Shared\Infrastructure\Laravel\TestCase;
use Tests\Unit\Quicktrack\Car\Domain\CarMother;
use Tests\TestCase;

final class CarCreatorTest extends TestCase {
final class CarCreatorTest extends TestCase
{

/**
* @test
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/Quicktrack/Car/Application/Find/CarFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
use Quicktrack\Car\Domain\Contract\CarRepository;
use Quicktrack\Car\Domain\Entity\Car;
use Shared\Domain\Errors;
use Tests\Shared\Infrastructure\Laravel\TestCase;
use Tests\Unit\Quicktrack\Car\Domain\CarMother;
use Tests\TestCase;
use Tests\Unit\Quicktrack\Car\Domain\CarIdMother;
use Tests\Unit\Shared\Domain\UuidMother;

final class CarFinderTest extends TestCase {
final class CarFinderTest extends TestCase
{

/**
* @test
Expand Down
12 changes: 7 additions & 5 deletions tests/Unit/Quicktrack/Car/Application/Search/CarSearcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Quicktrack\Car\Application\Search\CarSearcher;
use Quicktrack\Car\Domain\Contract\CarRepository;
use Quicktrack\Car\Domain\Entity\Car;
use Tests\TestCase;
use Tests\Shared\Infrastructure\Laravel\TestCase;
use Tests\Unit\Quicktrack\Car\Domain\CarMother;
use Tests\Unit\Quicktrack\Car\Domain\CarsMother;

Expand All @@ -22,15 +22,16 @@ public function itShouldFindOneActiveCar()
$car2 = CarMother::withStatus('sold');
$car3 = CarMother::withStatus('available');
$response = CarsMother::create($car3);
$propValue = 'available';

$repository = $this->createMock(CarRepository::class);
$searcher = new CarSearcher($repository);

$this->shouldSearchByPropEqualTo($repository, 'status', 'available', $car1, $car2, $car3);
$this->shouldSearchByPropEqualTo($repository, 'status', $propValue, $car1, $car2, $car3);

$this->assertEquals(
$response,
($searcher)(CarSearcherRequestMother::byStatus('available'))
($searcher)(CarSearcherRequestMother::byStatus($propValue))
);
}

Expand All @@ -43,15 +44,16 @@ public function itShouldNotFindAnyActiveCar()
$car2 = CarMother::withStatus('sold');
$car3 = CarMother::withStatus('sold');
$response = CarsMother::create();
$propValue = 'available';

$repository = $this->createMock(CarRepository::class);
$searcher = new CarSearcher($repository);

$this->shouldSearchByPropEqualTo($repository, 'status', 'available', $car1, $car2, $car3);
$this->shouldSearchByPropEqualTo($repository, 'status', $propValue, $car1, $car2, $car3);

$this->assertEquals(
$response,
($searcher)(CarSearcherRequestMother::byStatus('available'))
($searcher)(CarSearcherRequestMother::byStatus($propValue))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@

use PHPUnit\Framework\MockObject\MockObject;
use Quicktrack\Car\Application\Update\CarUpdater;
use Quicktrack\Car\Application\Update\CarUpdaterRequest;
use Quicktrack\Car\Domain\Contract\CarRepository;
use Quicktrack\Car\Domain\Entity\Car;
use Shared\Domain\Exceptions\DomainNotExistsException;
use Tests\Shared\Infrastructure\Laravel\TestCase;
use Tests\Unit\Quicktrack\Car\Domain\CarMother;
use Tests\TestCase;
use Tests\Unit\Quicktrack\Car\Domain\CarIdMother;
use Tests\Unit\Shared\Domain\UuidMother;

final class CarUpdaterTest extends TestCase {
final class CarUpdaterTest extends TestCase
{
/**
* @test
*/
Expand Down

0 comments on commit f970e13

Please sign in to comment.