Last active
January 29, 2023 23:07
-
-
Save NandoKstroNet/b589fb9b9259d47a328236931f4e45b8 to your computer and use it in GitHub Desktop.
Caso de Uso: Conversão de Livro para PDF - Curso Laravel Arquitetura Limpa 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 MiniLeanpub\Application\UseCases\Book\ConvertBookToPDF\DTO; | |
use MiniLeanpub\Application\UseCases\Shared\InteractorDTO; | |
class ConvertBookToPDFInputDTO extends InteractorDTO | |
{ | |
public function __construct(public string $bookCode) | |
{ | |
} | |
} |
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 MiniLeanpub\Application\UseCases\Book\ConvertBookToPDF\DTO; | |
use MiniLeanpub\Application\UseCases\Shared\InteractorDTO; | |
class ConvertBookToPDFOutputDTO extends InteractorDTO | |
{ | |
public function __construct(public string $bookCode) | |
{ | |
} | |
} |
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 MiniLeanpub\Application\UseCases\Book\ConvertBookToPDF; | |
use MiniLeanpub\Application\UseCases\Book\ConvertBookToPDF\DTO\ConvertBookToPDFInputDTO; | |
use MiniLeanpub\Application\UseCases\Book\ConvertBookToPDF\DTO\ConvertBookToPDFOutputDTO; | |
use MiniLeanpub\Domain\Book\Queue\Book\QueueInterface; | |
use MiniLeanpub\Domain\Book\Repository\Book\BookRepositoryInterface; | |
class ConvertBookToPDFUseCase | |
{ | |
public function __construct( | |
private ConvertBookToPDFInputDTO $input, | |
private BookRepositoryInterface $repository, | |
private QueueInterface $queue | |
) | |
{ | |
} | |
public function handle(): ConvertBookToPDFOutputDTO | |
{ | |
$book = $this->repository->find($this->input->getData()['bookCode']); | |
$this->queue->sendToQueue($book->book_code); | |
return new ConvertBookToPDFOutputDTO($book->book_code); | |
} | |
} |
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\MiniLeanpub\Unit\Application\UseCases\Book\ConvertBookToPDF; | |
use App\Models\Book; | |
use MiniLeanpub\Application\UseCases\Book\ConvertBookToPDF\ConvertBookToPDFUseCase; | |
use MiniLeanpub\Application\UseCases\Book\ConvertBookToPDF\DTO\ConvertBookToPDFInputDTO; | |
use MiniLeanpub\Application\UseCases\Book\ConvertBookToPDF\DTO\ConvertBookToPDFOutputDTO; | |
use MiniLeanpub\Infrastructure\Queue\BookConverterQueueSender; | |
use MiniLeanpub\Infrastructure\Repository\BookEloquentRepository; | |
use PHPUnit\Framework\TestCase; | |
class ConvertBookToPDFUseCaseTest extends TestCase | |
{ | |
public function testShouldSendABookToConvertToPDFViaUseCase() | |
{ | |
$input = new ConvertBookToPDFInputDTO('72479eaa-62a8-4ddb-8d6e-4c35c6c7f700'); | |
$repository = $this->getRepositoryMock(); | |
$queueSender = $this->getQueueSenderMock(); | |
$useCase = new ConvertBookToPDFUseCase($input, $repository, $queueSender); | |
$result = $useCase->handle(); | |
$this->assertInstanceOf(ConvertBookToPDFOutputDTO::class, $result); | |
} | |
private function getRepositoryMock() | |
{ | |
$stubModel = $this->createMock(Book::class); | |
$return = new \stdClass(); | |
$return->id = 1; | |
$return->book_code = '72479eaa-62a8-4ddb-8d6e-4c35c6c7f700'; | |
$return->title = 'My awesome book title!'; | |
$return->description = 'Book Description'; | |
$return->price = 25.9; | |
$return->book_path = 'book_path'; | |
$mock = $this->getMockBuilder(BookEloquentRepository::class) | |
->onlyMethods(['find']) | |
->setConstructorArgs([$stubModel]) | |
->getMock(); | |
$mock->expects($this->once()) | |
->method('find') | |
->with('72479eaa-62a8-4ddb-8d6e-4c35c6c7f700') | |
->willReturn($return); | |
return $mock; | |
} | |
private function getQueueSenderMock() | |
{ | |
$mock = $this->getMockBuilder(BookConverterQueueSender::class) | |
->onlyMethods(['sendToQueue']) | |
->getMock(); | |
$mock->expects($this->once()) | |
->method('sendToQueue') | |
->willReturn(true); | |
return $mock; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment