test: add some KernelTestCase

This commit is contained in:
Maël Gangloff 2025-10-14 00:09:01 +02:00
parent 398ca8df2f
commit 6154759507
No known key found for this signature in database
GPG Key ID: 11FDC81C24A7F629
15 changed files with 884 additions and 3 deletions

View File

@ -2,3 +2,4 @@
KERNEL_CLASS='App\Kernel'
APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999
DATABASE_URL="postgresql://app:!ChangeMe!@127.0.0.1:5432/app?serverVersion=16&charset=utf8"

View File

@ -145,7 +145,9 @@
}
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^4.2",
"friendsofphp/php-cs-fixer": "^3.61",
"justinrainbow/json-schema": "^6.6",
"phpstan/phpstan": "^1.11",
"phpunit/phpunit": "^10",
"symfony/browser-kit": "7.1.*",
@ -154,6 +156,7 @@
"symfony/maker-bundle": "^1.0",
"symfony/phpunit-bridge": "^7.1",
"symfony/stopwatch": "7.1.*",
"symfony/web-profiler-bundle": "7.1.*"
"symfony/web-profiler-bundle": "7.1.*",
"zenstruck/foundry": "^2.7"
}
}

561
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "f7cc40c90a1eeef5ef0de9b8802930f7",
"content-hash": "335a422eda7617ccb7345442c645e384",
"packages": [
{
"name": "api-platform/core",
@ -11453,6 +11453,175 @@
],
"time": "2024-05-06T16:37:16+00:00"
},
{
"name": "doctrine/data-fixtures",
"version": "2.1.0",
"source": {
"type": "git",
"url": "https://github.com/doctrine/data-fixtures.git",
"reference": "f161e20f04ba5440a09330e156b40f04dd70d47f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/doctrine/data-fixtures/zipball/f161e20f04ba5440a09330e156b40f04dd70d47f",
"reference": "f161e20f04ba5440a09330e156b40f04dd70d47f",
"shasum": ""
},
"require": {
"doctrine/persistence": "^3.1 || ^4.0",
"php": "^8.1",
"psr/log": "^1.1 || ^2 || ^3"
},
"conflict": {
"doctrine/dbal": "<3.5 || >=5",
"doctrine/orm": "<2.14 || >=4",
"doctrine/phpcr-odm": "<1.3.0"
},
"require-dev": {
"doctrine/coding-standard": "^13",
"doctrine/dbal": "^3.5 || ^4",
"doctrine/mongodb-odm": "^1.3.0 || ^2.0.0",
"doctrine/orm": "^2.14 || ^3",
"ext-sqlite3": "*",
"fig/log-test": "^1",
"phpstan/phpstan": "2.1.17",
"phpunit/phpunit": "10.5.45",
"symfony/cache": "^6.4 || ^7",
"symfony/var-exporter": "^6.4 || ^7"
},
"suggest": {
"alcaeus/mongo-php-adapter": "For using MongoDB ODM 1.3 with PHP 7 (deprecated)",
"doctrine/mongodb-odm": "For loading MongoDB ODM fixtures",
"doctrine/orm": "For loading ORM fixtures",
"doctrine/phpcr-odm": "For loading PHPCR ODM fixtures"
},
"type": "library",
"autoload": {
"psr-4": {
"Doctrine\\Common\\DataFixtures\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jonathan Wage",
"email": "jonwage@gmail.com"
}
],
"description": "Data Fixtures for all Doctrine Object Managers",
"homepage": "https://www.doctrine-project.org",
"keywords": [
"database"
],
"support": {
"issues": "https://github.com/doctrine/data-fixtures/issues",
"source": "https://github.com/doctrine/data-fixtures/tree/2.1.0"
},
"funding": [
{
"url": "https://www.doctrine-project.org/sponsorship.html",
"type": "custom"
},
{
"url": "https://www.patreon.com/phpdoctrine",
"type": "patreon"
},
{
"url": "https://tidelift.com/funding/github/packagist/doctrine%2Fdata-fixtures",
"type": "tidelift"
}
],
"time": "2025-07-08T17:48:20+00:00"
},
{
"name": "doctrine/doctrine-fixtures-bundle",
"version": "4.2.0",
"source": {
"type": "git",
"url": "https://github.com/doctrine/DoctrineFixturesBundle.git",
"reference": "cd58d7738fe1fea1dbfd3e3f3bb421ee92d45e10"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/doctrine/DoctrineFixturesBundle/zipball/cd58d7738fe1fea1dbfd3e3f3bb421ee92d45e10",
"reference": "cd58d7738fe1fea1dbfd3e3f3bb421ee92d45e10",
"shasum": ""
},
"require": {
"doctrine/data-fixtures": "^2.0",
"doctrine/doctrine-bundle": "^2.2 || ^3.0",
"doctrine/orm": "^2.14.0 || ^3.0",
"doctrine/persistence": "^2.4 || ^3.0 || ^4.0",
"php": "^8.1",
"psr/log": "^2 || ^3",
"symfony/config": "^6.4 || ^7.0",
"symfony/console": "^6.4 || ^7.0",
"symfony/dependency-injection": "^6.4 || ^7.0",
"symfony/deprecation-contracts": "^2.1 || ^3",
"symfony/doctrine-bridge": "^6.4.16 || ^7.1.9",
"symfony/http-kernel": "^6.4 || ^7.0"
},
"conflict": {
"doctrine/dbal": "< 3"
},
"require-dev": {
"doctrine/coding-standard": "14.0.0",
"phpstan/phpstan": "2.1.11",
"phpunit/phpunit": "^10.5.38 || 11.4.14"
},
"type": "symfony-bundle",
"autoload": {
"psr-4": {
"Doctrine\\Bundle\\FixturesBundle\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Doctrine Project",
"homepage": "https://www.doctrine-project.org"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony DoctrineFixturesBundle",
"homepage": "https://www.doctrine-project.org",
"keywords": [
"Fixture",
"persistence"
],
"support": {
"issues": "https://github.com/doctrine/DoctrineFixturesBundle/issues",
"source": "https://github.com/doctrine/DoctrineFixturesBundle/tree/4.2.0"
},
"funding": [
{
"url": "https://www.doctrine-project.org/sponsorship.html",
"type": "custom"
},
{
"url": "https://www.patreon.com/phpdoctrine",
"type": "patreon"
},
{
"url": "https://tidelift.com/funding/github/packagist/doctrine%2Fdoctrine-fixtures-bundle",
"type": "tidelift"
}
],
"time": "2025-10-12T16:50:54+00:00"
},
{
"name": "evenement/evenement",
"version": "v3.0.2",
@ -11500,6 +11669,69 @@
},
"time": "2023-08-08T05:53:35+00:00"
},
{
"name": "fakerphp/faker",
"version": "v1.24.1",
"source": {
"type": "git",
"url": "https://github.com/FakerPHP/Faker.git",
"reference": "e0ee18eb1e6dc3cda3ce9fd97e5a0689a88a64b5"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/FakerPHP/Faker/zipball/e0ee18eb1e6dc3cda3ce9fd97e5a0689a88a64b5",
"reference": "e0ee18eb1e6dc3cda3ce9fd97e5a0689a88a64b5",
"shasum": ""
},
"require": {
"php": "^7.4 || ^8.0",
"psr/container": "^1.0 || ^2.0",
"symfony/deprecation-contracts": "^2.2 || ^3.0"
},
"conflict": {
"fzaninotto/faker": "*"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.4.1",
"doctrine/persistence": "^1.3 || ^2.0",
"ext-intl": "*",
"phpunit/phpunit": "^9.5.26",
"symfony/phpunit-bridge": "^5.4.16"
},
"suggest": {
"doctrine/orm": "Required to use Faker\\ORM\\Doctrine",
"ext-curl": "Required by Faker\\Provider\\Image to download images.",
"ext-dom": "Required by Faker\\Provider\\HtmlLorem for generating random HTML.",
"ext-iconv": "Required by Faker\\Provider\\ru_RU\\Text::realText() for generating real Russian text.",
"ext-mbstring": "Required for multibyte Unicode string functionality."
},
"type": "library",
"autoload": {
"psr-4": {
"Faker\\": "src/Faker/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "François Zaninotto"
}
],
"description": "Faker is a PHP library that generates fake data for you.",
"keywords": [
"data",
"faker",
"fixtures"
],
"support": {
"issues": "https://github.com/FakerPHP/Faker/issues",
"source": "https://github.com/FakerPHP/Faker/tree/v1.24.1"
},
"time": "2024-11-21T13:46:39+00:00"
},
{
"name": "fidry/cpu-core-counter",
"version": "1.3.0",
@ -11664,6 +11896,154 @@
],
"time": "2024-12-29T13:46:23+00:00"
},
{
"name": "justinrainbow/json-schema",
"version": "6.6.0",
"source": {
"type": "git",
"url": "https://github.com/jsonrainbow/json-schema.git",
"reference": "68ba7677532803cc0c5900dd5a4d730537f2b2f3"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/jsonrainbow/json-schema/zipball/68ba7677532803cc0c5900dd5a4d730537f2b2f3",
"reference": "68ba7677532803cc0c5900dd5a4d730537f2b2f3",
"shasum": ""
},
"require": {
"ext-json": "*",
"marc-mabe/php-enum": "^4.0",
"php": "^7.2 || ^8.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "3.3.0",
"json-schema/json-schema-test-suite": "^23.2",
"marc-mabe/php-enum-phpstan": "^2.0",
"phpspec/prophecy": "^1.19",
"phpstan/phpstan": "^1.12",
"phpunit/phpunit": "^8.5"
},
"bin": [
"bin/validate-json"
],
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "6.x-dev"
}
},
"autoload": {
"psr-4": {
"JsonSchema\\": "src/JsonSchema/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Bruno Prieto Reis",
"email": "bruno.p.reis@gmail.com"
},
{
"name": "Justin Rainbow",
"email": "justin.rainbow@gmail.com"
},
{
"name": "Igor Wiedler",
"email": "igor@wiedler.ch"
},
{
"name": "Robert Schönthal",
"email": "seroscho@googlemail.com"
}
],
"description": "A library to validate a json schema.",
"homepage": "https://github.com/jsonrainbow/json-schema",
"keywords": [
"json",
"schema"
],
"support": {
"issues": "https://github.com/jsonrainbow/json-schema/issues",
"source": "https://github.com/jsonrainbow/json-schema/tree/6.6.0"
},
"time": "2025-10-10T11:34:09+00:00"
},
{
"name": "marc-mabe/php-enum",
"version": "v4.7.2",
"source": {
"type": "git",
"url": "https://github.com/marc-mabe/php-enum.git",
"reference": "bb426fcdd65c60fb3638ef741e8782508fda7eef"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/marc-mabe/php-enum/zipball/bb426fcdd65c60fb3638ef741e8782508fda7eef",
"reference": "bb426fcdd65c60fb3638ef741e8782508fda7eef",
"shasum": ""
},
"require": {
"ext-reflection": "*",
"php": "^7.1 | ^8.0"
},
"require-dev": {
"phpbench/phpbench": "^0.16.10 || ^1.0.4",
"phpstan/phpstan": "^1.3.1",
"phpunit/phpunit": "^7.5.20 | ^8.5.22 | ^9.5.11",
"vimeo/psalm": "^4.17.0 | ^5.26.1"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-3.x": "3.2-dev",
"dev-master": "4.7-dev"
}
},
"autoload": {
"psr-4": {
"MabeEnum\\": "src/"
},
"classmap": [
"stubs/Stringable.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Marc Bennewitz",
"email": "dev@mabe.berlin",
"homepage": "https://mabe.berlin/",
"role": "Lead"
}
],
"description": "Simple and fast implementation of enumerations with native PHP",
"homepage": "https://github.com/marc-mabe/php-enum",
"keywords": [
"enum",
"enum-map",
"enum-set",
"enumeration",
"enumerator",
"enummap",
"enumset",
"map",
"set",
"type",
"type-hint",
"typehint"
],
"support": {
"issues": "https://github.com/marc-mabe/php-enum/issues",
"source": "https://github.com/marc-mabe/php-enum/tree/v4.7.2"
},
"time": "2025-09-14T11:18:39+00:00"
},
{
"name": "masterminds/html5",
"version": "2.10.0",
@ -14508,6 +14888,185 @@
}
],
"time": "2024-03-03T12:36:25+00:00"
},
{
"name": "zenstruck/assert",
"version": "v1.5.1",
"source": {
"type": "git",
"url": "https://github.com/zenstruck/assert.git",
"reference": "39554ce3a275fbf8c870b251e620101f644e9277"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/zenstruck/assert/zipball/39554ce3a275fbf8c870b251e620101f644e9277",
"reference": "39554ce3a275fbf8c870b251e620101f644e9277",
"shasum": ""
},
"require": {
"php": ">=8.0",
"symfony/polyfill-php81": "^1.23",
"symfony/var-exporter": "^5.4|^6.0|^7.0"
},
"require-dev": {
"phpstan/phpstan": "^1.4",
"phpunit/phpunit": "^9.5.21",
"symfony/phpunit-bridge": "^6.3|^7.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Zenstruck\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Kevin Bond",
"email": "kevinbond@gmail.com"
}
],
"description": "Standalone, lightweight, framework agnostic, test assertion library.",
"homepage": "https://github.com/zenstruck/assert",
"keywords": [
"assertion",
"phpunit",
"test"
],
"support": {
"issues": "https://github.com/zenstruck/assert/issues",
"source": "https://github.com/zenstruck/assert/tree/v1.5.1"
},
"funding": [
{
"url": "https://github.com/kbond",
"type": "github"
}
],
"time": "2024-10-28T18:08:12+00:00"
},
{
"name": "zenstruck/foundry",
"version": "v2.7.5",
"source": {
"type": "git",
"url": "https://github.com/zenstruck/foundry.git",
"reference": "bbaa207d397386237a6bad988956132450c9c1ca"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/zenstruck/foundry/zipball/bbaa207d397386237a6bad988956132450c9c1ca",
"reference": "bbaa207d397386237a6bad988956132450c9c1ca",
"shasum": ""
},
"require": {
"fakerphp/faker": "^1.23",
"php": ">=8.1",
"symfony/deprecation-contracts": "^2.2|^3.0",
"symfony/polyfill-php84": "^1.32",
"symfony/property-access": "^6.4|^7.0|^8.0",
"symfony/property-info": "^6.4|^7.0|^8.0",
"symfony/var-exporter": "^6.4.9|~7.0.9|^7.1.2|^8.0",
"zenstruck/assert": "^1.4"
},
"conflict": {
"doctrine/persistence": "<2.0",
"symfony/framework-bundle": "<6.4"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.8",
"brianium/paratest": "^6|^7",
"dama/doctrine-test-bundle": "^8.0",
"doctrine/collections": "^1.7|^2.0",
"doctrine/common": "^3.2.2",
"doctrine/doctrine-bundle": "^2.10",
"doctrine/doctrine-migrations-bundle": "^2.2|^3.0",
"doctrine/mongodb-odm": "^2.4",
"doctrine/mongodb-odm-bundle": "^4.6|^5.0",
"doctrine/orm": "^2.16|^3.0",
"doctrine/persistence": "^2.0|^3.0|^4.0",
"phpunit/phpunit": "^9.5.0 || ^10.0 || ^11.0 || ^12.0",
"rector/rector": "^2.2",
"symfony/browser-kit": "^6.4|^7.0|^8.0",
"symfony/console": "^6.4|^7.0|^8.0",
"symfony/dotenv": "^6.4|^7.0|^8.0",
"symfony/framework-bundle": "^6.4|^7.0|^8.0",
"symfony/maker-bundle": "^1.55",
"symfony/phpunit-bridge": "^6.4|^7.0|^8.0",
"symfony/routing": "^6.4|^7.0|^8.0",
"symfony/runtime": "^6.4|^7.0|^8.0",
"symfony/translation-contracts": "^3.4",
"symfony/uid": "^6.4|^7.0|^8.0",
"symfony/var-dumper": "^6.4|^7.0|^8.0",
"symfony/yaml": "^6.4|^7.0|^8.0",
"webmozart/assert": "^1.11"
},
"type": "library",
"extra": {
"psalm": {
"pluginClass": "Zenstruck\\Foundry\\Psalm\\FoundryPlugin"
},
"bamarni-bin": {
"bin-links": true,
"forward-command": false,
"target-directory": "bin/tools"
}
},
"autoload": {
"files": [
"src/functions.php",
"src/Persistence/functions.php",
"src/symfony_console.php"
],
"psr-4": {
"Zenstruck\\Foundry\\": "src/",
"Zenstruck\\Foundry\\Psalm\\": "utils/psalm",
"Zenstruck\\Foundry\\Utils\\Rector\\": "utils/rector/src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Kevin Bond",
"email": "kevinbond@gmail.com"
},
{
"name": "Nicolas PHILIPPE",
"email": "nikophil@gmail.com"
}
],
"description": "A model factory library for creating expressive, auto-completable, on-demand dev/test fixtures with Symfony and Doctrine.",
"homepage": "https://github.com/zenstruck/foundry",
"keywords": [
"Fixture",
"dev",
"doctrine",
"factory",
"faker",
"symfony",
"test"
],
"support": {
"issues": "https://github.com/zenstruck/foundry/issues",
"source": "https://github.com/zenstruck/foundry/tree/v2.7.5"
},
"funding": [
{
"url": "https://github.com/kbond",
"type": "github"
},
{
"url": "https://github.com/nikophil",
"type": "github"
}
],
"time": "2025-10-10T11:41:47+00:00"
}
],
"aliases": [],

