Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5d2f196

Browse files
author
Amrouche Hamza
committed
[BrowserKit] add a way to switch to ajax for one request
1 parent a522e04 commit 5d2f196

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/Symfony/Component/BrowserKit/Client.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ public function getServerParameter($key, $default = '')
150150
return isset($this->server[$key]) ? $this->server[$key] : $default;
151151
}
152152

153+
public function switchToXMLHttpRequest()
154+
{
155+
$this->setServerParameter('HTTP_X-Requested-With', 'XMLHttpRequest');
156+
}
157+
158+
public function removeXMLHttpRequest()
159+
{
160+
unset($this->server['HTTP_X-Requested-With']);
161+
}
162+
153163
/**
154164
* Returns the History instance.
155165
*
@@ -249,14 +259,15 @@ public function click(Link $link)
249259
*
250260
* @param Form $form A Form instance
251261
* @param array $values An array of form field values
262+
* @param bool $isAjax Whether it's ajax or not
252263
*
253264
* @return Crawler
254265
*/
255-
public function submit(Form $form, array $values = array())
266+
public function submit(Form $form, array $values = array(), $isAjax = false)
256267
{
257268
$form->setValues($values);
258269

259-
return $this->request($form->getMethod(), $form->getUri(), $form->getPhpValues(), $form->getPhpFiles());
270+
return $this->request($form->getMethod(), $form->getUri(), $form->getPhpValues(), $form->getPhpFiles(), array(), null, true, $isAjax);
260271
}
261272

262273
/**
@@ -269,17 +280,22 @@ public function submit(Form $form, array $values = array())
269280
* @param array $server The server parameters (HTTP headers are referenced with a HTTP_ prefix as PHP does)
270281
* @param string $content The raw body data
271282
* @param bool $changeHistory Whether to update the history or not (only used internally for back(), forward(), and reload())
283+
* @param bool $isAjax Whether it's a ajax request or not
272284
*
273285
* @return Crawler
274286
*/
275-
public function request($method, $uri, array $parameters = array(), array $files = array(), array $server = array(), $content = null, $changeHistory = true)
287+
public function request($method, $uri, array $parameters = array(), array $files = array(), array $server = array(), $content = null, $changeHistory = true, $isAjax = false)
276288
{
277289
if ($this->isMainRequest) {
278290
$this->redirectCount = 0;
279291
} else {
280292
++$this->redirectCount;
281293
}
282294

295+
if (true === $isAjax) {
296+
$this->switchToXMLHttpRequest();
297+
}
298+
283299
$uri = $this->getAbsoluteUri($uri);
284300

285301
$server = array_merge($this->server, $server);
@@ -324,6 +340,10 @@ public function request($method, $uri, array $parameters = array(), array $files
324340
$this->redirect = null;
325341
}
326342

343+
if (true === $isAjax) {
344+
$this->removeXMLHttpRequest();
345+
}
346+
327347
if ($this->followRedirects && $this->redirect) {
328348
$this->redirects[serialize($this->history->current())] = true;
329349

@@ -354,7 +374,7 @@ protected function doRequestInProcess($request)
354374
unlink($deprecationsFile);
355375
foreach ($deprecations ? unserialize($deprecations) : array() as $deprecation) {
356376
if ($deprecation[0]) {
357-
trigger_error($deprecation[1], E_USER_DEPRECATED);
377+
@trigger_error($deprecation[1], E_USER_DEPRECATED);
358378
} else {
359379
@trigger_error($deprecation[1], E_USER_DEPRECATED);
360380
}

src/Symfony/Component/BrowserKit/Tests/ClientTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ public function testGetRequest()
9494
$this->assertEquals('http://example.com/', $client->getRequest()->getUri(), '->getCrawler() returns the Request of the last request');
9595
}
9696

97+
public function testGetRequestWithAjax()
98+
{
99+
$client = new TestClient();
100+
$client->request('GET', 'http://example.com/', array(), array(), array(), null, true, true);
101+
$this->assertEquals($client->getRequest()->getServer()['HTTP_X-Requested-With'], 'XMLHttpRequest');
102+
$this->assertEquals('http://example.com/', $client->getRequest()->getUri(), '->getCrawler() returns the Request of the last request');
103+
}
104+
97105
public function testGetRequestWithIpAsHttpHost()
98106
{
99107
$client = new TestClient();

0 commit comments

Comments
 (0)