Skip to content

Adapt documentation for multiple clients on WebTestCases #13351

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

Open
wants to merge 1 commit into
base: 4.4
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 38 additions & 17 deletions testing/insulating_clients.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,56 @@ If you need to simulate an interaction between different clients (think of a
chat for instance), create several clients::

// ...
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;

$harry = static::createClient();
$sally = static::createClient();
class MyTest extends WebTestCase
{
// ...
public function testWithMultipleClients()
{
self::ensureKernelShutdown();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This first shutdown is not really needed, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part of the documentation is for multiple clients, I thought it would be better to have some kind of patter and add the call prior to all client creations (which is not present on the examples of a single client) to show the main difference and easy the understanding of the mechanism.

But I am happy with it if you want me to remove it and/or add a comment about the need to add it only if there are new clients being create.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I opened #34507 a long time ago and proposed this solution on the docs. To be honest, feels weird to ask people to shut down the kernel before actually creating any client. And when you call createClient() it is already shutting down any previous kernel on the bootKernel() method.

I'll leave it up to the maintainers!

$harry = static::createClient();

$harry->request('POST', '/say/sally/Hello');
$sally->request('GET', '/messages');
self::ensureKernelShutdown();
$sally = static::createClient();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of the second client? It's using the same parameters, same kernel etc as the one before. So you could just use the same client for both the following requests or not?

$harry = static::createClient();
self::ensureKernelShutdown();
$sally = static::createClient();

This also seems pretty strange. How does $harry client still work when the kernel is shutdown?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this "works" because https://github.com/symfony/symfony/blob/829566cdea90a0be0231a15a229df2166868fce0/src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php#L155
boots the kernel again. So harry starts a client and boots the kernel, then shutdown the kernel, then boot it again...


$this->assertEquals(Response::HTTP_CREATED, $harry->getResponse()->getStatusCode());
$this->assertRegExp('/Hello/', $sally->getResponse()->getContent());
$harry->request('POST', '/say/sally/Hello');
$sally->request('GET', '/messages');

$this->assertEquals(Response::HTTP_CREATED, $harry->getResponse()->getStatusCode());
$this->assertRegExp('/Hello/', $sally->getResponse()->getContent());
}
}

This works except when your code maintains a global state or if it depends on
a third-party library that has some kind of global state. In such a case, you
can insulate your clients::

// ...
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;

$harry = static::createClient();
$sally = static::createClient();

$harry->insulate();
$sally->insulate();

$harry->request('POST', '/say/sally/Hello');
$sally->request('GET', '/messages');

$this->assertEquals(Response::HTTP_CREATED, $harry->getResponse()->getStatusCode());
$this->assertRegExp('/Hello/', $sally->getResponse()->getContent());
class MyTest extends WebTestCase
{
// ...
public function testWithMultipleInsulatedClients()
{
self::ensureKernelShutdown();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as my previous answer.

$harry = static::createClient();
$harry->insulate();

self::ensureKernelShutdown();
$sally = static::createClient();
$sally->insulate();

$harry->request('POST', '/say/sally/Hello');
$sally->request('GET', '/messages');

$this->assertEquals(Response::HTTP_CREATED, $harry->getResponse()->getStatusCode());
$this->assertRegExp('/Hello/', $sally->getResponse()->getContent());
}
}

Insulated clients transparently execute their requests in a dedicated and
clean PHP process, thus avoiding any side effects.
Expand Down