From 7a12611ec0af4c37737d47bc2b08c4d988aa3541 Mon Sep 17 00:00:00 2001 From: Zazimou Date: Wed, 3 Jan 2024 09:24:03 +0100 Subject: [PATCH] Reworked tests for BackedEnums --- .../Collection/collection.enumOnEntities.phpt | 18 ++-- .../integration/Entity/entity.enumProps.phpt | 96 +++++++++++++------ tests/db/array-data.php | 20 +--- tests/db/mysql-data.sql | 21 ++-- tests/db/mysql-init.sql | 7 +- tests/inc/model/Model.php | 1 - tests/inc/model/book/Book.php | 2 + tests/inc/model/book/GenreEnum.php | 13 +++ tests/inc/model/car/Car.php | 16 ---- tests/inc/model/car/CarsMapper.php | 14 --- tests/inc/model/car/CarsRepository.php | 24 ----- tests/inc/model/car/FuelType.php | 12 --- 12 files changed, 109 insertions(+), 135 deletions(-) create mode 100644 tests/inc/model/book/GenreEnum.php delete mode 100644 tests/inc/model/car/Car.php delete mode 100644 tests/inc/model/car/CarsMapper.php delete mode 100644 tests/inc/model/car/CarsRepository.php delete mode 100644 tests/inc/model/car/FuelType.php diff --git a/tests/cases/integration/Collection/collection.enumOnEntities.phpt b/tests/cases/integration/Collection/collection.enumOnEntities.phpt index 2507649f..a5d3f6c1 100644 --- a/tests/cases/integration/Collection/collection.enumOnEntities.phpt +++ b/tests/cases/integration/Collection/collection.enumOnEntities.phpt @@ -8,8 +8,8 @@ namespace NextrasTests\Orm\Integration\Collection; +use inc\model\book\GenreEnum; use NextrasTests\Orm\DataTestCase; -use NextrasTests\Orm\FuelType; use Tester\Assert; @@ -20,19 +20,19 @@ class CollectionEnumOnEntitiesTest extends DataTestCase { public function testEntityEnumType(): void { - $collection = $this->orm->cars->findBy([ - 'fuelType' => [ - FuelType::DIESEL, - FuelType::ELECTRIC, - FuelType::PETROL, - FuelType::HYBRID, + $collection = $this->orm->books->findBy([ + 'genre' => [ + GenreEnum::HORROR, + GenreEnum::THRILLER, + GenreEnum::SCIFI, + GenreEnum::FANTASY, ], ]); $collection = $collection->orderBy('id'); Assert::same(3, $collection->countStored()); - foreach ($collection as $car) { - Assert::type(FuelType::class, $car->fuelType); + foreach ($collection as $book) { + Assert::type(GenreEnum::class, $book->genre); } } diff --git a/tests/cases/integration/Entity/entity.enumProps.phpt b/tests/cases/integration/Entity/entity.enumProps.phpt index 57188fe6..4c93d0f1 100644 --- a/tests/cases/integration/Entity/entity.enumProps.phpt +++ b/tests/cases/integration/Entity/entity.enumProps.phpt @@ -8,10 +8,15 @@ namespace NextrasTests\Orm\Integration\Entity; +use DateTimeImmutable; +use inc\model\book\GenreEnum; use Nextras\Orm\Exception\InvalidArgumentException; -use NextrasTests\Orm\Car; +use NextrasTests\Orm\Author; +use NextrasTests\Orm\Book; +use NextrasTests\Orm\Currency; use NextrasTests\Orm\DataTestCase; -use NextrasTests\Orm\FuelType; +use NextrasTests\Orm\Money; +use NextrasTests\Orm\Publisher; use Tester\Assert; @@ -23,64 +28,97 @@ class EntityEnumPropTest extends DataTestCase public function testEnumOnEntity(): void { - /** @var Car $car */ - $car = $this->orm->cars->findAll()->fetch(); + /** @var Book $book */ + $book = $this->orm->books->findAll()->fetch(); - Assert::notNull($car); + Assert::notNull($book); - Assert::same(FuelType::HYBRID, $car->fuelType); + Assert::same(GenreEnum::SCIFI, $book->genre); } public function testAddEntityWithEnum(): void { - $carName = 'Tesla Model S'; + $bookName = 'Book 5'; - $car = new Car(); - $car->fuelType = FuelType::ELECTRIC; - $car->name = $carName; - $this->orm->cars->persistAndFlush($car); + $book = $this->createBookEntity($bookName); + $book->genre = GenreEnum::ROMANCE; + $this->orm->books->persistAndFlush($book); - Assert::same(FuelType::ELECTRIC, $car->fuelType); + Assert::same(GenreEnum::ROMANCE, $book->genre); - $entity = $this->orm->cars->getBy(['name' => $carName]); + $entity = $this->orm->books->getBy(['title' => $bookName]); Assert::notNull($entity); - Assert::type(FuelType::class, $entity->fuelType); + Assert::type(GenreEnum::class, $entity->genre); - Assert::same(FuelType::ELECTRIC, $entity->fuelType); + Assert::same(GenreEnum::ROMANCE, $entity->genre); + } + + + private function createBookEntity(string $title): Book + { + $book = new Book(); + $book->title = $title; + $book->publishedAt = new DateTimeImmutable('2021-12-14 21:10:02'); + $book->price = new Money(150, Currency::CZK); + $this->createAuthorToBook($book); + $this->createPublisherToBook($book); + $this->orm->books->persist($book); + + return $book; + } + + + private function createAuthorToBook(Book $book): void + { + $author1 = new Author(); + $author1->name = 'Writer 1'; + $author1->web = 'http://example.com/1'; + $this->orm->authors->persist($author1); + + $book->author = $author1; + } + + + private function createPublisherToBook(Book $book): void + { + $publisher1 = new Publisher(); + $publisher1->name = 'Nextras publisher A'; + $this->orm->publishers->persist($publisher1); + + $book->publisher = $publisher1; } public function testAddEntityWithDefaultEnum(): void { - $carName = 'Volkswagen Golf'; + $bookName = 'Book 6'; - $car = new Car(); - $car->name = $carName; - $this->orm->cars->persistAndFlush($car); + $book = $this->createBookEntity($bookName); + $this->orm->books->persistAndFlush($book); - Assert::same(FuelType::DIESEL, $car->fuelType); + Assert::same(GenreEnum::FANTASY, $book->genre); - $entity = $this->orm->cars->getBy(['name' => $carName]); + $entity = $this->orm->books->getBy(['title' => $bookName]); Assert::notNull($entity); - Assert::type(FuelType::class, $entity->fuelType); + Assert::type(GenreEnum::class, $entity->genre); - Assert::same(FuelType::DIESEL, $entity->fuelType); + Assert::same(GenreEnum::FANTASY, $entity->genre); } public function testAddEntityWithUnknownEnum(): void { - $carName = 'Toyota Mirai'; + $bookName = 'Book 7'; - $car = new Car(); - $car->name = $carName; - $car->fuelType = 'hydrogen'; + $book = $this->createBookEntity($bookName); + // @phpstan-ignore-next-line + $book->genre = 'documentary'; - Assert::exception(function () use ($car) { - $this->orm->cars->persistAndFlush($car); + Assert::exception(function () use ($book) { + $this->orm->books->persistAndFlush($book); }, InvalidArgumentException::class); } diff --git a/tests/db/array-data.php b/tests/db/array-data.php index 7ab56c48..06ebd913 100644 --- a/tests/db/array-data.php +++ b/tests/db/array-data.php @@ -3,6 +3,7 @@ namespace NextrasTests\Orm; +use inc\model\book\GenreEnum; use Nextras\Dbal\Utils\DateTimeImmutable; @@ -43,6 +44,7 @@ $book1->author = $author1; $book1->translator = $author1; $book1->publisher = $publisher1; +$book1->genre = GenreEnum::SCIFI; $book1->publishedAt = new \DateTimeImmutable('2021-12-14 21:10:04'); $book1->price = new Money(50, Currency::CZK); $book1->tags->set([$tag1, $tag2]); @@ -52,6 +54,7 @@ $book2->title = 'Book 2'; $book2->author = $author1; $book2->publisher = $publisher2; +$book2->genre = GenreEnum::HORROR; $book2->publishedAt = new \DateTimeImmutable('2021-12-14 21:10:02'); $book2->price = new Money(150, Currency::CZK); $book2->tags->set([$tag2, $tag3]); @@ -62,6 +65,7 @@ $book3->author = $author2; $book3->translator = $author2; $book3->publisher = $publisher3; +$book3->genre = GenreEnum::THRILLER; $book3->publishedAt = new \DateTimeImmutable('2021-12-14 21:10:03'); $book3->price = new Money(20, Currency::CZK); $book3->tags->set([$tag3]); @@ -72,6 +76,7 @@ $book4->author = $author2; $book4->translator = $author2; $book4->publisher = $publisher1; +$book4->genre = GenreEnum::ROMANCE; $book4->nextPart = $book3; $book4->publishedAt = new \DateTimeImmutable('2021-12-14 21:10:01'); $book4->price = new Money(220, Currency::CZK); @@ -108,20 +113,5 @@ $comment2->repliedAt = new DateTimeImmutable('2020-01-02 12:00:00'); $orm->contents->persist($comment2); -$car1 = new Car(); -$car1->name = 'Skoda Octavia'; -$car1->fuelType = FuelType::HYBRID; -$orm->cars->persist($car1); - -$car2 = new Car(); -$car2->name = 'BMW X5'; -$car2->fuelType = FuelType::DIESEL; -$orm->cars->persist($car2); - -$car3 = new Car(); -$car3->name = 'Bugatti Chiron'; -$car3->fuelType = FuelType::PETROL; -$orm->cars->persist($car3); - $orm->flush(); $orm->clear(); diff --git a/tests/db/mysql-data.sql b/tests/db/mysql-data.sql index ea4fcd8f..1f90bc89 100644 --- a/tests/db/mysql-data.sql +++ b/tests/db/mysql-data.sql @@ -12,7 +12,6 @@ TRUNCATE users_x_users; TRUNCATE user_stats; TRUNCATE users; TRUNCATE logs; -TRUNCATE cars; SET FOREIGN_KEY_CHECKS = 1; INSERT INTO authors (id, name, web, born) VALUES (1, 'Writer 1', 'http://example.com/1', NULL); @@ -26,10 +25,18 @@ INSERT INTO tags (id, name, is_global) VALUES (1, 'Tag 1', 'y'); INSERT INTO tags (id, name, is_global) VALUES (2, 'Tag 2', 'y'); INSERT INTO tags (id, name, is_global) VALUES (3, 'Tag 3', 'n'); -INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (1, 1, 1, 'Book 1', NULL, 1, '2021-12-14 21:10:04', 50, 'CZK'); -INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (2, 1, NULL, 'Book 2', NULL, 2, '2021-12-14 21:10:02', 150, 'CZK'); -INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (3, 2, 2, 'Book 3', NULL, 3, '2021-12-14 21:10:03', 20, 'CZK'); -INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (4, 2, 2, 'Book 4', 3, 1, '2021-12-14 21:10:01', 220, 'CZK'); +INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, genre, published_at, price, + price_currency) +VALUES (1, 1, 1, 'Book 1', NULL, 1, 'sciFi', '2021-12-14 21:10:04', 50, 'CZK'); +INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, genre, published_at, price, + price_currency) +VALUES (2, 1, NULL, 'Book 2', NULL, 2, 'horror', '2021-12-14 21:10:02', 150, 'CZK'); +INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, genre, published_at, price, + price_currency) +VALUES (3, 2, 2, 'Book 3', NULL, 3, 'thriller', '2021-12-14 21:10:03', 20, 'CZK'); +INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, genre, published_at, price, + price_currency) +VALUES (4, 2, 2, 'Book 4', 3, 1, 'romance', '2021-12-14 21:10:01', 220, 'CZK'); INSERT INTO books_x_tags (book_id, tag_id) VALUES (1, 1); INSERT INTO books_x_tags (book_id, tag_id) VALUES (1, 2); @@ -44,7 +51,3 @@ INSERT INTO tag_followers (tag_id, author_id, created_at) VALUES (2, 2, '2014-01 INSERT INTO contents (id, type, thread_id, replied_at) VALUES (1, 'thread', NULL, NULL); INSERT INTO contents (id, type, thread_id, replied_at) VALUES (2, 'comment', 1, '2020-01-01 12:00:00'); INSERT INTO contents (id, type, thread_id, replied_at) VALUES (3, 'comment', 1, '2020-01-02 12:00:00'); - -INSERT INTO cars (name, fuel_type) VALUES ('Skoda Octavia', 'hybrid'); -INSERT INTO cars (name, fuel_type) VALUES ('BMW X5', 'diesel'); -INSERT INTO cars (name, fuel_type) VALUES ('Bugatti Chiron', 'petrol'); diff --git a/tests/db/mysql-init.sql b/tests/db/mysql-init.sql index 768cd0fe..5fff6d18 100644 --- a/tests/db/mysql-init.sql +++ b/tests/db/mysql-init.sql @@ -42,6 +42,7 @@ CREATE TABLE books title varchar(50) NOT NULL, next_part int, publisher_id int NOT NULL, + genre varchar(20) NOT NULL, published_at DATETIME NOT NULL, printed_at DATETIME, ean_id int, @@ -178,9 +179,3 @@ CREATE TABLE publishers_x_tags CONSTRAINT publishers_x_tags_tag FOREIGN KEY (tag_id) REFERENCES tags (id), CONSTRAINT publishers_x_tags_publisher FOREIGN KEY (publisher_id) REFERENCES publishers (publisher_id) ON DELETE CASCADE ); - -CREATE TABLE `cars` ( - `id` int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, - `name` varchar(64) NOT NULL, - `fuel_type` varchar(20) NOT NULL -); diff --git a/tests/inc/model/Model.php b/tests/inc/model/Model.php index 560bf1e4..cfe2f209 100644 --- a/tests/inc/model/Model.php +++ b/tests/inc/model/Model.php @@ -10,7 +10,6 @@ * Testing model * @property-read AuthorsRepository $authors * @property-read BooksRepository $books - * @property-read CarsRepository $cars * @property-read BookCollectionsRepository $bookCollections * @property-read ContentsRepository $contents * @property-read EansRepository $eans diff --git a/tests/inc/model/book/Book.php b/tests/inc/model/book/Book.php index e078c74d..e7c64623 100644 --- a/tests/inc/model/book/Book.php +++ b/tests/inc/model/book/Book.php @@ -4,6 +4,7 @@ use DateTimeImmutable; +use inc\model\book\GenreEnum; use Nextras\Orm\Entity\Entity; use Nextras\Orm\Relationships\ManyHasMany; @@ -18,6 +19,7 @@ * @property Book|null $previousPart {1:1 Book::$nextPart} * @property Ean|null $ean {1:1 Ean::$book, isMain=true, cascade=[persist, remove]} * @property Publisher $publisher {m:1 Publisher::$books} + * @property GenreEnum $genre {default GenreEnum::FANTASY} * @property DateTimeImmutable $publishedAt {default "2021-12-31 23:59:59"} * @property DateTimeImmutable|null $printedAt * @property Money|null $price {embeddable} diff --git a/tests/inc/model/book/GenreEnum.php b/tests/inc/model/book/GenreEnum.php new file mode 100644 index 00000000..2d254d2b --- /dev/null +++ b/tests/inc/model/book/GenreEnum.php @@ -0,0 +1,13 @@ + - */ -final class CarsMapper extends DbalMapper -{ -} diff --git a/tests/inc/model/car/CarsRepository.php b/tests/inc/model/car/CarsRepository.php deleted file mode 100644 index 92335be2..00000000 --- a/tests/inc/model/car/CarsRepository.php +++ /dev/null @@ -1,24 +0,0 @@ - - */ -final class CarsRepository extends Repository -{ - static function getEntityClassNames(): array - { - return [Car::class]; - } - - - public function getByName(string $name): ?Car - { - return $this->getBy(['name' => $name]); - } -} diff --git a/tests/inc/model/car/FuelType.php b/tests/inc/model/car/FuelType.php deleted file mode 100644 index c62059c6..00000000 --- a/tests/inc/model/car/FuelType.php +++ /dev/null @@ -1,12 +0,0 @@ -