From 57e53526e6b804a4f3ffd00107a4192d2a7afb85 Mon Sep 17 00:00:00 2001 From: Hecht Date: Mon, 17 Jun 2024 22:09:12 +0200 Subject: [PATCH] Fixed some issues with retrieving dojo and character --- config/api_platform/resources.xml | 1 - src/Controller/GetUserDojo.php | 21 --------- src/Factory/CharacterFactory.php | 8 ++-- src/Factory/DojoFactory.php | 52 ++++++++++----------- src/Factory/FightFactory.php | 52 ++++++++++----------- src/Factory/TechniqueFactory.php | 8 ++-- src/Factory/TournamentFactory.php | 52 ++++++++++----------- src/Factory/UserFactory.php | 8 ++-- src/State/DojoPostProcessor.php | 2 + tests/CharacterTest.php | 39 ++++++++++++++++ tests/DojoTest.php | 75 +++++++++++-------------------- tests/UserTest.php | 14 +++++- 12 files changed, 171 insertions(+), 161 deletions(-) delete mode 100644 src/Controller/GetUserDojo.php diff --git a/config/api_platform/resources.xml b/config/api_platform/resources.xml index 8fcc585..d7488a9 100644 --- a/config/api_platform/resources.xml +++ b/config/api_platform/resources.xml @@ -79,7 +79,6 @@ - diff --git a/src/Controller/GetUserDojo.php b/src/Controller/GetUserDojo.php deleted file mode 100644 index d6607b2..0000000 --- a/src/Controller/GetUserDojo.php +++ /dev/null @@ -1,21 +0,0 @@ -getRepository(Dojo::class)->findOneByOwner($this->security->getUser()); - } -} - diff --git a/src/Factory/CharacterFactory.php b/src/Factory/CharacterFactory.php index 0b61aae..9a87d4d 100644 --- a/src/Factory/CharacterFactory.php +++ b/src/Factory/CharacterFactory.php @@ -2,7 +2,7 @@ namespace App\Factory; use App\Entity\Character; -use Zenstruck\Foundry\ModelFactory; +use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory; /** * @@ -24,7 +24,7 @@ use Zenstruck\Foundry\ModelFactory; * @method static Character[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) * @method static Character[]|Proxy[] randomSet(int $number, array $attributes = []) */ -final class CharacterFactory extends ModelFactory +final class CharacterFactory extends PersistentProxyObjectFactory { /** @@ -44,7 +44,7 @@ final class CharacterFactory extends ModelFactory * * @todo add your default values here */ - protected function getDefaults(): array + protected function defaults(): array { return [ 'name' => self::faker()->firstName(), @@ -66,7 +66,7 @@ final class CharacterFactory extends ModelFactory // ->afterInstantiate(function(Character $character): void {}) } - protected static function getClass(): string + public static function class(): string { return Character::class; } diff --git a/src/Factory/DojoFactory.php b/src/Factory/DojoFactory.php index c16bdfb..d62d84a 100644 --- a/src/Factory/DojoFactory.php +++ b/src/Factory/DojoFactory.php @@ -1,35 +1,34 @@ * - * @method Dojo|Proxy create(array|callable $attributes = []) - * @method static Dojo|Proxy createOne(array $attributes = []) - * @method static Dojo|Proxy find(object|array|mixed $criteria) - * @method static Dojo|Proxy findOrCreate(array $attributes) - * @method static Dojo|Proxy first(string $sortedField = 'id') - * @method static Dojo|Proxy last(string $sortedField = 'id') - * @method static Dojo|Proxy random(array $attributes = []) - * @method static Dojo|Proxy randomOrCreate(array $attributes = []) + * @method Dojo|Proxy create(array|callable $attributes = []) + * @method static Dojo|Proxy createOne(array $attributes = []) + * @method static Dojo|Proxy find(object|array|mixed $criteria) + * @method static Dojo|Proxy findOrCreate(array $attributes) + * @method static Dojo|Proxy first(string $sortedField = 'id') + * @method static Dojo|Proxy last(string $sortedField = 'id') + * @method static Dojo|Proxy random(array $attributes = []) + * @method static Dojo|Proxy randomOrCreate(array $attributes = []) * @method static EntityRepository|RepositoryProxy repository() - * @method static Dojo[]|Proxy[] all() - * @method static Dojo[]|Proxy[] createMany(int $number, array|callable $attributes = []) - * @method static Dojo[]|Proxy[] createSequence(iterable|callable $sequence) - * @method static Dojo[]|Proxy[] findBy(array $attributes) - * @method static Dojo[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) - * @method static Dojo[]|Proxy[] randomSet(int $number, array $attributes = []) + * @method static Dojo[]|Proxy[] all() + * @method static Dojo[]|Proxy[] createMany(int $number, array|callable $attributes = []) + * @method static Dojo[]|Proxy[] createSequence(iterable|callable $sequence) + * @method static Dojo[]|Proxy[] findBy(array $attributes) + * @method static Dojo[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) + * @method static Dojo[]|Proxy[] randomSet(int $number, array $attributes = []) */ -final class DojoFactory extends ModelFactory +final class DojoFactory extends PersistentProxyObjectFactory { + /** + * * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services * * @todo inject services if required @@ -40,29 +39,30 @@ final class DojoFactory extends ModelFactory } /** + * * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories * * @todo add your default values here */ - protected function getDefaults(): array + protected function defaults(): array { return [ 'name' => self::faker()->text(), - 'owner' => UserFactory::new(), + 'owner' => UserFactory::new() ]; } /** + * * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ protected function initialize(): self { - return $this - // ->afterInstantiate(function(Dojo $dojo): void {}) - ; + return $this; + // ->afterInstantiate(function(Dojo $dojo): void {}) } - protected static function getClass(): string + public static function class(): string { return Dojo::class; } diff --git a/src/Factory/FightFactory.php b/src/Factory/FightFactory.php index 2030a2f..2ad04fc 100644 --- a/src/Factory/FightFactory.php +++ b/src/Factory/FightFactory.php @@ -1,35 +1,34 @@ * - * @method Fight|Proxy create(array|callable $attributes = []) - * @method static Fight|Proxy createOne(array $attributes = []) - * @method static Fight|Proxy find(object|array|mixed $criteria) - * @method static Fight|Proxy findOrCreate(array $attributes) - * @method static Fight|Proxy first(string $sortedField = 'id') - * @method static Fight|Proxy last(string $sortedField = 'id') - * @method static Fight|Proxy random(array $attributes = []) - * @method static Fight|Proxy randomOrCreate(array $attributes = []) + * @method Fight|Proxy create(array|callable $attributes = []) + * @method static Fight|Proxy createOne(array $attributes = []) + * @method static Fight|Proxy find(object|array|mixed $criteria) + * @method static Fight|Proxy findOrCreate(array $attributes) + * @method static Fight|Proxy first(string $sortedField = 'id') + * @method static Fight|Proxy last(string $sortedField = 'id') + * @method static Fight|Proxy random(array $attributes = []) + * @method static Fight|Proxy randomOrCreate(array $attributes = []) * @method static FightRepository|RepositoryProxy repository() - * @method static Fight[]|Proxy[] all() - * @method static Fight[]|Proxy[] createMany(int $number, array|callable $attributes = []) - * @method static Fight[]|Proxy[] createSequence(iterable|callable $sequence) - * @method static Fight[]|Proxy[] findBy(array $attributes) - * @method static Fight[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) - * @method static Fight[]|Proxy[] randomSet(int $number, array $attributes = []) + * @method static Fight[]|Proxy[] all() + * @method static Fight[]|Proxy[] createMany(int $number, array|callable $attributes = []) + * @method static Fight[]|Proxy[] createSequence(iterable|callable $sequence) + * @method static Fight[]|Proxy[] findBy(array $attributes) + * @method static Fight[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) + * @method static Fight[]|Proxy[] randomSet(int $number, array $attributes = []) */ -final class FightFactory extends ModelFactory +final class FightFactory extends PersistentProxyObjectFactory { + /** + * * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services * * @todo inject services if required @@ -40,31 +39,32 @@ final class FightFactory extends ModelFactory } /** + * * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories * * @todo add your default values here */ - protected function getDefaults(): array + protected function defaults(): array { return [ 'events' => [], 'startDate' => self::faker()->dateTime(), 'tournament' => TournamentFactory::new(), - 'winner' => CharacterFactory::new(), + 'winner' => CharacterFactory::new() ]; } /** + * * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ protected function initialize(): self { - return $this - // ->afterInstantiate(function(Fight $fight): void {}) - ; + return $this; + // ->afterInstantiate(function(Fight $fight): void {}) } - protected static function getClass(): string + public static function class(): string { return Fight::class; } diff --git a/src/Factory/TechniqueFactory.php b/src/Factory/TechniqueFactory.php index 5f6b23d..b1a32a2 100644 --- a/src/Factory/TechniqueFactory.php +++ b/src/Factory/TechniqueFactory.php @@ -2,7 +2,7 @@ namespace App\Factory; use App\Entity\Technique; -use Zenstruck\Foundry\ModelFactory; +use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory; /** * @@ -24,7 +24,7 @@ use Zenstruck\Foundry\ModelFactory; * @method static Technique[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) * @method static Technique[]|Proxy[] randomSet(int $number, array $attributes = []) */ -final class TechniqueFactory extends ModelFactory +final class TechniqueFactory extends PersistentProxyObjectFactory { /** @@ -44,7 +44,7 @@ final class TechniqueFactory extends ModelFactory * * @todo add your default values here */ - protected function getDefaults(): array + protected function defaults(): array { return [ 'name' => self::faker()->ean13(), @@ -66,7 +66,7 @@ final class TechniqueFactory extends ModelFactory // ->afterInstantiate(function(Technique $technique): void {}) } - protected static function getClass(): string + public static function class(): string { return Technique::class; } diff --git a/src/Factory/TournamentFactory.php b/src/Factory/TournamentFactory.php index 681fb24..711e723 100644 --- a/src/Factory/TournamentFactory.php +++ b/src/Factory/TournamentFactory.php @@ -1,35 +1,34 @@ * - * @method Tournament|Proxy create(array|callable $attributes = []) - * @method static Tournament|Proxy createOne(array $attributes = []) - * @method static Tournament|Proxy find(object|array|mixed $criteria) - * @method static Tournament|Proxy findOrCreate(array $attributes) - * @method static Tournament|Proxy first(string $sortedField = 'id') - * @method static Tournament|Proxy last(string $sortedField = 'id') - * @method static Tournament|Proxy random(array $attributes = []) - * @method static Tournament|Proxy randomOrCreate(array $attributes = []) + * @method Tournament|Proxy create(array|callable $attributes = []) + * @method static Tournament|Proxy createOne(array $attributes = []) + * @method static Tournament|Proxy find(object|array|mixed $criteria) + * @method static Tournament|Proxy findOrCreate(array $attributes) + * @method static Tournament|Proxy first(string $sortedField = 'id') + * @method static Tournament|Proxy last(string $sortedField = 'id') + * @method static Tournament|Proxy random(array $attributes = []) + * @method static Tournament|Proxy randomOrCreate(array $attributes = []) * @method static TournamentRepository|RepositoryProxy repository() - * @method static Tournament[]|Proxy[] all() - * @method static Tournament[]|Proxy[] createMany(int $number, array|callable $attributes = []) - * @method static Tournament[]|Proxy[] createSequence(iterable|callable $sequence) - * @method static Tournament[]|Proxy[] findBy(array $attributes) - * @method static Tournament[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) - * @method static Tournament[]|Proxy[] randomSet(int $number, array $attributes = []) + * @method static Tournament[]|Proxy[] all() + * @method static Tournament[]|Proxy[] createMany(int $number, array|callable $attributes = []) + * @method static Tournament[]|Proxy[] createSequence(iterable|callable $sequence) + * @method static Tournament[]|Proxy[] findBy(array $attributes) + * @method static Tournament[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) + * @method static Tournament[]|Proxy[] randomSet(int $number, array $attributes = []) */ -final class TournamentFactory extends ModelFactory +final class TournamentFactory extends PersistentProxyObjectFactory { + /** + * * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services * * @todo inject services if required @@ -40,29 +39,30 @@ final class TournamentFactory extends ModelFactory } /** + * * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories * * @todo add your default values here */ - protected function getDefaults(): array + protected function defaults(): array { return [ 'name' => self::faker()->text(255), - 'startDate' => self::faker()->dateTime(), + 'startDate' => self::faker()->dateTime() ]; } /** + * * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ protected function initialize(): self { - return $this - // ->afterInstantiate(function(Tournament $tournament): void {}) - ; + return $this; + // ->afterInstantiate(function(Tournament $tournament): void {}) } - protected static function getClass(): string + public static function class(): string { return Tournament::class; } diff --git a/src/Factory/UserFactory.php b/src/Factory/UserFactory.php index 6d9e013..128bb5c 100644 --- a/src/Factory/UserFactory.php +++ b/src/Factory/UserFactory.php @@ -2,7 +2,7 @@ namespace App\Factory; use App\Entity\User; -use Zenstruck\Foundry\ModelFactory; +use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory; /** * @@ -24,7 +24,7 @@ use Zenstruck\Foundry\ModelFactory; * @method static User[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) * @method static User[]|Proxy[] randomSet(int $number, array $attributes = []) */ -final class UserFactory extends ModelFactory +final class UserFactory extends PersistentProxyObjectFactory { /** @@ -44,7 +44,7 @@ final class UserFactory extends ModelFactory * * @todo add your default values here */ - protected function getDefaults(): array + protected function defaults(): array { return [ 'authName' => self::faker()->userName(), @@ -62,7 +62,7 @@ final class UserFactory extends ModelFactory // ->afterInstantiate(function(User $user): void {}) } - protected static function getClass(): string + public static function class(): string { return User::class; } diff --git a/src/State/DojoPostProcessor.php b/src/State/DojoPostProcessor.php index 4f72b3d..64dd022 100644 --- a/src/State/DojoPostProcessor.php +++ b/src/State/DojoPostProcessor.php @@ -35,6 +35,8 @@ class DojoPostProcessor implements ProcessorInterface private function updateDojo(Dojo $dojo): void { + $user = $this->security->getUser(); $dojo->setOwner($this->security->getUser()); + $user->setDojo($dojo); } } diff --git a/tests/CharacterTest.php b/tests/CharacterTest.php index 77aab74..8863265 100644 --- a/tests/CharacterTest.php +++ b/tests/CharacterTest.php @@ -1,14 +1,30 @@ $dojo, + 'techniques' => [ + $technique + ] + ]); + + return $characters; + } + /** * Requirement: A user should be able see all characters from a dojo, but only the public fields! */ @@ -92,6 +108,29 @@ class CharacterTest extends AbstractTest ->toBase32(), $chars[0]['techniques'][0]); } + /** + * Regression test, to verify that the method will also work when multiple dojos are created. + */ + public function testRetrieveCharactersFromOwnDojoOnly(): void + { + $dojo = DojoFactory::createOne([ + 'owner' => UserFactory::createOne() + ]); + $chars = $this->createCharacters($dojo); + + $otherDojos = DojoFactory::createMany(10); + foreach ($otherDojos as $otherDojo) { + $this->createCharacters($otherDojo); + } + + $response = static::createClientWithToken($dojo->getOwner()->authName)->request('GET', '/api/dojo/characters'); + $this->assertResponseStatusCodeSame(200); + + $readChars = $response->toArray(); + + $this->assertEquals(count($chars), count($readChars)); + } + /** * Requirement: MVP only (in the future the recuitment will be different). * A user should be able to create a single character. diff --git a/tests/DojoTest.php b/tests/DojoTest.php index 97ee37f..a4047a0 100644 --- a/tests/DojoTest.php +++ b/tests/DojoTest.php @@ -11,7 +11,7 @@ class DojoTest extends AbstractTest /** * Requirement: A user should be able to create a dojo! */ - public function testCreateDojo(): void + public function testDojoCreate(): void { $userName = "FooBarFigher"; $dojoName = "BigFightDojo"; @@ -27,58 +27,18 @@ class DojoTest extends AbstractTest $this->assertResponseStatusCodeSame(201); - $this->assertCount(1, $userRepository->findByAuthName($userName)); - } - - /** - * Requirement: A user should be request his own dojo! - */ - public function testUserReadDojo(): void - { - $userName = "FooBarFigher"; - $dojoName = "BigFightDojo"; - $dojo = DojoFactory::createOne( - [ - 'name' => $dojoName, - 'owner' => UserFactory::createOne([ - 'authName' => $userName - ]) - ]); - - static::createClientWithToken($userName)->request('GET', '/api/dojo'); - - $this->assertResponseStatusCodeSame(200); - - $this->assertJsonContains( - [ - 'name' => 'BigFightDojo', - 'members' => [], - 'owner' => $this->getIri($dojo->getOwner()), - 'id' => $dojo->getId() - ->toBase32() - ]); - } - - /** - * Requirement: A user should be request his own dojo! - * Fails, if the dojo is not yet created. - */ - public function testUserReadDojoNotYetCreated(): void - { - $userName = "FooBarFigher"; - $userRepository = $this->getContainer()->get(UserRepository::class); - - $this->assertCount(0, $userRepository->findByAuthName($userName)); + $user = $userRepository->findOneByAuthName($userName); - static::createClientWithToken($userName)->request('GET', '/api/dojo'); - - $this->assertResponseStatusCodeSame(404); + $this->assertJsonContains([ + 'name' => $dojoName, + 'owner' => $this->getIri($user) + ]); } /** * Requirement: A user should NOT be able to create more than one dojos! */ - public function testUserCannotCreateMultipleDojos(): void + public function testDojoUserCannotCreateMultipleDojos(): void { $userName = "FooBarFigher"; $dojoName = "BigFightDojo"; @@ -102,7 +62,7 @@ class DojoTest extends AbstractTest * Requirement: A user should be able to change the dojos name! * FIXME: Add limitation so users will not do this frequently. */ - public function testChangeDojoName(): void + public function testDojoChangeDojoName(): void { $userName = "FooBarFigher"; $dojoName = "BigFightDojo"; @@ -127,5 +87,24 @@ class DojoTest extends AbstractTest $this->assertResponseStatusCodeSame(200); } + + public function testDojoRetrieve(): void + { + $userName = "FooBarFigher"; + $dojoName = "SmallFightDojo"; + $dojo = DojoFactory::createOne( + [ + 'name' => $dojoName, + 'owner' => UserFactory::createOne([ + 'authName' => $userName + ]) + ]); + + static::createClientWithToken($userName)->request('GET', '/api/dojos/' . $dojo->getId()); + + $this->assertJsonContains([ + 'name' => $dojoName + ]); + } } diff --git a/tests/UserTest.php b/tests/UserTest.php index 1f59197..722f3a8 100644 --- a/tests/UserTest.php +++ b/tests/UserTest.php @@ -6,6 +6,18 @@ use App\Factory\UserFactory; class UserTest extends AbstractTest { + public function testUserGetByAuthname(): void + { + $requestUser = UserFactory::createOne(); + UserFactory::createMany(5); + + $response = static::createClientWithToken($requestUser->authName)->request('GET', + '/api/users/authName/' . $requestUser->authName); + + $this->assertResponseStatusCodeSame(200); + $this->assertEquals($requestUser->getId(), $response->toArray()['id']); + } + /** * Requirement: A user should be able to update his properties */ @@ -53,7 +65,7 @@ class UserTest extends AbstractTest ] ]); - $this->assertResponseStatusCodeSame(405); + $this->assertResponseStatusCodeSame(200); $this->assertNotEquals("foo.bar", $response->toArray()['authName']); }