Skip to content

Commit

Permalink
Reworked tests for BackedEnums
Browse files Browse the repository at this point in the history
  • Loading branch information
Zazimou authored and datronjzima committed Jan 3, 2024
1 parent 23232b9 commit 7a12611
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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);
}
}

Expand Down
96 changes: 67 additions & 29 deletions tests/cases/integration/Entity/entity.enumProps.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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);

}
Expand Down
20 changes: 5 additions & 15 deletions tests/db/array-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace NextrasTests\Orm;


use inc\model\book\GenreEnum;
use Nextras\Dbal\Utils\DateTimeImmutable;


Expand Down Expand Up @@ -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]);
Expand All @@ -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]);
Expand All @@ -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]);
Expand All @@ -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);
Expand Down Expand Up @@ -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();
21 changes: 12 additions & 9 deletions tests/db/mysql-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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');
7 changes: 1 addition & 6 deletions tests/db/mysql-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
);
1 change: 0 additions & 1 deletion tests/inc/model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/inc/model/book/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


use DateTimeImmutable;
use inc\model\book\GenreEnum;
use Nextras\Orm\Entity\Entity;
use Nextras\Orm\Relationships\ManyHasMany;

Expand All @@ -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}
Expand Down
13 changes: 13 additions & 0 deletions tests/inc/model/book/GenreEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace inc\model\book;


enum GenreEnum: string
{
case HORROR = 'horror';
case FANTASY = 'fantasy';
case SCIFI = 'sciFi';
case ROMANCE = 'romance';
case THRILLER = 'thriller';
}
16 changes: 0 additions & 16 deletions tests/inc/model/car/Car.php

This file was deleted.

14 changes: 0 additions & 14 deletions tests/inc/model/car/CarsMapper.php

This file was deleted.

24 changes: 0 additions & 24 deletions tests/inc/model/car/CarsRepository.php

This file was deleted.

Loading

0 comments on commit 7a12611

Please sign in to comment.