Completed
Push — master ( bcf6c2...90950f )
by Alexpts
01:22
created

JsonSchema   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 34
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A _beforeSuite() 0 4 1
A init() 0 15 3
A validateJsonSchema() 0 5 1
1
<?php
2
namespace PTS\Codeception;
3
4
use Codeception\Configuration;
5
use Codeception\Module;
6
7
/**
8
 * Json schema module for codeception
9
 */
10
class JsonSchema extends Module
11
{
12
    const SCHEMA_URI = 'http://demo.org';
13
14
    /** @var \PTS\JsonSchema */
15
    protected $validator;
16
17
    public function _beforeSuite($settings = [])
18
    {
19
        $this->init();
20
    }
21
22
    protected function init()
23
    {
24
        if (!array_key_exists('schemaDir', $this->config)) {
25
            throw new \Exception('Config `schemaDir` is required ');
26
        }
27
28
        $this->validator = new \PTS\JsonSchema;
29
30
        if (!array_key_exists('baseUri', $this->config)) {
31
            $this->validator->setBaseUri($this->config['baseUri']);
32
        }
33
34
        $schemasDir = realpath(Configuration::testsDir() . '/' . $this->config['schemaDir']);
35
        $this->validator->loadAllSchemas($schemasDir);
36
    }
37
38
    public function validateJsonSchema(string $response, string $pathSchema): void
39
    {
40
        $errors = $this->validator->validateJsonSchema($response, $pathSchema);
41
        $this->assertTrue($errors !== null, $errors);
42
    }
43
}
44