PHP code example of osi-open-source / codeception-json-schema

1. Go to this page and download the library: Download osi-open-source/codeception-json-schema library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

osi-open-source / codeception-json-schema example snippets



class MessageApiCest
{
    public function aTest(ApiTester $I)
    {
        /* call api */
        $I->wantTo('Ensure API Returns Json which matches schema file.');
        $I->sendGET('/path/to/api');

        /* check if api matches schema */
        $I->seeResponseIsValidOnSchemaFile('/path/to/schema.json');
    }

    public function bTest(ApiTester $I)
    {
        /* call api */
        $I->wantTo('Ensure API Returns Json which matches schema file.');
        $I->sendGET('/path/to/api');

        /* alternative syntax, check if api matches schema */
        $I->canSeeResponseIsValidOnSchemaFile('/path/to/schema.json');
    }

    public function cTest(ApiTester $I)
    {
        /* call api */
        $I->wantTo('Ensure API Returns Json which matches inline schema.');
        $I->sendGET('/path/to/api');

        /* if you don't have a separate schema file, that is alright, you can use inline schema */
        /* this schema expects the api to return something like {"message": "SOME_STRING"} */
        /* schema as php objects */
        $schema = (object)[
            'type' => 'object',
            'properties' => (object)[
                'message' => (object)[
                    'type' => 'string',
                ],
            ],
            '


class MessageApiCest
{
    public function eTest(ApiTester $I)
    {
        /* call api */
        $I->wantTo('Ensure API Returns Json which matches type.');
        $I->sendGET('/path/to/api');

        /* this type expects the api to return something like {"message": "SOME_STRING"} */
        $I->seeResponseMatchesJsonType([
            'message' => 'string',
        ]);
    }
}