Last active
March 14, 2023 19:19
-
-
Save NandoKstroNet/fa1934c7e36c674ce29d93e06bd9328e to your computer and use it in GitHub Desktop.
Testando Endpoints do Livro baseado nos Use Cases do nosso projeto de Arquitetura Limpa com Laravel em https://codeexperts.com.br
This file contains hidden or 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
<?php | |
namespace App\Http\Controllers\API; | |
use App\Http\Controllers\Controller; | |
use App\Models\Book; | |
use Illuminate\Http\Request; | |
use MiniLeanpub\Application\UseCases\Book\CreateBook\CreateBookUseCase; | |
use MiniLeanpub\Application\UseCases\Book\CreateBook\DTO\BookCreateInputDTO; | |
use MiniLeanpub\Infrastructure\Repository\Book\BookEloquentRepository; | |
use Illuminate\Support\Str; | |
use MiniLeanpub\Application\UseCases\Book\ConvertBookToPDF\ConvertBookToPDFUseCase; | |
use MiniLeanpub\Application\UseCases\Book\ConvertBookToPDF\DTO\ConvertBookToPDFInputDTO; | |
use MiniLeanpub\Infrastructure\Queue\Book\BookConverterQueueSender; | |
class BookApiController extends Controller | |
{ | |
public function store(Request $request) | |
{ | |
$bookCode = '188e831e-0335-4bc9-9deb-c4d6dba6b258'; | |
$input = new BookCreateInputDTO( | |
$bookCode, | |
$request->title, | |
$request->description, | |
$request->price, | |
storage_path('app/books/' . $bookCode . '/chapters'), | |
'text/markdown' | |
); | |
$repository = new BookEloquentRepository(new Book()); | |
$useCase = new CreateBookUseCase($input, $repository); | |
$result = ($useCase->handle())->getData(); | |
return response()->json([ | |
'data' => [ | |
'message' => "Book {$result['title']} has been created successfully" | |
] | |
]); | |
} | |
public function convertBook($bookCode) | |
{ | |
$repository = new BookEloquentRepository(new Book()); | |
$input = new ConvertBookToPDFInputDTO($bookCode); | |
$queueSender = new BookConverterQueueSender($bookCode); | |
$useCase = new ConvertBookToPDFUseCase($input, $repository, $queueSender); | |
$result = $useCase->handle(); | |
return response()->json([ | |
'data' => [ | |
'message' => "Book has been sended to conversion successfully" | |
] | |
]); | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace Tests\Feature; | |
use App\Models\Book; | |
use Illuminate\Foundation\Testing\RefreshDatabase; | |
use Illuminate\Foundation\Testing\WithFaker; | |
use MiniLeanpub\Infrastructure\Repository\Book\BookEloquentRepository; | |
use Tests\TestCase; | |
class BookApiControllerTest extends TestCase | |
{ | |
use RefreshDatabase; | |
/** | |
* A basic feature test example. | |
* | |
* @return void | |
*/ | |
public function testPostToCreateANewBookViaApiEndpoint() | |
{ | |
$data = [ | |
'title' => 'Meu Livro de PHP', | |
'description' => 'Descrição de Meu Livro de PHP', | |
'price' => 29.9 | |
]; | |
$response = $this->postJson('/api/books', $data); | |
$response->assertStatus(200); | |
$this->assertEquals('Book Meu Livro de PHP has been created successfully', $response->json()['data']['message']); | |
} | |
public function testPostToMadeABookPDFViaApiEndpoint() | |
{ | |
$repository = new BookEloquentRepository(new Book()); | |
$bookCode = '188e831e-0335-4bc9-9deb-c4d6dba6b258'; | |
$repository->create([ | |
'bookCode' => $bookCode, | |
'title' => 'Book Test', | |
'description' => 'Book Description', | |
'price' => 1.99, | |
'bookPath' => storage_path('app/books/' . $bookCode . '/chapters') | |
]); | |
$response = $this->postJson('/api/books/' . $bookCode); | |
$response->assertStatus(200); | |
$this->assertEquals('Book has been sended to conversion successfully', $response->json()['data']['message']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment