Passed
Push — test ( d838cf...cef4a5 )
by Tom
03:29
created

ValidationTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 57
dl 0
loc 133
rs 10
c 0
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testValidationRequiresFormatsDisabled() 0 17 1
A validatorByFixture() 0 16 2
A testValidationConstInOneOf() 0 15 1
A testValidateProjectFile() 0 9 1
A debugValidator() 0 13 4
A testInvalidPipelinesFile() 0 9 1
A testValidationRequiresFormatsDisabledReducedFixtures() 0 17 1
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Integration\File;
6
7
use JsonSchema\Constraints\Constraint;
8
use Ktomk\Pipelines\TestCase;
9
use Ktomk\Pipelines\Yaml\Yaml;
10
11
/**
12
 * @coversNothing
13
 */
14
class ValidationTest extends TestCase
15
{
16
    /**
17
     * validate own bitbucket-pipelines.yml file against schema
18
     */
19
    public function testValidateProjectFile()
20
    {
21
        list($data, $validator) = $this->validatorByFixture(
22
            __DIR__ . '/../../../bitbucket-pipelines.yml',
23
            __DIR__ . '/../../../lib/pipelines/schema/pipelines-schema.json',
24
            Constraint::CHECK_MODE_DISABLE_FORMAT
25
        );
26
        $this->debugValidator($validator);
27
        self::assertTrue($validator->isValid());
28
    }
29
30
    /**
31
     * validate a file with knowing validation error
32
     */
33
    public function testInvalidPipelinesFile()
34
    {
35
        list($data, $validator) = $this->validatorByFixture(
36
            __DIR__ . '/../../data/yml/invalid-pipeline-step.yml',
37
            __DIR__ . '/../../../lib/pipelines/schema/pipelines-schema.json',
38
            Constraint::CHECK_MODE_DISABLE_FORMAT
39
        );
40
        $this->debugValidator($validator);
41
        self::assertFalse($validator->isValid());
42
    }
43
44
    /**
45
     * justinrainbow / json-schema flaw on "const" usage (regression test)
46
     */
47
    public function testValidationConstInOneOf()
48
    {
49
        $prefix = __DIR__ . '/_validation/';
50
51
        $yamlFile = $prefix . 'test-01.yml';
52
53
        // test for regression on "const" constraint in "oneOf"
54
        list($data, $validator) = $this->validatorByFixture($yamlFile, $prefix . 'test-01-schema-fail.json');
55
        self::assertFalse($validator->isValid());
56
57
        // demonstrate "enum" work-around working
58
        list($data, $validator) = $this->validatorByFixture($yamlFile, $prefix . 'test-01-schema-ok.json');
59
        self::assertTrue($validator->isValid());
60
61
        $this->debugValidator($validator);
62
    }
63
64
    /**
65
     * test disable format requirement (fuller yaml)
66
     *
67
     * for a useful validation to work, formats (e.g. for "email") must be disabled
68
     * for schema validation as pipelines supports variable substitution for
69
     * such properties
70
     */
71
    public function testValidationRequiresFormatsDisabled()
72
    {
73
        $prefix = __DIR__ . '/_validation/';
74
        $yamlFile = $prefix . 'test-02.yml';
75
76
        // test for image parse error on pipeline step
77
        list($data, $validator) = $this->validatorByFixture($yamlFile, $prefix . 'test-02-schema-fail.json');
78
        self::assertFalse($validator->isValid());
79
80
        // test for image parse error on pipeline step
81
        list($data, $validator) = $this->validatorByFixture(
82
            $yamlFile,
83
            $prefix . 'test-02-schema-fail.json',
84
            Constraint::CHECK_MODE_DISABLE_FORMAT
85
        );
86
        $this->debugValidator($validator);
87
        self::assertTrue($validator->isValid());
88
    }
89
90
    /**
91
     * test disable format requirement (reduced yaml)
92
     *
93
     * for a useful validation to work, formats (e.g. for "email") must be disabled
94
     * for schema validation as pipelines supports variable substitution for
95
     * such properties
96
     */
97
    public function testValidationRequiresFormatsDisabledReducedFixtures()
98
    {
99
        $prefix = __DIR__ . '/_validation/';
100
        $yamlFile = $prefix . 'test-03.yml';
101
102
        // test for image parse error on pipeline step
103
        list($data, $validator) = $this->validatorByFixture($yamlFile, $prefix . 'test-03-schema-fail.json');
104
        self::assertFalse($validator->isValid());
105
106
        // test for image w/o parse error disabling format
107
        list($data, $validator) = $this->validatorByFixture(
108
            $yamlFile,
109
            $prefix . 'test-03-schema-fail.json',
110
            Constraint::CHECK_MODE_DISABLE_FORMAT
111
        );
112
        $this->debugValidator($validator);
113
        self::assertTrue($validator->isValid());
114
    }
115
116
    private function validatorByFixture($yamlFile, $schemaFile, $checkMode = null)
117
    {
118
        $data = Yaml::file($yamlFile);
119
120
        null === $checkMode && $checkMode = 0;
121
        $checkMode |= Constraint::CHECK_MODE_TYPE_CAST;
122
123
        // Validate
124
        $validator = new \JsonSchema\Validator();
125
        $validator->validate(
126
            $data,
127
            (object)array('$ref' => 'file://' . $schemaFile),
128
            $checkMode
129
        );
130
131
        return array($data, $validator);
132
    }
133
134
    private function debugValidator($validator)
135
    {
136
        // do not output unless debugOutput is set
137
        if (empty($this->debugOutput)) {
138
            return;
139
        }
140
141
        if ($validator->isValid()) {
142
            echo "The supplied JSON validates against the schema.\n";
143
        } else {
144
            echo "JSON does not validate. Violations:\n";
145
            foreach ($validator->getErrors() as $error) {
146
                printf("[%s] %s\n", $error['property'], $error['message']);
147
            }
148
        }
149
    }
150
}
151