Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

When AbstractControllerTestCase::assertResponseStatusCode() fails it is not counted as an assertion by PHPUnit #29

@GeeH

Description

@GeeH

This issue has been moved from the zendframework repository as part of the bug migration program as outlined here - http://framework.zend.com/blog/2016-04-11-issue-closures.html


Original Issue: https://api.github.com/repos/zendframework/zendframework/issues/7556
User: @sebastianbergmann
Created On: 2015-05-29T11:41:12Z
Updated At: 2015-11-06T21:55:09Z
Body
AbstractControllerTestCase::assertResponseStatusCode() is not counted as an assertion in the failure case. This is due to the fact that an PHPUnit_Framework_ExpectationFailedException is thrown when the actual and expected response status code are not equal:

public function assertResponseStatusCode($code)
{
    if ($this->useConsoleRequest) {
        if (!in_array($code, array(0, 1))) {
            throw new PHPUnit_Framework_ExpectationFailedException(
                'Console status code assert value must be O (valid) or 1 (error)'
            );
        }
    }
    $match = $this->getResponseStatusCode();
    if ($code != $match) {
        throw new PHPUnit_Framework_ExpectationFailedException(
            sprintf('Failed asserting response code "%s", actual status code is "%s"', $code, $match)
        );
    }
    $this->assertEquals($code, $match);
}

Since the comparison of actual and expected response status code is not performed using assertEquals() or assertThat(), for instance, the assertion counter is not incremented.

The first place in the method's code where a PHPUnit_Framework_ExpectationFailedException is thrown could be refactored as

$this->assertThat(
    $code,
    $this->logicalOr(
        $this->equalTo(0),
        $this->equalTo(1)
    ),
    'Console status code assert value must be O (valid) or 1 (error)'
);

It is not as easy as this, unfortunately, for the second place in the code where a PHPUnit_Framework_ExpectationFailedException is thrown:

if ($code != $match) {
    throw new PHPUnit_Framework_ExpectationFailedException(
        sprintf('Failed asserting response code "%s", actual status code is "%s"', $code, $match)
    );
}
$this->assertEquals($code, $match);

When the actual and expected response status codes are not equal then a custom message is generated and used with the manually thrown PHPUnit_Framework_ExpectationFailedException. This, again, circumvents the assertion counter.

When the actual and expected response status codes are equal then assertEquals() is used to verify the match again. This ensures that the assertion is counted in the success case.

The assertion methods of AbstractControllerTestCase should be refactored to follow the best practice of implementing custom assertions for PHPUnit. Simply implement a constraint object (by extending PHPUnit_Framework_Constraint) for each assertion. Then refactor the assertion to use the constraint object by passing it to assertThat().


Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions