Jan
15

Unit Testing your AJAX requests with Zend Framework and PHPunit

I ran into a problem yesterday, when trying to create a couple of unit tests using PHPunit along side the Zend Framework.

If you want to unit test an action in a controller that checks the request to see if is an XML HTTP Request using the isXmlHttpRequest() method provided by the Zend Framework, then you will definitely run into a problem.

isXmlHttpRequest() pretty much checks to see if the $_SERVER[‘HTTP_X_REQUESTED_WITH’] is set with ‘XMLHttpRequest’, if not, then returns FALSE. I thought that by setting the server variable with XMLHttpRequest directly will allow me to bypass the check.

function myAjaxTestAction()
{
      $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';

      //then do your ajax tests here
}

This solution seemed like it was going to work, but it turns out that isXmlHttpRequest() makes a call to getHeader() like this:

public function isXmlHttpRequest()
{
        return ($this->getHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
}

What I didn’t know is that when you are doing unit tests the framework extends the HTTP class and changes the getHeader() to only use headers set using the setHeader or setHeaders methods.
After a couple of minutes tracing over the calls being made. I discovered that the fix was pretty simple, set our headers doing the following in your test case:

function myAjaxTestAction()
{
      $this->request->setHeader('X_REQUESTED_WITH');

      //then do your ajax tests here
}

This will fake an AJAX request and return the expected result.

I hope this article helps many of you since I spent many hours trying to decipher why setting ‘XMLHttpRequest’ to the $_SERVER did not work as I expected.

9 Comments to “Unit Testing your AJAX requests with Zend Framework and PHPunit”

  • Hi ,

    It helps me lot ,saved my time . a small change in the code is required .

    $this->request->setHeader('X-Requested-With', 'XmlHttpRequest');
    Thanks you very much

    • Well that's what I had initially, but if you use it inside a unit test (assuming you are using Zend_Test_PHPUnit) I was running into the problem specified above. Not sure if this is a bug, or a change based on a specific version.
      But thanks for sharing!

  • could u provide me one example using ajax(isXMLHttpRequest) and zend to insert without reloading the page

    please
    i tried several examples i hadn't got the correct solution

    • Mike,
      This article is for unit testing. If you want to work with ajax I suggest reading this article I wrote some time ago. http://www.smooka.com/blog/2009/04/17/5-things-wo
      It lists 5 things you need to know when working with ajax and zend framework.

      thanks

  • function searchPackagesAction()
    if ($this->_request->isXmlHttpRequest()) {
    $output['data']='jegan';
    $this->getHelper('json')->sendJson($output);

    }}

  • $this->getRequest()->setHeader('X_REQUESTED_WITH', 'XMLHttpRequest'); is working fine now .. 'X-Requested-With is changed to X_REQUESTED_WITH

    • Jegan what version of ZF are you using?
      Thanks for letting me know, I will try it out and see if it was a bug that they fixed.

  • Nice post. I implementd PHPUnit with Symfony2 and PHPStorm and i have to reconiced that TDD is the best practice if you develop.
    Thanks 😉

  • Great post. How do you configure phpunit on zend framework¿

Leave a comment