Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

seeResponseIsJson() method from REST module fails under PHP7 #3400

Closed
niclopez opened this issue Aug 3, 2016 · 4 comments
Closed

seeResponseIsJson() method from REST module fails under PHP7 #3400

niclopez opened this issue Aug 3, 2016 · 4 comments

Comments

@niclopez
Copy link
Contributor

@niclopez niclopez commented Aug 3, 2016

A simple functional test using "seeResponseIsJson()" method fails under PHP7.

The test code

class DemoPhp7Cest {
    /**
     * @group  php7
     */
    public function isJsonEndpointUnderPhp7Test(FunctionalTester $I)
    {
        $I->sendPOST('/alert', "some invalid json");
        $I->seeResponseIsJson();
    }  
}

Test result under php 5.6.23

Codeception PHP Testing Framework v2.2.3
Powered by PHPUnit 5.4.8 by Sebastian Bergmann and contributors.
[Groups] php7 

Functional Tests (1) ---------------------------------------
Modules: \Helper\Functional, \Helper\Utils, \Helper\AMQP, Asserts, PhpBrowser, REST
------------------------------------------------------------
DemoPhp7Cest: Is json endpoint under php7 test
Signature: DemoPhp7Cest:isJsonEndpointUnderPhp7Test
Test: tests/functional/DemoPhp7Cest.php:isJsonEndpointUnderPhp7Test
Scenario --
 I send post "/alert","some invalid json"
  [Request] POST http://127.0.0.1/alert some invalid json
  [Request Headers] []
  [Page] http://127.0.0.1/alert
  [Response] 404
  [Request Cookies] []
  [Response Headers] {"Date":["Wed, 03 Aug 2016 08:10:10 GMT"],"Server":["Apache/2.4.10 (Debian)"],"X-Powered-By":["PHP/5.6.23"],"Status":["404 Not Found"],"Content-Length":["0"],"Content-Type":["application/json"]}
  [Response] 
 I see response is json 
 PASSED 

Test result under php 7

Codeception PHP Testing Framework v2.2.3
Powered by PHPUnit 5.4.8 by Sebastian Bergmann and contributors.
[Groups] php7 

Functional Tests (1) ---------------------------------------
Modules: \Helper\Functional, \Helper\Utils, \Helper\AMQP, Asserts, PhpBrowser, REST
------------------------------------------------------------
PruebaPhp7Cest: Is json endpoint under php7 test
Signature: PruebaPhp7Cest:isJsonEndpointUnderPhp7Test
Test: tests/functional/PruebaPhp7Cest.php:isJsonEndpointUnderPhp7Test
Scenario --
 I send post "/alert","some invalid json"
  [Request] POST http://127.0.0.1/alert some invalid json
  [Request Headers] []
  [Page] http://127.0.0.1/alert
  [Response] 404
  [Request Cookies] []
  [Response Headers] {"Date":["Wed, 03 Aug 2016 08:03:01 GMT"],"Server":["Apache/2.4.10 (Debian)"],"X-Powered-By":["PHP/7.0.9"],"Status":["404 Not Found"],"Content-Length":["0"],"Content-Type":["application/json"]}
  [Response] 
 I see response is json 
 FAIL 
@niclopez
Copy link
Contributor Author

@niclopez niclopez commented Aug 3, 2016

After debugging, I'ven seen that "json_last_error()" php function works in a different way under php7. Now, it returns a syntax error when json_decode fails (http://php.net/manual/es/function.json-last-error.php#118165).

Then, "seeResponseIsJson()" method in REST module, uses "json_last_error()" for assertion and fails.

#REST.php module   
public function seeResponseIsJson()
    {
        json_decode($this->connectionModule->_getResponseContent());
        $errorCode = json_last_error();
        $errorMessage = json_last_error_msg();
        \PHPUnit_Framework_Assert::assertEquals(
            JSON_ERROR_NONE,
            $errorCode,
            sprintf(
                "Invalid json: %s. System message: %s.",
                $this->connectionModule->_getResponseContent(),
                json_last_error_msg()
            )
        );
    }
@Naktibalda
Copy link
Member

@Naktibalda Naktibalda commented Aug 3, 2016

So it was failing correctly, I wonder if we should make the empty response fail on PHP 5.

@niclopez
Copy link
Contributor Author

@niclopez niclopez commented Aug 3, 2016

I agree @Naktibalda , I think It works ok on PHP7. The empty response fail should be launched on PHP5 too.

Now the same method works different depends on php version.

Naktibalda added a commit to Naktibalda/Codeception that referenced this issue Aug 3, 2016
This change makes behaviour of this method on PHP5 consistent with PHP7
Closes Codeception#3400
DavertMik added a commit that referenced this issue Aug 4, 2016
* [REST] seeResponseIsJson must fail when response is empty

This change makes behaviour of this method on PHP5 consistent with PHP7
Closes #3400

* Comparison message should not override assertion message

Before:
 Test  tests/unit/Codeception/Module/RestTest.php:testSeeResponseIsJsonFailsWhenResponseIsInvalidJson
Failed asserting that 4 matches expected 0.

After:
 Test  tests/unit/Codeception/Module/RestTest.php:testSeeResponseIsJsonFailsWhenResponseIsInvalidJson
Invalid json: {. System message: Syntax error.
Failed asserting that 4 matches expected 0.
@niclopez
Copy link
Contributor Author

@niclopez niclopez commented Aug 5, 2016

Fantastic job! thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.