View File

@ -19,4 +19,6 @@ return [
KnpU\OAuth2ClientBundle\KnpUOAuth2ClientBundle::class => ['all' => true],
Symfony\WebpackEncoreBundle\WebpackEncoreBundle::class => ['all' => true],
SymfonyCasts\Bundle\VerifyEmail\SymfonyCastsVerifyEmailBundle::class => ['all' => true],
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
Zenstruck\Foundry\ZenstruckFoundryBundle::class => ['dev' => true, 'test' => true],
];

View File

@ -0,0 +1,16 @@
when@dev: &dev
# See full configuration: https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#full-default-bundle-configuration
zenstruck_foundry:
persistence:
# Flush only once per call of `PersistentObjectFactory::create()`
flush_once: true
# If you use the `make:factory --test` command, you may need to uncomment the following.
# See https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#generate
#services:
# App\Tests\Factory\:
# resource: '%kernel.project_dir%/tests/Factory/'
# autowire: true
# autoconfigure: true
when@test: *dev

View File

@ -30,5 +30,6 @@
</source>
<extensions>
<bootstrap class="Zenstruck\Foundry\PHPUnit\FoundryExtension" />
</extensions>
</phpunit>

View File

@ -0,0 +1,15 @@
<?php
namespace App\DataFixtures;
use App\Story\DefaultUsersStory;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
class AppFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
DefaultUsersStory::load();
}
}

View File

@ -80,6 +80,8 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $verifiedAt = null;
private ?string $plainPassword = null;
public function __construct()
{
$this->watchLists = new ArrayCollection();
@ -155,7 +157,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
public function eraseCredentials(): void
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
$this->plainPassword = null;
}
/**
@ -241,4 +243,16 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
return $this;
}
public function getPlainPassword(): ?string
{
return $this->plainPassword;
}
public function setPlainPassword(?string $plainPassword): self
{
$this->plainPassword = $plainPassword;
return $this;
}
}

View File

@ -0,0 +1,64 @@
<?php
namespace App\Factory;
use App\Entity\User;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;
/**
* @extends PersistentObjectFactory<User>
*/
final class UserFactory extends PersistentObjectFactory
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
*
* @todo inject services if required
*/
public function __construct(
private readonly UserPasswordHasherInterface $passwordHasher,
) {
}
#[\Override]
public static function class(): string
{
return User::class;
}
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
*
* @todo add your default values here
*/
#[\Override]
protected function defaults(): array|callable
{
$createdAt = \DateTimeImmutable::createFromMutable(self::faker()->dateTime());
$plainPassword = self::faker()->password(8, 20);
return [
'createdAt' => $createdAt,
'verifiedAt' => $createdAt,
'email' => self::faker()->unique()->safeEmail(),
'plainPassword' => $plainPassword,
'roles' => ['ROLE_USER'],
];
}
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
*/
#[\Override]
protected function initialize(): static
{
return $this->afterInstantiate(function (User $user): void {
if ($user->getPlainPassword()) {
$user->setPassword(
$this->passwordHasher->hashPassword($user, $user->getPlainPassword())
);
}
});
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace App\Story;
use App\Factory\UserFactory;
use Zenstruck\Foundry\Story;
final class DefaultUsersStory extends Story
{
public function build(): void
{
UserFactory::createMany(3);
}
}

View File

@ -36,6 +36,18 @@
"src/Repository/.gitignore"
]
},
"doctrine/doctrine-fixtures-bundle": {
"version": "4.2",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "main",
"version": "3.0",
"ref": "1f5514cfa15b947298df4d771e694e578d4c204d"
},
"files": [
"src/DataFixtures/AppFixtures.php"
]
},
"doctrine/doctrine-migrations-bundle": {
"version": "3.3",
"recipe": {
@ -512,5 +524,18 @@
},
"twig/extra-bundle": {
"version": "v3.10.0"
},
"zenstruck/foundry": {
"version": "2.7",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "main",
"version": "2.7",
"ref": "135a835d9425f916e23f5509b2a48f70b4d986a5"
},
"files": [
"config/packages/zenstruck_foundry.yaml",
"src/Story/AppStory.php"
]
}
}

