πŸ‡ΊπŸ‡¦ Help Ukraine!

Guides

  1. Introduction
  2. Getting Started
  3. Acceptance Tests
  4. Functional Tests
  5. Unit Tests
  6. Debugging
  7. Modules And Helpers
  8. Reusing Test Code
  9. Advanced Usage
  10. BDD
  11. Customization
  12. Data
  13. API Testing
  14. Codecoverage
  15. Reporting
  16. Continuous Integration
  17. Parallel Execution

Enterprise support

Trainings

Testomatio - Test Management for Codeception

Search

Codeception\Util\JsonType

JsonType matches JSON structures against templates. You can specify the type of fields in JSON or add additional validation rules.

JsonType is used by REST module in seeResponseMatchesJsonType and dontSeeResponseMatchesJsonType methods.

Usage example:

<?php
$jsonType = new JsonType(['name' => 'davert', 'id' => 1, 'data' => []]);
$jsonType->matches([
 'name' => 'string:!empty',
 'id' => 'integer:>0|string:>0',
 'data' => 'array:empty',
]); // => true
$jsonType->matches([
 'id' => 'string',
]); // => `id: 1` is not of type string

Class JsonType @package Codeception\Util

__construct()

public __construct($jsonArray)

  • param $jsonArray array JsonArray

Creates instance of JsonType Pass an array or \Codeception\Util\JsonArray with data.

If non-associative array is passed - the very first element of it will be used for matching.

See source

addCustomFilter()

public static addCustomFilter($name, callable $callable)

  • param string $name
  • param callable $callable
  • return void

Adds custom filter to JsonType list.

You should specify a name and parameters of a filter.

Example:

<?php
JsonType::addCustomFilter('slug', function($value) {
 return strpos(' ', $value) !== false;
});
// => use it as 'string:slug'
// add custom function to matcher with `len($val)` syntax
// parameter matching patterns should be valid regex and start with `/` char
JsonType::addCustomFilter('/len\((.*?)\)/', function($value, $len) {
 return strlen($value) == $len;
});
// use it as 'string:len(5)'

See source

cleanCustomFilters()

public static cleanCustomFilters()

  • return void

Removes all custom filters

See source

matches()

public matches(array $jsonType)

  • param array $jsonType
  • return string|bool

Checks data against passed JsonType.

If matching fails function returns a string with a message describing failure. On success returns true.

See source

Reference is taken from the source code. Help us to improve documentation. Edit module reference
Don't know how to write tests on your own? We will build or improve them for you. Request a quote for official enterprise support or trainings
❀️ Support us on OpenCollective!
[フレーム]

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /