Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4018798
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:01:38+00:00 2026-05-20T10:01:38+00:00

How to test an ajax call using phpunit test case ?

  • 0

How to test an ajax call using phpunit test case ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:01:39+00:00Added an answer on May 20, 2026 at 10:01 am

    Depending on how your ajax response is generated, this may be easy or more difficult. If you use a controller to generate the response, you can simply call the appropriate function in the unit test and check the returned HTML/XML. For example, if responses are generated by calling functions on a controller object like this:

    $htmlResponse = $controller->buildPage($_REQUEST);
    

    then you can unit test the response like this:

    $expected = "<html>whatever html you expect</html>";
    $test_request = array(...); // parameters to test
    $actual = $controller->buildPage($test_request);
    
    $this->assertEquals($expected, $actual);
    

    If your response isn’t generated with a function call like that – if your AJAX request is for an actual page with some dynamic content in it, seems like you could do something like this:

    $_POST['parameter1'] = "test value";  // stuff whichever request object you're using
                                          // with the values you need
    ob_start();
    include('ajax_page.php');
    $actual = ob_get_clean();
    $expected = "<html>expected html as above</html>";
    
    $this->assertEquals($expected, $actual);
    

    PHPUnit also provides the assertTag assertion for testing that generated HTML and XML contains the tags you expect – it can be a little finicky but it’s more robust than just echoing out the response and comparing to a string.

    Ultimately, all you want to know is that, given a certain input (the request parameters), you get the output you expect (the returned HTML or XML). The fact that it’s an AJAX call doesn’t fundamentally alter the equation. If you wanted to test that the AJAX requests are being MADE appropriately, you’d want to do that on the client side with JS testing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I trying to make my first AJAX with JSON call using jQuery and CodeIgniter.
Doing an ajax get request works as expected using the following code: $.ajax({ type:
Overflowers': I'm using Zend, and doing an Ajax post to a controller. I can
Calling the ajax called URL works well without ajax eg. http://localhost/ci/controller/method/param_value . But using
I had a previous question can jquery ajax call external webservice? and some good
I'm trying to test a google maps app with Rails3. I'm using cucumber with
I'm building a mobile web application that may or may not rely on ajax,
The contents of test.json are: {foo: The quick brown fox jumps over the lazy
I am making an ajax request from a jquery script to one of my
Really simple question. I trying to test a Restful webservice that I am developing,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.