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

Randomly "Unable to find the controller for path '/api/auth'" in Tests #1237

Open
4d4ch4u32 opened this issue Sep 24, 2024 · 0 comments
Open

Comments

@4d4ch4u32
Copy link

4d4ch4u32 commented Sep 24, 2024

Hello,

I'm using the bundle in a API platform project for auth. In my API tests, I use the configured login endpoint /api/tests.

Bundle configuration:

lexik_jwt_authentication:
  secret_key: '%env(resolve:JWT_SECRET_KEY)%'
  public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
  pass_phrase: '%env(JWT_PASSPHRASE)%'
  api_platform:
    check_path: /api/auth
    username_path: email
    password_path: password

The URL is also defined in my routes.yaml:

api_auth:
  path: /api/auth
  methods: ['POST']

This is my test class with 3 test cases:

<?php

declare(strict_types=1);

namespace App\Tests\IntegrationTests\Api\XY;

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Symfony\Bundle\Test\Client;
use Spatie\Snapshots\MatchesSnapshots;

class CanConnectTest extends ApiTestCase
{
    use MatchesSnapshots;

    private Client $client;

    private string $token;

    protected function setUp(): void
    {
        $client = self::createClient();

        $response1 = $client->request('POST', '/api/auth', [
            'headers' => [
                'Content-Type' => 'application/json',
            ],
            'json' => [
                'email' => '[email protected]',
                'password' => 'apiuser',
            ],
        ]);
        $json = $response1->toArray();
        self::assertResponseIsSuccessful();
        self::assertArrayHasKey('token', $json);

        $this->token = $json['token'];
        $this->client = $client;
    }

    public function testCanConnectTrue(): void
    {
        $response = $this->client->request(
            'GET',
            'http://localhost/api/audiosus/can_connect/store/7',
            [
                'auth_bearer' => $this->token,
                'headers' => [
                    'Content-Type' => 'application/json',
                    'Accept' => 'application/json',
                ],
            ],
        );

        self::assertResponseIsSuccessful();
        self::assertMatchesJsonSnapshot($response->toArray());
    }

    public function testCanConnectFalse(): void
    {
        $response = $this->client->request(
            'GET',
            'http://localhost/api/audiosus/can_connect/store/6',
            [
                'auth_bearer' => $this->token,
                'headers' => [
                    'Content-Type' => 'application/json',
                    'Accept' => 'application/json',
                ],
            ],
        );

        self::assertResponseIsSuccessful();
        self::assertMatchesJsonSnapshot($response->toArray());
    }

    public function testCanConnectNotFound(): void
    {
        $this->client->request(
            'GET',
            'http://localhost/api/audiosus/can_connect/store/12312313213',
            [
                'auth_bearer' => $this->token,
                'headers' => [
                    'Content-Type' => 'application/json',
                    'Accept' => 'application/json',
                ],
            ],
        );

        self::assertResponseStatusCodeSame(404);
    }
}

When I run the tests, I get randomly errors like this:

1) App\Tests\IntegrationTests\Api\Audiosus\CanConnectTest::testCanConnectFalse
Symfony\Component\HttpClient\Exception\ClientException: An error occurred

Unable to find the controller for path "/api/auth". The route is wrongly configured.

/srv/app/vendor/api-platform/core/src/Symfony/Bundle/Test/Response.php:80
/srv/app/vendor/api-platform/core/src/Symfony/Bundle/Test/Response.php:94
/srv/app/vendor/api-platform/core/src/Symfony/Bundle/Test/Response.php:125
/srv/app/tests/IntegrationTests/Api/XY/CanConnectTest.php:33

2) App\Tests\IntegrationTests\Api\XY\CanConnectTest::testCanConnectTrue
Symfony\Component\HttpClient\Exception\ClientException: An error occurred

Unable to find the controller for path "/api/auth". The route is wrongly configured.

/srv/app/vendor/api-platform/core/src/Symfony/Bundle/Test/Response.php:80
/srv/app/vendor/api-platform/core/src/Symfony/Bundle/Test/Response.php:94
/srv/app/vendor/api-platform/core/src/Symfony/Bundle/Test/Response.php:125
/srv/app/tests/IntegrationTests/Api/XY/CanConnectTest.php:33

or sometimes all the test cases are green, or only one test fails.

Any ideas?

@4d4ch4u32 4d4ch4u32 changed the title Randomized "Unable to find the controller for path '/api/auth'" in Tests Randomly "Unable to find the controller for path '/api/auth'" in Tests Sep 24, 2024
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