diff --git a/composer.json b/composer.json index 81c237e..c7893b6 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,8 @@ "symfony/phpunit-bridge": "^5.2", "symfony/stopwatch": "^5.2", "symfony/var-dumper": "5.2.*", - "symfony/web-profiler-bundle": "^5.2" + "symfony/web-profiler-bundle": "^5.2", + "zenstruck/foundry": "^1.10" }, "config": { "optimize-autoloader": true, diff --git a/composer.lock b/composer.lock index 78f97a2..fc60645 100644 --- a/composer.lock +++ b/composer.lock @@ -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": "644438120a14b9a8ee514089115afbd4", + "content-hash": "4259024408ba2a58367291a68a992edb", "packages": [ { "name": "api-platform/core", @@ -6758,6 +6758,67 @@ ], "time": "2020-11-14T09:36:49+00:00" }, + { + "name": "fakerphp/faker", + "version": "v1.14.1", + "source": { + "type": "git", + "url": "https://github.com/FakerPHP/Faker.git", + "reference": "ed22aee8d17c7b396f74a58b1e7fefa4f90d5ef1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/FakerPHP/Faker/zipball/ed22aee8d17c7b396f74a58b1e7fefa4f90d5ef1", + "reference": "ed22aee8d17c7b396f74a58b1e7fefa4f90d5ef1", + "shasum": "" + }, + "require": { + "php": "^7.1 || ^8.0", + "psr/container": "^1.0", + "symfony/deprecation-contracts": "^2.2" + }, + "conflict": { + "fzaninotto/faker": "*" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.4.1", + "ext-intl": "*", + "symfony/phpunit-bridge": "^4.4 || ^5.2" + }, + "suggest": { + "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", + "extra": { + "branch-alias": { + "dev-main": "v1.15-dev" + } + }, + "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" + ], + "time": "2021-03-30T06:27:33+00:00" + }, { "name": "guzzlehttp/guzzle", "version": "7.2.0", @@ -8203,6 +8264,122 @@ } ], "time": "2020-07-12T23:59:07+00:00" + }, + { + "name": "zenstruck/callback", + "version": "v1.2.0", + "source": { + "type": "git", + "url": "https://github.com/zenstruck/callback.git", + "reference": "b759cfc784da44c6ffdd19671025597428cf9435" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/zenstruck/callback/zipball/b759cfc784da44c6ffdd19671025597428cf9435", + "reference": "b759cfc784da44c6ffdd19671025597428cf9435", + "shasum": "" + }, + "require": { + "php": ">=7.2.5" + }, + "require-dev": { + "phpunit/phpunit": "^8.5.0", + "symfony/phpunit-bridge": "^5.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "Zenstruck\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Kevin Bond", + "email": "kevinbond@gmail.com" + } + ], + "description": "Callable wrapper to validate and inject arguments.", + "homepage": "https://github.com/zenstruck/callback", + "keywords": [ + "callable", + "callback", + "utility" + ], + "time": "2021-01-11T21:34:02+00:00" + }, + { + "name": "zenstruck/foundry", + "version": "v1.10.0", + "source": { + "type": "git", + "url": "https://github.com/zenstruck/foundry.git", + "reference": "7dc49f0e6247db66b7d8215d04b5d3b9e4455734" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/zenstruck/foundry/zipball/7dc49f0e6247db66b7d8215d04b5d3b9e4455734", + "reference": "7dc49f0e6247db66b7d8215d04b5d3b9e4455734", + "shasum": "" + }, + "require": { + "doctrine/persistence": "^1.3.3|^2.0", + "fakerphp/faker": "^1.5", + "php": ">=7.2.12", + "symfony/deprecation-contracts": "^2.2", + "symfony/property-access": "^3.4|^4.4|^5.0", + "zenstruck/callback": "^1.1" + }, + "require-dev": { + "dama/doctrine-test-bundle": "^6.0", + "doctrine/doctrine-bundle": "^2.0", + "doctrine/doctrine-migrations-bundle": "^2.2|^3.0", + "doctrine/orm": "^2.7", + "matthiasnoback/symfony-dependency-injection-test": "^4.1", + "psalm/plugin-symfony": "^1.5|^2.0", + "symfony/framework-bundle": "^4.4|^5.0", + "symfony/maker-bundle": "^1.30", + "symfony/phpunit-bridge": "^5.2", + "vimeo/psalm": "^3.18|^4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Zenstruck\\Foundry\\": "src/" + }, + "files": [ + "src/functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Kevin Bond", + "email": "kevinbond@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", + "doctrine", + "factory", + "faker", + "symfony", + "test" + ], + "time": "2021-04-19T20:28:09+00:00" } ], "aliases": [], diff --git a/config/bundles.php b/config/bundles.php index 55565ea..91e385b 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -12,4 +12,5 @@ Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true], Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true], Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true], + Zenstruck\Foundry\ZenstruckFoundryBundle::class => ['dev' => true, 'test' => true], ]; diff --git a/config/packages/dev/zenstruck_foundry.yaml b/config/packages/dev/zenstruck_foundry.yaml new file mode 100644 index 0000000..e353187 --- /dev/null +++ b/config/packages/dev/zenstruck_foundry.yaml @@ -0,0 +1,4 @@ +# See full configuration: https://github.com/zenstruck/foundry#full-default-bundle-configuration +zenstruck_foundry: + # Whether to auto-refresh proxies by default (https://github.com/zenstruck/foundry#auto-refresh) + auto_refresh_proxies: true diff --git a/config/packages/test/zenstruck_foundry.yaml b/config/packages/test/zenstruck_foundry.yaml new file mode 100644 index 0000000..3bf6547 --- /dev/null +++ b/config/packages/test/zenstruck_foundry.yaml @@ -0,0 +1,5 @@ +# Unless you want different configuration for test/dev environments, +# add configuration to config/packages/dev/zenstruck_foundry.yml +# and this will be synced to your test environment. +imports: + - { resource: ../dev/zenstruck_foundry.yaml } diff --git a/src/Test/CustomApiTestCase.php b/src/Test/CustomApiTestCase.php index 6b8e059..26fe079 100644 --- a/src/Test/CustomApiTestCase.php +++ b/src/Test/CustomApiTestCase.php @@ -18,12 +18,17 @@ class CustomApiTestCase extends ApiTestCase const RESOURCE_NOT_FOUND_404 = 404; const UNAUTHORIZED_401 = 401; + public function setUp(): void + { + self::bootKernel(); + } + protected function getEntityManager(): EntityManagerInterface { return self::$container->get('doctrine')->getManager(); } - protected function getPasswordEncoder() + public function getPasswordEncoder() { return self::$container->get('security.user_password_encoder.generic'); } diff --git a/symfony.lock b/symfony.lock index 6ca46ab..1a47e03 100644 --- a/symfony.lock +++ b/symfony.lock @@ -113,6 +113,9 @@ "egulias/email-validator": { "version": "2.1.24" }, + "fakerphp/faker": { + "version": "v1.14.1" + }, "fig/link-util": { "version": "1.1.1" }, @@ -532,5 +535,21 @@ }, "willdurand/negotiation": { "version": "v2.3.1" + }, + "zenstruck/callback": { + "version": "v1.2.0" + }, + "zenstruck/foundry": { + "version": "1.9", + "recipe": { + "repo": "github.com/symfony/recipes", + "branch": "master", + "version": "1.9", + "ref": "e03ffeeb52fd76e99f2c527ecddc9c986cb194e5" + }, + "files": [ + "./config/packages/dev/zenstruck_foundry.yaml", + "./config/packages/test/zenstruck_foundry.yaml" + ] } } diff --git a/tests/Factory/UserFactory.php b/tests/Factory/UserFactory.php new file mode 100644 index 0000000..a118b53 --- /dev/null +++ b/tests/Factory/UserFactory.php @@ -0,0 +1,61 @@ + faker()->email, + 'password' => faker()->password, + 'roles' => ['ROLE_USER'], + 'apiToken' => uniqid(), + 'enabled' => true, + ]; + } + + protected function initialize(): self + { + // see https://github.com/zenstruck/foundry#initialization + return $this + // ->afterInstantiate(function(User $user) {}) + ; + } + + protected static function getClass(): string + { + return User::class; + } +} diff --git a/tests/Functional/Activity/ActivityCreateTest.php b/tests/Functional/Activity/ActivityCreateTest.php index fe0dcad..8a40bce 100644 --- a/tests/Functional/Activity/ActivityCreateTest.php +++ b/tests/Functional/Activity/ActivityCreateTest.php @@ -2,30 +2,47 @@ namespace App\Tests\Functional\Activity; +use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client; +use App\Entity\User; use App\Test\CustomApiTestCase; +use App\Tests\Factory\UserFactory; use DateTime; +use Zenstruck\Foundry\Proxy; class ActivityCreateTest extends CustomApiTestCase { + private Client $client; + /** + * @var User|Proxy + */ + private Proxy $user; + + public function setUp(): void + { + parent::setUp(); + $this->user = UserFactory::createOne(); + $this->client = self::createClient(); + $this->client->setDefaultOptions([ + 'headers' => [ + 'X-AUTH-TOKEN' => $this->user->getApiToken() + ] + ]); + } + /** * @dataProvider activityDataSets */ public function testCreateActivity(float $performendTime, string $description, string $activityDate) { - $client = self::createClient(); - $user = $this->createUser(); $d = new DateTime(); - $client->request('POST', '/api/activities', [ + $this->client->request('POST', '/api/activities', [ 'json' => [ 'activityDate' => $activityDate, 'performendTime' => $performendTime, 'description' => $description, - 'user' => '/api/users/' . $user->getId() + 'user' => '/api/users/' . $this->user->getId() ], - 'headers' => [ - 'X-AUTH-TOKEN' => $user->getApiToken() - ] ]); $this->assertResponseHeaderSame('Content-Type', 'application/ld+json; charset=utf-8');