Question

I am using phpunit to write unit tests.

Now I would like to test that the HTTP response codes are the ones that are expected, ie. something like :

$res = $req->getPage('NonExistingPage.php', 'GET');
assertTrue($res->getHttpResponseCode(), 404);

I know Symfony and Zend can do this. However I developped my entire project without the use of any framework. And, from what I understood, if one wants to use these frameworks he has to change his project to adopt the default project's structure of these frameworks. But I don't want to change anything in my project (not even its folder structure).

So is there any way to write such tests (checking http response codes) without changing my existing project ?

Was it helpful?

Solution

assert(strpos(get_headers('http://www.nonexistingpage.com')[0],'404') !== false) 

OTHER TIPS

While you do not need a framework, the testing framework should still use Mock objects, and you should then have your code handle the functions accordingly. For instance, your libraries need to do something on the 404 error. Do not test that the HTML error code is 404, but rather that your libraries behave correctly.

class YourHTTPClass
{
    private $HttpResponseCode;
    public function getPage($URL, $Method)
    {
        // Do some code to get the page, set the error etc.
    }

    public function getHttpResponseCode()
    {
        return $this->HttpResponseCode;
    }

    ...
}

PHPUnit Tests:

class YourHTTPClass_Test extends \PHPUnit_Framework_TestCase
{
    public function testHTMLError404()
    {
        // Create a stub for the YourHTTPClass.
        $stub = $this->getMock('YourHTTPClass');

        // Configure the stub.
        $stub->expects($this->any())
             ->method('getHttpResponseCode')
             ->will($this->returnValue(404));

        // Calling $stub->getHttpResponseCode() will now return 404
        $this->assertEquals(404, $stub->getHttpResponseCode('http://Bad_Url.com', 'GET'));      
        // Actual URL does not matter as external call will not be done with the mock
    }

    public function testHTMLError505()
    {
        // Create a stub for the YourHTTPClass.
        $stub = $this->getMock('YourHTTPClass');

        // Configure the stub.
        $stub->expects($this->any())
             ->method('getHttpResponseCode')
             ->will($this->returnValue(505));

        // Calling $stub->getHttpResponseCode() will now return 505
        $this->assertEquals(505, $stub->getHttpResponseCode('http://Bad_Url.com', 
}

This way you have tested that your code will handle the various return codes. With the mock objects, you may define multiple access options, or use data providers, etc... to generate the different error codes.

You will know that your code will be able to handle any of the errors, without needing to go to external web services to validate the errors.

To test your code that gets data, you would do something similar where you would actually mock the GET function to return known information, so you could test the code getting the results.

  1. $this->assertEquals($expected, $actual);
  2. Need to check $res(not $req->getHttpResponseCode())
  3. Look at class, which is returned by method $req->getPage(...), and find there method, which returns http code.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top