-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
car update controller & laravel test case back to normal
- Loading branch information
Showing
13 changed files
with
193 additions
and
41 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
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
58 changes: 58 additions & 0 deletions
58
src/Quicktrack/Car/Infrastructure/Controllers/Api/CarPutController.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,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 | ||
); | ||
} | ||
} |
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
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,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()}" | ||
] | ||
]); | ||
} | ||
} |
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,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}" | ||
] | ||
]); | ||
} | ||
} |
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
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
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