From f845af9592e422625c4ec3223282ca5a2e5bfabe Mon Sep 17 00:00:00 2001 From: Hecht Date: Wed, 31 Jan 2024 13:09:47 +0100 Subject: [PATCH] Fix unit test :) --- composer.lock | 12 +++++------ config/packages/doctrine.yaml | 3 ++- migrations/Version20240129212818.php | 31 ++++++++++++++++++++++++++++ src/Entity/Dojo.php | 11 ++++++---- src/Entity/User.php | 6 ++++++ src/Security/AccessTokenHandler.php | 13 ++++++++++-- start_postgres.sh | 3 +++ tests/DojoTest.php | 11 +++------- 8 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 migrations/Version20240129212818.php create mode 100755 start_postgres.sh diff --git a/composer.lock b/composer.lock index 0ac8651..d6d5a9b 100644 --- a/composer.lock +++ b/composer.lock @@ -8593,16 +8593,16 @@ }, { "name": "symfony/phpunit-bridge", - "version": "v7.0.2", + "version": "v7.0.3", "source": { "type": "git", "url": "https://github.com/symfony/phpunit-bridge.git", - "reference": "92df075808c9437beca9540e25ae0c40eea1c061" + "reference": "0a2eeb0d9e68bf01660e3e903f8113508bb46132" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/phpunit-bridge/zipball/92df075808c9437beca9540e25ae0c40eea1c061", - "reference": "92df075808c9437beca9540e25ae0c40eea1c061", + "url": "https://api.github.com/repos/symfony/phpunit-bridge/zipball/0a2eeb0d9e68bf01660e3e903f8113508bb46132", + "reference": "0a2eeb0d9e68bf01660e3e903f8113508bb46132", "shasum": "" }, "require": { @@ -8654,7 +8654,7 @@ "description": "Provides utilities for PHPUnit, especially user deprecation notices management", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/phpunit-bridge/tree/v7.0.2" + "source": "https://github.com/symfony/phpunit-bridge/tree/v7.0.3" }, "funding": [ { @@ -8670,7 +8670,7 @@ "type": "tidelift" } ], - "time": "2023-12-19T11:23:03+00:00" + "time": "2024-01-23T15:02:46+00:00" }, { "name": "symfony/process", diff --git a/config/packages/doctrine.yaml b/config/packages/doctrine.yaml index ec0f77e..8610fd8 100644 --- a/config/packages/doctrine.yaml +++ b/config/packages/doctrine.yaml @@ -4,9 +4,10 @@ doctrine: # IMPORTANT: You MUST configure your server version, # either here or in the DATABASE_URL env var (see .env file) - #server_version: '15' + server_version: '%env(resolve:DATABASE_VERSION)%' profiling_collect_backtrace: '%kernel.debug%' + use_savepoints: true orm: auto_generate_proxy_classes: true enable_lazy_ghost_objects: true diff --git a/migrations/Version20240129212818.php b/migrations/Version20240129212818.php new file mode 100644 index 0000000..da5f16d --- /dev/null +++ b/migrations/Version20240129212818.php @@ -0,0 +1,31 @@ +addSql('CREATE SCHEMA public'); + } +} diff --git a/src/Entity/Dojo.php b/src/Entity/Dojo.php index 5049ca5..c2acf34 100644 --- a/src/Entity/Dojo.php +++ b/src/Entity/Dojo.php @@ -4,14 +4,14 @@ namespace App\Entity; use ApiPlatform\Metadata\ApiProperty; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; -use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping\Column; +use Doctrine\ORM\Mapping\Entity; use Doctrine\ORM\Mapping\JoinColumn; use Doctrine\ORM\Mapping\ManyToOne; use Doctrine\ORM\Mapping\OneToMany; use Doctrine\ORM\Mapping\OneToOne; -#[ORM\Entity] +#[Entity] class Dojo extends Thing { @@ -29,7 +29,9 @@ class Dojo extends Thing #[JoinColumn(onDelete: 'cascade', nullable: true)] public ?Village $village; - #[OneToOne(inversedBy: 'dojo')] + #[OneToOne(inversedBy: 'dojo', cascade: [ + 'persist' + ])] #[JoinColumn(onDelete: 'cascade', nullable: false)] public User $owner; @@ -59,6 +61,7 @@ class Dojo extends Thing } /** + * * @return Collection */ public function getMembers(): Collection @@ -68,7 +71,7 @@ class Dojo extends Thing public function addMember(Character $member): static { - if (!$this->members->contains($member)) { + if (! $this->members->contains($member)) { $this->members->add($member); $member->setDojo($this); } diff --git a/src/Entity/User.php b/src/Entity/User.php index 477c876..d26c7a9 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -28,6 +28,12 @@ class User extends Thing implements UserInterface #[Column(type: 'json')] public mixed $properties; + public function __construct(string $authName) + { + $this->authName = $authName; + $this->properties = []; + } + public function getProperties(): array { return $this->properties; diff --git a/src/Security/AccessTokenHandler.php b/src/Security/AccessTokenHandler.php index a8d4448..4726f30 100644 --- a/src/Security/AccessTokenHandler.php +++ b/src/Security/AccessTokenHandler.php @@ -1,6 +1,7 @@ logger->critical("Nearly there! $auth_name"); - return new UserBadge($auth_name, fn (string $id) => $this->userRepository->findByAuthName($id)); + return new UserBadge($auth_name, fn (string $id) => $this->getOrCreateUser($id)); + } + + private function getOrCreateUser(string $authName): User + { + $user = $this->userRepository->findOneByAuthName($authName); + if (NULL === $user) { + $user = new User($authName); + } + return $user; } } diff --git a/start_postgres.sh b/start_postgres.sh new file mode 100755 index 0000000..4749fe9 --- /dev/null +++ b/start_postgres.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +podman run --rm --name ag-dojo-postgres -e POSTGRES_USER=foo -e POSTGRES_PASSWORD=bar -e POSTGRES_HOST_AUTH_METHOD=trust -p "5432:5432" docker.io/library/postgres:14.5 diff --git a/tests/DojoTest.php b/tests/DojoTest.php index 4c0cbb8..a33ba5d 100644 --- a/tests/DojoTest.php +++ b/tests/DojoTest.php @@ -3,7 +3,6 @@ namespace App\Tests; use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; use ApiPlatform\Symfony\Bundle\Test\Response; -use App\Factory\UserFactory; use App\Repository\UserRepository; use Zenstruck\Foundry\Test\Factories; use Zenstruck\Foundry\Test\ResetDatabase; @@ -28,13 +27,6 @@ class DojoTest extends ApiTestCase public function testCreateDojo(): void { - UserFactory::createOne([ - 'authName' => "blablabla" - ]); - - $userRepository = $this->getContainer()->get(UserRepository::class); - $this->assertCount(1, $userRepository->findAll()); - /** * * @var Response $response @@ -51,6 +43,9 @@ class DojoTest extends ApiTestCase ]); $this->assertResponseStatusCodeSame(201); + + $userRepository = $this->getContainer()->get(UserRepository::class); + $this->assertCount(1, $userRepository->findAll()); } }