symfony-docs icon indicating copy to clipboard operation
symfony-docs copied to clipboard

Multiple clients in user test is deprecated, still present in documentation

Open Voetloos opened this issue 5 years ago • 15 comments
trafficstars

Symfony version(s) affected: 4.4.0

Description
After upgrading to Symfony 4.4.0 I got the following deprecation warning: Calling "Symfony\Bundle\FrameworkBundle\Test\WebTestCase::createClient()" while a kernel has been booted is deprecated since Symfony 4.4 and will throw in 5.0, ensure the kernel is shut down before calling the method.

However, I do the same as in the documentation (https://symfony.com/doc/current/testing/insulating_clients.html), this also gives the same deprecation warning. There is no clear alternative, is the documentation outdated or is this a bug?

How to reproduce

class PagesTestCase extends WebTestCase {
     ...
     public function setUp(): void {
        parent::setUp();
        ...
        // Create clients
        self::$anonymousClient = self::createClient(array(), array(
            'HTTPS' => true,
        ));
        self::$userClient = self::createClient(array(), array(
            'PHP_AUTH_USER' => self::get('anonymous')->getUsername(),
            'PHP_AUTH_PW' => '***',
            'HTTPS' => true,
        ));
        self::$adminClient = self::createClient(array(), array(
            'PHP_AUTH_USER' => self::get('testuser')->getUsername(),
            'PHP_AUTH_PW' => '***',
            'HTTPS' => true,
        ));

Voetloos avatar Jan 09 '20 22:01 Voetloos

You need to shutdown the kernel. WebTestCase extends KernelTestCase, which provides a static method self::ensureKernelShutdown().

Call this before creating the clients.

This is indeed still missing from the documentation.

From stack

Voetloos avatar Jan 19 '20 16:01 Voetloos

@Voetloos FYI, I have moved the issue to the documentation repository as it seems that the changes need to be done there.

xabbuh avatar Jan 20 '20 10:01 xabbuh

ok fine, i want to start transacion before each test and then rollback on tearDown, it also described in Symfony docs - "use DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension }" but this extension loads kernel of couse. So currently it is impossible to use transactions and use client to make tests, have to write own Guzzle client, but it is not good because requires real http server which could be hard in CI testing

errogaht avatar Jun 02 '20 06:06 errogaht

Doesn't it make sense to reuse kernel instead of shutdown?

bravik avatar Jun 08 '20 03:06 bravik

Client (KernelBrowser) can be created only when there is no Kernel(deprecation notice) ORM transaction can be done only when Kernel is loaded so it is incompatible requirements and impossible to use built in Client for REST API testing with resetting DB state each time(In tearDown method) with transaction. I can’t believe that is Symfony 5 has no good way to test API...

пн, 8 июня 2020 г. в 06:11, bravik [email protected]:

Doesn't it make sense to reuse kernel instead of shutdown?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/symfony/symfony-docs/issues/12961#issuecomment-640337689, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABQIAKKQ5P3WNIQXACAHVBLRVRJFDANCNFSM4KJBNKKQ .

errogaht avatar Jun 08 '20 03:06 errogaht

You need to shutdown the kernel. WebTestCase extends KernelTestCase, which provides a static method self::ensureKernelShutdown().

Call this before creating the clients.

This is indeed still missing from the documentation.

From stack

So, you cannot use several clients at the same time ? Creating one shuts down the other's kernel, making it unusable.

theredled avatar Mar 16 '21 13:03 theredled

Thank you for this issue. There has not been a lot of activity here for a while. Has this been resolved?

carsonbot avatar Mar 17 '22 12:03 carsonbot

@carsonbot This is still an issue

broeser avatar Mar 20 '22 12:03 broeser

Thank you for this issue. There has not been a lot of activity here for a while. Has this been resolved?

carsonbot avatar May 03 '23 12:05 carsonbot

@carsonbot This is still an issue

JornJorn avatar May 03 '23 18:05 JornJorn

Symfony 4.4 is EOM, so lets close here

OskarStark avatar May 04 '23 05:05 OskarStark

Symfony 4.4 may be EOM. But the issue still exists in Symfony 7. https://symfony.com/doc/current/testing/insulating_clients.html just doesn't work.

jdelaune avatar Apr 19 '24 15:04 jdelaune

@OskarStark why close if the docs are still wrong?

burtek avatar Feb 19 '25 23:02 burtek

Can you please send a PR?

OskarStark avatar Feb 20 '25 04:02 OskarStark

#13351 has been open forever

burtek avatar Feb 20 '25 09:02 burtek