Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests failing after update to API Platform 4.1 #6991

Open
jonag opened this issue Feb 28, 2025 · 0 comments
Open

Tests failing after update to API Platform 4.1 #6991

jonag opened this issue Feb 28, 2025 · 0 comments

Comments

@jonag
Copy link
Contributor

jonag commented Feb 28, 2025

API Platform version(s) affected: 4.1.0

Description
Hello,

When trying to migrate my project to API Platform 4.1, I'm encountering issues with my test suite.

In my tests, I automatically load my fixtures using the RefreshDatabaseTrait and I have an AbstractApiTestCase class that all my functional tests inherit from:

<?php

namespace App\Tests\Functional;

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Symfony\Bundle\Test\Client;

class AbstractApiTestCase extends ApiTestCase
{
    public function setUp(): void
    {
        self::bootKernel();
    }

    protected static function createClient(array $kernelOptions = [], array $defaultOptions = []): Client
    {
        return parent::createClient(
            $kernelOptions,
            [
                'headers' => [
                    'accept' => 'application/ld+json',
                    'content-type' => 'application/ld+json',
                ],
            ] + $defaultOptions,
        );
    }
}

Since the migration, all my tests are failing because my fixtures are no longer loaded automatically.

I noticed that if I modify the setUp method in my AbstractApiTestCase class to:

public function setUp(): void
{
    static::bootKernel();
}

then the fixtures seem to load, but some of my tests are still failing. The failing tests appear to be those that make two requests in the same test, like this one:

public function testInvalidPayload(): void
{
    $client = self::createClient();
    $user = static::getContainer()->get('doctrine')->getRepository(User::class)->findOneBy(['email' => '[email protected]']);
    $client->loginUser($user);

    $client->request('POST', '/users', [
        'json' => [
            'email' => 'new_user',
            'firstName' => '',
            'lastName' => '',
            'role' => Role::USER->value,
        ],
    ]);

    self::assertResponseStatusCodeSame(422);
    self::assertJsonContains([
        'violations' => [
            ['propertyPath' => 'email', 'message' => 'This value is not a valid email address.'],
            ['propertyPath' => 'firstName', 'message' => 'This value should not be blank.'],
            ['propertyPath' => 'lastName', 'message' => 'This value should not be blank.'],
        ],
    ]);

    $client = self::createClient();
    $client->loginUser($user);
    $client->request('POST', '/users', [
        'json' => [
            'firstName' => 'John',
            'lastName' => 'Doe',
            'role' => Role::USER->value,
            'email' => '',
        ],
    ]);

    self::assertResponseStatusCodeSame(422);
    self::assertJsonContains([
        'violations' => [
            ['propertyPath' => 'email', 'message' => 'This value should not be blank.'],
        ],
    ]);
}

The second request fails with the following error:

{"code":401,"message":"JWT Token not found"}

I believe these changes are caused by this PR because if I revert all its changes, my tests pass correctly again.

Unfortunately, I cannot find what I can modify in my tests to fix this second issue (and I'm not sure if my first fix is the correct one).

Please tell me if there is anything I can do to help find the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant