Skip to content

[FIXED] Create multiple users for functional testing #35031

Closed
@juanwilde

Description

@juanwilde

Before upgrading to 5.0.99 I used to create multiple users to test my API with a TestBase like this:

<?php

declare(strict_types=1);

namespace App\Tests\Functional;

use Doctrine\DBAL\Connection;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\HttpFoundation\Response;

class TestBase extends WebTestCase
{
    protected const FORMAT = 'jsonld';

    protected const IDS = [
        'admin_id' => 'de3c30fa-a987-4e83-a333-73dde50928f1',
        'user_id' => 'de3c30fa-a987-4e83-a333-73dde50928f2',
    ];

    protected Connection $connection;

    protected static ?Application $application = null;

    protected static ?KernelBrowser $admin = null;

    protected static ?KernelBrowser $user = null;

    /**
     * @throws \Exception
     */
    public function setUp(): void
    {
        parent::setUp();

        self::runCommand('doctrine:fixtures:load --no-interaction');

        if (null === self::$admin) {
            self::$admin = $this->createAuthenticatedClient('admin@api.com', 'password');
        }

        if (null === self::$user) {
            self::$admin = $this->createAuthenticatedClient('user@api.com', 'password');
        }
    }

    public function tearDown(): void
    {
        parent::tearDown();

        self::$application = null;
    }

    protected function createAuthenticatedClient(string $username, string $password): KernelBrowser
    {
        $client = static::createClient();
        $client->request(
            'POST',
            '/api/v1/login_check',
            [
                '_email' => $username,
                '_password' => $password,
            ]
        );

        $data = \json_decode($client->getResponse()->getContent(), true);

        $client = static::createClient();
        $client->setServerParameter('HTTP_Authorization', \sprintf('Bearer %s', $data['token']));
        $client->setServerParameter('CONTENT_TYPE', 'application/json');

        return $client;
    }

    /**
     * @throws \Exception
     */
    protected static function runCommand(string $command): int
    {
        $command = \sprintf('%s --quiet', $command);

        return self::getApplication()->run(new StringInput($command));
    }

    protected static function getApplication(): Application
    {
        if (null === self::$application) {
            $client = static::createClient();

            self::$application = new Application($client->getKernel());
            self::$application->setAutoExit(false);
        }

        return self::$application;
    }

    /**
     * @return mixed[]
     */
    protected function getResponseData(Response $response): array
    {
        return \json_decode($response->getContent(), true);
    }

    protected function initDbConnection(): void
    {
        $kernel = static::bootKernel();

        $this->connection = $kernel->getContainer()->get('doctrine')->getConnection();
    }
}

The problem is that now I get a

Booting the kernel before calling Symfony\Bundle\FrameworkBundle\Test\WebTestCase::createClient() is not supported, the kernel should only be booted once.

Following the documentation I see I can use static::createClient() to have new users, but I always get this message.

Any tip will be appreciated

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy