-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
base: 4.4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
$harry = static::createClient(); | ||
|
||
$harry->request('POST', '/say/sally/Hello'); | ||
$sally->request('GET', '/messages'); | ||
self::ensureKernelShutdown(); | ||
$sally = static::createClient(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
This also seems pretty strange. How does There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
$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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 thebootKernel()
method.I'll leave it up to the maintainers!