Last active
January 24, 2023 21:11
-
-
Save NandoKstroNet/b647cc96566433b5aa212a1231eb12d1 to your computer and use it in GitHub Desktop.
Entidade Book - 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\Domain\Book\Entity; | |
class Book | |
{ | |
public function __construct( | |
private ?string $id, | |
private ?string $title, | |
private ?string $description, | |
private ?float $price, | |
private ?string $bookPath, | |
private ?string $mimeType | |
) | |
{ | |
} | |
public function validate() | |
{ | |
if(!$this->id) throw new \Exception("Invalid Entity: ID"); | |
if(!$this->title || !$this->description) throw new \Exception("Invalid Entity: Title or Description"); | |
if($this->price < 0) throw new \Exception("Invalid Entity: Price"); | |
if(!$this->bookPath) throw new \Exception("Invalid Entity: Path Book"); | |
if($this->mimeType != 'text/markdown') throw new \Exception("Invalid Entity: MimeType"); | |
} | |
} |
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\Domain\Book\Entity; | |
use MiniLeanpub\Domain\Book\Entity\Book; | |
use PHPUnit\Framework\TestCase; | |
class BookTest extends TestCase | |
{ | |
public function testIfBookValidationThrowsExceptionToAnInvalidId() | |
{ | |
$this->expectException(\Exception::class); | |
$this->expectExceptionMessage('Invalid Entity: ID'); | |
$book = new Book(null, 'Titulo Livro', 'Descrição Livro', 25.9, 'path_book', 'mime_type'); | |
$book->validate(); | |
} | |
public function testIfBookValidationThrowsExceptionToAnInvalidTitleOrDescription() | |
{ | |
$this->expectException(\Exception::class); | |
$this->expectExceptionMessage('Invalid Entity: Title or Description'); | |
$book = new Book('UUID', null, 'Descrição Livro', 25.9, 'path_book', 'text/markdown'); | |
$book->validate(); | |
$this->expectException(\Exception::class); | |
$this->expectExceptionMessage('Invalid Entity: Title or Description'); | |
$book = new Book('UUID', 'Titulo Livro', null, 25.9, 'path_book', 'text/markdown'); | |
$book->validate(); | |
} | |
public function testIfBookValidationThrowsExceptionToAnInvalidPrice() | |
{ | |
$this->expectException(\Exception::class); | |
$this->expectExceptionMessage('Invalid Entity: Price'); | |
$book = new Book('UUID', 'Titulo Livro', 'Descrição Livro', -10, 'path_book', 'text/markdown'); | |
$book->validate(); | |
} | |
public function testIfBookValidationThrowsExceptionToAnInvalidBookPath() | |
{ | |
$this->expectException(\Exception::class); | |
$this->expectExceptionMessage('Invalid Entity: Path Book'); | |
$book = new Book('UUID', 'Titulo Livro', 'Descrição Livro', 25.9, null, 'text/markdown'); | |
$book->validate(); | |
} | |
public function testIfBookValidationThrowsExceptionToAValidBookMimeType() | |
{ | |
$this->expectException(\Exception::class); | |
$this->expectExceptionMessage('Invalid Entity: MimeType'); | |
$book = new Book('UUID', 'Titulo Livro', 'Descrição Livro', 25.9, 'book_path', 'application/json'); | |
$book->validate(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment