From 1a213ab5f877920fa7151102beb3148ace0be747 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 13 Feb 2024 16:04:10 +0100 Subject: [PATCH] Create a simple test to play with foundry - problem with test db remaining --- .../PrivateCommentEmbeddableFactory.php | 58 ++++++++++++ .../ChillMainBundle/Factory/UserFactory.php | 83 +++++++++++++++++ .../AccompanyingPeriodFactory.php | 83 +++++++++++++++++ .../AccompanyingPeriodWorkFactory.php | 89 +++++++++++++++++++ .../AccompanyingPeriodRepositoryTest.php | 53 +++++++++++ 5 files changed, 366 insertions(+) create mode 100644 src/Bundle/ChillMainBundle/Factory/Embeddable/PrivateCommentEmbeddableFactory.php create mode 100644 src/Bundle/ChillMainBundle/Factory/UserFactory.php create mode 100644 src/Bundle/ChillPersonBundle/Factory/AccompanyingPeriod/AccompanyingPeriodFactory.php create mode 100644 src/Bundle/ChillPersonBundle/Factory/AccompanyingPeriod/AccompanyingPeriodWorkFactory.php create mode 100644 src/Bundle/ChillPersonBundle/Tests/Repository/AccompanyingPeriodRepositoryTest.php diff --git a/src/Bundle/ChillMainBundle/Factory/Embeddable/PrivateCommentEmbeddableFactory.php b/src/Bundle/ChillMainBundle/Factory/Embeddable/PrivateCommentEmbeddableFactory.php new file mode 100644 index 000000000..68c7e8c7e --- /dev/null +++ b/src/Bundle/ChillMainBundle/Factory/Embeddable/PrivateCommentEmbeddableFactory.php @@ -0,0 +1,58 @@ + + * + * @method PrivateCommentEmbeddable|Proxy create(array|callable $attributes = []) + * @method static PrivateCommentEmbeddable|Proxy createOne(array $attributes = []) + * @method static PrivateCommentEmbeddable[]|Proxy[] createMany(int $number, array|callable $attributes = []) + * @method static PrivateCommentEmbeddable[]|Proxy[] createSequence(iterable|callable $sequence) + * + * @phpstan-method Proxy create(array|callable $attributes = []) + * @phpstan-method static Proxy createOne(array $attributes = []) + */ +final class PrivateCommentEmbeddableFactory extends ModelFactory +{ + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services + * + * @todo inject services if required + */ + public function __construct() + { + parent::__construct(); + } + + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories + * + * @todo add your default values here + */ + protected function getDefaults(): array + { + return [ + ]; + } + + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization + */ + protected function initialize(): self + { + return $this + ->withoutPersisting() + // ->afterInstantiate(function(PrivateCommentEmbeddable $privateCommentEmbeddable): void {}) + ; + } + + protected static function getClass(): string + { + return PrivateCommentEmbeddable::class; + } +} diff --git a/src/Bundle/ChillMainBundle/Factory/UserFactory.php b/src/Bundle/ChillMainBundle/Factory/UserFactory.php new file mode 100644 index 000000000..884c6c174 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Factory/UserFactory.php @@ -0,0 +1,83 @@ + + * + * @method User|Proxy create(array|callable $attributes = []) + * @method static User|Proxy createOne(array $attributes = []) + * @method static User|Proxy find(object|array|mixed $criteria) + * @method static User|Proxy findOrCreate(array $attributes) + * @method static User|Proxy first(string $sortedField = 'id') + * @method static User|Proxy last(string $sortedField = 'id') + * @method static User|Proxy random(array $attributes = []) + * @method static User|Proxy randomOrCreate(array $attributes = []) + * @method static EntityRepository|RepositoryProxy repository() + * @method static User[]|Proxy[] all() + * @method static User[]|Proxy[] createMany(int $number, array|callable $attributes = []) + * @method static User[]|Proxy[] createSequence(iterable|callable $sequence) + * @method static User[]|Proxy[] findBy(array $attributes) + * @method static User[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) + * @method static User[]|Proxy[] randomSet(int $number, array $attributes = []) + * + * @phpstan-method Proxy create(array|callable $attributes = []) + * @phpstan-method static Proxy createOne(array $attributes = []) + * @phpstan-method static Proxy find(object|array|mixed $criteria) + * @phpstan-method static Proxy findOrCreate(array $attributes) + * @phpstan-method static Proxy first(string $sortedField = 'id') + * @phpstan-method static Proxy last(string $sortedField = 'id') + * @phpstan-method static Proxy random(array $attributes = []) + * @phpstan-method static Proxy randomOrCreate(array $attributes = []) + * @phpstan-method static RepositoryProxy repository() + */ +final class UserFactory extends ModelFactory +{ + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services + * + * @todo inject services if required + */ + public function __construct() + { + parent::__construct(); + } + + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories + * + * @todo add your default values here + */ + protected function getDefaults(): array + { + return [ + 'attributes' => [], + 'enabled' => self::faker()->boolean(), + 'label' => self::faker()->text(200), + 'locked' => self::faker()->boolean(), + 'password' => self::faker()->text(255), + 'username' => self::faker()->text(80), + ]; + } + + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization + */ + protected function initialize(): self + { + return $this + // ->afterInstantiate(function(User $user): void {}) + ; + } + + protected static function getClass(): string + { + return User::class; + } +} diff --git a/src/Bundle/ChillPersonBundle/Factory/AccompanyingPeriod/AccompanyingPeriodFactory.php b/src/Bundle/ChillPersonBundle/Factory/AccompanyingPeriod/AccompanyingPeriodFactory.php new file mode 100644 index 000000000..6f9b73f2a --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Factory/AccompanyingPeriod/AccompanyingPeriodFactory.php @@ -0,0 +1,83 @@ + + * + * @method AccompanyingPeriod|Proxy create(array|callable $attributes = []) + * @method static AccompanyingPeriod|Proxy createOne(array $attributes = []) + * @method static AccompanyingPeriod|Proxy find(object|array|mixed $criteria) + * @method static AccompanyingPeriod|Proxy findOrCreate(array $attributes) + * @method static AccompanyingPeriod|Proxy first(string $sortedField = 'id') + * @method static AccompanyingPeriod|Proxy last(string $sortedField = 'id') + * @method static AccompanyingPeriod|Proxy random(array $attributes = []) + * @method static AccompanyingPeriod|Proxy randomOrCreate(array $attributes = []) + * @method static EntityRepository|RepositoryProxy repository() + * @method static AccompanyingPeriod[]|Proxy[] all() + * @method static AccompanyingPeriod[]|Proxy[] createMany(int $number, array|callable $attributes = []) + * @method static AccompanyingPeriod[]|Proxy[] createSequence(iterable|callable $sequence) + * @method static AccompanyingPeriod[]|Proxy[] findBy(array $attributes) + * @method static AccompanyingPeriod[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) + * @method static AccompanyingPeriod[]|Proxy[] randomSet(int $number, array $attributes = []) + * + * @phpstan-method Proxy create(array|callable $attributes = []) + * @phpstan-method static Proxy createOne(array $attributes = []) + * @phpstan-method static Proxy find(object|array|mixed $criteria) + * @phpstan-method static Proxy findOrCreate(array $attributes) + * @phpstan-method static Proxy first(string $sortedField = 'id') + * @phpstan-method static Proxy last(string $sortedField = 'id') + * @phpstan-method static Proxy random(array $attributes = []) + * @phpstan-method static Proxy randomOrCreate(array $attributes = []) + * @phpstan-method static RepositoryProxy repository() + * @phpstan-method static list> all() + */ +final class AccompanyingPeriodFactory extends ModelFactory +{ + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services + * + * @todo inject services if required + */ + public function __construct() + { + parent::__construct(); + } + + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories + * + * @todo add your default values here + */ + protected function getDefaults(): array + { + return [ + 'confidential' => self::faker()->boolean(), + 'emergency' => self::faker()->boolean(), + 'openingDate' => self::faker()->dateTime(), + 'remark' => self::faker()->text(), + 'requestorAnonymous' => self::faker()->boolean(), + ]; + } + + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization + */ + protected function initialize(): self + { + return $this + // ->afterInstantiate(function(AccompanyingPeriod $accompanyingPeriod): void {}) + ; + } + + protected static function getClass(): string + { + return AccompanyingPeriod::class; + } +} diff --git a/src/Bundle/ChillPersonBundle/Factory/AccompanyingPeriod/AccompanyingPeriodWorkFactory.php b/src/Bundle/ChillPersonBundle/Factory/AccompanyingPeriod/AccompanyingPeriodWorkFactory.php new file mode 100644 index 000000000..4541f0cfd --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Factory/AccompanyingPeriod/AccompanyingPeriodWorkFactory.php @@ -0,0 +1,89 @@ + + * + * @method AccompanyingPeriodWork|Proxy create(array|callable $attributes = []) + * @method static AccompanyingPeriodWork|Proxy createOne(array $attributes = []) + * @method static AccompanyingPeriodWork|Proxy find(object|array|mixed $criteria) + * @method static AccompanyingPeriodWork|Proxy findOrCreate(array $attributes) + * @method static AccompanyingPeriodWork|Proxy first(string $sortedField = 'id') + * @method static AccompanyingPeriodWork|Proxy last(string $sortedField = 'id') + * @method static AccompanyingPeriodWork|Proxy random(array $attributes = []) + * @method static AccompanyingPeriodWork|Proxy randomOrCreate(array $attributes = []) + * @method static EntityRepository|RepositoryProxy repository() + * @method static AccompanyingPeriodWork[]|Proxy[] all() + * @method static AccompanyingPeriodWork[]|Proxy[] createMany(int $number, array|callable $attributes = []) + * @method static AccompanyingPeriodWork[]|Proxy[] createSequence(iterable|callable $sequence) + * @method static AccompanyingPeriodWork[]|Proxy[] findBy(array $attributes) + * @method static AccompanyingPeriodWork[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) + * @method static AccompanyingPeriodWork[]|Proxy[] randomSet(int $number, array $attributes = []) + * + * @phpstan-method Proxy create(array|callable $attributes = []) + * @phpstan-method static Proxy createOne(array $attributes = []) + * @phpstan-method static Proxy find(object|array|mixed $criteria) + * @phpstan-method static Proxy findOrCreate(array $attributes) + * @phpstan-method static Proxy first(string $sortedField = 'id') + * @phpstan-method static Proxy last(string $sortedField = 'id') + * @phpstan-method static Proxy random(array $attributes = []) + * @phpstan-method static Proxy randomOrCreate(array $attributes = []) + * @phpstan-method static RepositoryProxy repository() + */ +final class AccompanyingPeriodWorkFactory extends ModelFactory +{ + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services + * + * @todo inject services if required + */ + public function __construct() + { + parent::__construct(); + } + + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories + * + * @todo add your default values here + */ + protected function getDefaults(): array + { + return [ + 'createdAt' => \DateTimeImmutable::createFromMutable(self::faker()->dateTime()), + 'createdAutomatically' => self::faker()->boolean(), + 'createdAutomaticallyReason' => self::faker()->text(), + 'createdBy' => UserFactory::new(), + 'note' => self::faker()->text(), + 'privateComment' => PrivateCommentEmbeddableFactory::new(), + 'startDate' => \DateTimeImmutable::createFromMutable(self::faker()->dateTime()), + 'updatedAt' => \DateTimeImmutable::createFromMutable(self::faker()->dateTime()), + 'updatedBy' => UserFactory::new(), + 'version' => self::faker()->randomNumber(), + ]; + } + + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization + */ + protected function initialize(): self + { + return $this + // ->afterInstantiate(function(AccompanyingPeriodWork $accompanyingPeriodWork): void {}) + ; + } + + protected static function getClass(): string + { + return AccompanyingPeriodWork::class; + } +} diff --git a/src/Bundle/ChillPersonBundle/Tests/Repository/AccompanyingPeriodRepositoryTest.php b/src/Bundle/ChillPersonBundle/Tests/Repository/AccompanyingPeriodRepositoryTest.php new file mode 100644 index 000000000..4590deab6 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Repository/AccompanyingPeriodRepositoryTest.php @@ -0,0 +1,53 @@ + 'Bob ISLA' + ]); + + $userTwo = UserFactory::createOne([ + 'username' => 'Richard REEVES' + ]); + + $apOne = AccompanyingPeriodFactory::createOne([ + 'step' => AccompanyingPeriod::STEP_CONFIRMED, + 'user' => $user->object() + ]); + + $apTwo = AccompanyingPeriodFactory::createOne([ + 'step' => AccompanyingPeriod::STEP_DRAFT, + 'user' => $user->object() + ]); + + $apThree = AccompanyingPeriodFactory::createOne([ + 'step' => AccompanyingPeriod::STEP_CONFIRMED, + 'user' => $userTwo->object() + ]); + + $this->assertEquals($apOne, $this->getAccompanyingPeriodRepository()->findConfirmedByUser($user->object())); + + } + + private function getAccompanyingPeriodRepository(): AccompanyingPeriodRepository + { + return self::$container->get(AccompanyingPeriodRepository::class); + } + +}