37
tests/AbstractTest.php Normal file
View File

@ -0,0 +1,37 @@
<?php
namespace App\Tests;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Symfony\Bundle\Test\Client;
use App\Entity\User;
abstract class AbstractTest extends ApiTestCase
{
private ?string $token = null;
public function setUp(): void
{
self::bootKernel();
}
protected function createClientWithCredentials($token = null): Client
{
return static::createClient([], ['headers' => ['authorization' => 'Bearer '.$token]]);
}
protected function getToken(User $testUser): string
{
if ($this->token) {
return $this->token;
}
$response = static::createClient()->request('POST', '/api/login', ['json' => ['email' => $testUser->getEmail(), 'password' => $testUser->getPlainPassword()]]);
$this->assertResponseIsSuccessful();
$data = $response->toArray();
$this->token = $data['token'];
return $data['token'];
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace App\Tests\Entity;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\Entity\Instance;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Zenstruck\Foundry\Test\Factories;
final class InstanceTest extends ApiTestCase
{
use Factories;
/**
* @throws TransportExceptionInterface
*/
public function testInstance(): void
{
$client = InstanceTest::createClient();
$client->request('GET', '/api/config');
$this->assertResponseIsSuccessful();
$this->assertMatchesResourceItemJsonSchema(Instance::class);
}
}

View File

@ -0,0 +1,76 @@
<?php
namespace App\Tests\Service;
use App\Entity\RdapServer;
use App\Exception\DomainNotFoundException;
use App\Exception\TldNotSupportedException;
use App\Exception\UnknownRdapServerException;
use App\Message\UpdateRdapServers;
use App\MessageHandler\UpdateRdapServersHandler;
use App\Service\RDAPService;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\Attributes\Depends;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpClient\Exception\ClientException;
use Symfony\Component\HttpClient\Exception\TransportException;
class RDAPServiceTest extends KernelTestCase
{
protected static ContainerInterface $container;
protected static EntityManagerInterface $entityManager;
protected static RDAPService $RDAPService;
protected function setUp(): void
{
static::$container = static::getContainer();
static::$entityManager = static::$container->get(EntityManagerInterface::class);
static::$RDAPService = static::$container->get(RDAPService::class);
}
public function testUpdateRdapServers(): void
{
$updateRdapServersHandler = self::$container->get(UpdateRdapServersHandler::class);
$message = new UpdateRdapServers();
$updateRdapServersHandler($message);
self::$entityManager->flush();
$rdapServerRepository = self::$entityManager->getRepository(RdapServer::class);
$this->assertNotEmpty($rdapServerRepository->findAll());
}
#[Depends('testUpdateRdapServers')]
public function testRegisterDomain(): void
{
$rdapServerRepository = self::$entityManager->getRepository(RdapServer::class);
$testedTldList = ['com', 'net', 'org', 'fr', 'de', 'ch', 'ca', 'leclerc', 'uz'];
/** @var RdapServer $rdapServer */
foreach ($rdapServerRepository->findAll() as $rdapServer) {
if (!in_array($rdapServer->getTld()->getTld(), $testedTldList)) {
continue;
}
try {
self::$RDAPService->registerDomain('nic.'.$rdapServer->getTld()->getTld());
} catch (DomainNotFoundException|ClientException|TransportException) {
}
}
$this->expectNotToPerformAssertions();
}
#[Depends('testUpdateRdapServers')]
public function testUnknownRdapServer(): void
{
$this->expectException(UnknownRdapServerException::class);
self::$RDAPService->registerDomain('nic.arpa');
}
#[Depends('testUpdateRdapServers')]
public function testUnknownTld(): void
{
$this->expectException(TldNotSupportedException::class);
self::$RDAPService->registerDomain('nic.noexist');
}
}

30
tests/UserTest.php Normal file
View File

@ -0,0 +1,30 @@
<?php
namespace App\Tests;
use App\Entity\User;
use App\Factory\UserFactory;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;
final class UserTest extends AbstractTest
{
use ResetDatabase;
use Factories;
/**
* @throws TransportExceptionInterface
*/
public function testGetMyProfile(): void
{
$testUser = UserFactory::createOne();
$client = UserTest::createClientWithCredentials(UserTest::getToken($testUser));
$client->loginUser($testUser);
$client->request('GET', '/api/me');
$this->assertResponseIsSuccessful();
$this->assertMatchesResourceItemJsonSchema(User::class);
}
}