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

seeResponseMatchesJsonType throws PHP exception #4517

Closed
Konzertheld opened this issue Sep 15, 2017 · 6 comments
Closed

seeResponseMatchesJsonType throws PHP exception #4517

Konzertheld opened this issue Sep 15, 2017 · 6 comments

Comments

@Konzertheld
Copy link

@Konzertheld Konzertheld commented Sep 15, 2017

I am trying to use this code in a Cept:

$I->seeResponseMatchesJsonType([
    'id' => 'integer',
    'playerMin' => 'integer',
    'playerMax' => 'integer',
    'ageMin' => 'integer',
    'duration' => 'integer',
    'name' => 'string',
    'publisher' => 'string',
    'feature' => 'string',
    'number' => 'string',
    'numberHuman' => 'string',
    'fund' => 'string',
    'lentTo' => 'string',
    ]);

But I receive:

Test  tests/api/RetrieveGameCept.php
                                                                                                 
 [PHPUnit\Framework\Exception] array_key_exists() expects parameter 2 to be array, string given  
                                                                                                 

Scenario Steps:

5. $I->seeResponseMatchesJsonType({"id":"integer", "playerMin":"integer","playerMax":"integer","ageMin":"integer","duration":"integer","name":"string","publisher":"string","feature":"string","number":"strin...}) at tests/api/RetrieveGameCept.php:9

Other tests (seeResponseContainsJson, seeResponseIsJson) work.

  • Codeception version: 2.3.5
  • PHP Version: 7.1.9
  • Operating System: Arch
  • Installation type: Phar
  • Suite configuration:
actor: ApiTester
modules:
    enabled:
        - REST:
            url: http://localhost/spielenacht/api/
            depends: PhpBrowser
            part: Json

Test succeeds when I remove the code cited above. :-(

@Naktibalda
Copy link
Member

@Naktibalda Naktibalda commented Sep 16, 2017

Please provide a complete output of codecept run tests/api/RetrieveGameCept.php -vvv

@Konzertheld
Copy link
Author

@Konzertheld Konzertheld commented Sep 16, 2017

$ php codecept.phar run tests/api/RetrieveGameCept.php -vvv
PHP Warning:  PHP Startup: gnupg: Unable to initialize module
Module compiled with module API=20151012
PHP    compiled with module API=20160303
These options need to match
 in Unknown on line 0
Codeception PHP Testing Framework v2.3.5
Powered by PHPUnit 6.2.4 by Sebastian Bergmann and contributors.

  Rebuilding ApiTester...

Api Tests (1) -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Modules: REST, PhpBrowser
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
RetrieveGameCept: Retrieve one game
Signature: RetrieveGameCept
Test: tests/api/RetrieveGameCept.php
Scenario --
 I send get "/game.php",{"number":"00003403"}
  [Request] GET http://localhost/spielenacht/api//game.php?number=00003403
  [Request Headers] []
  [Page] http://localhost/spielenacht/api//game.php?number=00003403
  [Response] 200
  [Request Cookies] []
  [Response Headers] {"Server":["nginx/1.13.4"],"Date":["Sat, 16 Sep 2017 21:57:19 GMT"],"Content-Type":["text/html; charset=UTF-8"],"Transfer-Encoding":["chunked"],"Connection":["keep-alive"],"X-Powered-By":["PHP/7.1.9"]}
  [Response] {"0":"IstanbulPegasus","id":"IstanbulPegasus","1":"Istanbul","name":"Istanbul","2":"Pegasus","publisher":"Pegasus","3":2,"playerMin":2,"4":5,"playerMax":5,"5":10,"ageMin":10,"6":40,"duration":40,"7":"P","feature":"P","8":"00003403","number":"00003403","9":"0-34-3P","numberHuman":"0-34-3P","10":"Ausgleich","fund":"Ausgleich","11":null,"lentTo":null}
 I see response code is 200
 I see response is json 
 I see response contains json {"name":"Istanbul"}
 I see response matches json type {"id":"integer","playerMin":"integer","playerMax":"integer","ageMin":"integer","duration":"integer","name":"string","publisher":"string","feature":"string","number":"string"...}
 ERROR 

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Time: 190 ms, Memory: 10.00MB

There was 1 error:

---------
1) RetrieveGameCept: Retrieve one game
 Test  tests/api/RetrieveGameCept.php
                                                                                                  
  [PHPUnit\Framework\Exception] array_key_exists() expects parameter 2 to be array, string given  
                                                                                                  

Scenario Steps:

 5. $I->seeResponseMatchesJsonType({"id":"integer","playerMin":"integer","playerMax":"integer","ageMin":"integer","duration":"integer","name":"string","publisher":"string","feature":"string","number":"strin...}) at tests/api/RetrieveGameCept.php:9
 4. $I->seeResponseContainsJson({"name":"Istanbul"}) at tests/api/RetrieveGameCept.php:7
 3. $I->seeResponseIsJson() at tests/api/RetrieveGameCept.php:6
 2. $I->seeResponseCodeIs(200) at tests/api/RetrieveGameCept.php:5
 1. $I->sendGET("/game.php",{"number":"00003403"}) at tests/api/RetrieveGameCept.php:4

phar:///home/christian/http/spielenacht/codecept.phar/src/Codeception/Util/JsonType.php:119
phar:///home/christian/http/spielenacht/codecept.phar/src/Codeception/Util/JsonType.php:103
phar:///home/christian/http/spielenacht/codecept.phar/src/Codeception/PHPUnit/Constraint/JsonType.php:34
phar:///home/christian/http/spielenacht/codecept.phar/vendor/phpunit/phpunit/src/Framework/Constraint/Constraint.php:53
phar:///home/christian/http/spielenacht/codecept.phar/vendor/phpunit/phpunit/src/Framework/Assert.php:2116
phar:///home/christian/http/spielenacht/codecept.phar/src/Codeception/Module/REST.php:1005
phar:///home/christian/http/spielenacht/codecept.phar/src/Codeception/Step.php:260
phar:///home/christian/http/spielenacht/codecept.phar/src/Codeception/Scenario.php:72
/home/christian/http/spielenacht/tests/_support/_generated/ApiTesterActions.php:1081
/home/christian/http/spielenacht/tests/api/RetrieveGameCept.php:9
phar:///home/christian/http/spielenacht/codecept.phar/src/Codeception/Test/Cept.php:42
phar:///home/christian/http/spielenacht/codecept.phar/src/Codeception/Test/Test.php:87
phar:///home/christian/http/spielenacht/codecept.phar/vendor/phpunit/phpunit/src/Framework/TestSuite.php:744
phar:///home/christian/http/spielenacht/codecept.phar/src/Codeception/PHPUnit/Runner.php:106
phar:///home/christian/http/spielenacht/codecept.phar/src/Codeception/SuiteManager.php:157
phar:///home/christian/http/spielenacht/codecept.phar/src/Codeception/Codecept.php:188
phar:///home/christian/http/spielenacht/codecept.phar/src/Codeception/Codecept.php:157
phar:///home/christian/http/spielenacht/codecept.phar/src/Codeception/Command/Run.php:341
phar:///home/christian/http/spielenacht/codecept.phar/vendor/symfony/console/Command/Command.php:264
phar:///home/christian/http/spielenacht/codecept.phar/vendor/symfony/console/Application.php:874
phar:///home/christian/http/spielenacht/codecept.phar/vendor/symfony/console/Application.php:228
phar:///home/christian/http/spielenacht/codecept.phar/vendor/symfony/console/Application.php:130
phar:///home/christian/http/spielenacht/codecept.phar/src/Codeception/Application.php:103
phar:///home/christian/http/spielenacht/codecept.phar/codecept:36
/home/christian/http/spielenacht/codecept.phar:7


ERRORS!
Tests: 1, Assertions: 5, Errors: 1.

@Naktibalda
Copy link
Member

@Naktibalda Naktibalda commented Sep 18, 2017

You get this error, because you have a key 0 at the top level,
because of that JsonType assumes that you have an array of objects.

The solution for you is to change fetch type from FETCH_BOTH to FETCH_ASSOC.

@Naktibalda Naktibalda closed this Sep 18, 2017
@Konzertheld
Copy link
Author

@Konzertheld Konzertheld commented Sep 18, 2017

Confirmed, thanks!

@Naktibalda
Copy link
Member

@Naktibalda Naktibalda commented Sep 18, 2017

I will patch JsonType to handle this type of response correctly.

@Naktibalda Naktibalda reopened this Sep 18, 2017
@Konzertheld
Copy link
Author

@Konzertheld Konzertheld commented Sep 18, 2017

That would be useful, I also think responses with numerical index not starting with 0 will cause problems too (not tested).

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

Successfully merging a pull request may close this issue.

None yet
2 participants