Completed
Push — develop ( 93c875...c90588 )
by Freddie
05:45
created

testItSchemaAttributeWithRequiredPropertiesSetValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 12
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of FlexPHP.
4
 *
5
 * (c) Freddie Gar <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace FlexPHP\Schema\Tests;
11
12
use FlexPHP\Schema\SchemaAttribute;
13
14
class SchemaAttributeTest extends TestCase
15
{
16
    public function testItSchemaAttributeInvalidInvalidThrowException(): void
17
    {
18
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class);
19
        $this->expectExceptionMessage('is empty');
20
21
        $schemaAttribute = new SchemaAttribute();
22
        $schemaAttribute->validate();
23
    }
24
25
    public function testItSchemaAttributeWithIncompleteRequirePropertiesThrowException(): void
26
    {
27
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class);
28
        $this->expectExceptionMessage('are missing');
29
30
        $schemaAttribute = new SchemaAttribute();
31
        $schemaAttribute->setName('foo');
32
        $schemaAttribute->validate();
33
    }
34
35
    public function testItSchemaAttributeWithInvalidNamePropertiesThrowException(): void
36
    {
37
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class);
38
        $this->expectExceptionMessage('Name:');
39
40
        $schemaAttribute = new SchemaAttribute();
41
        $schemaAttribute->setName('');
42
        $schemaAttribute->setDataType('string');
43
        $schemaAttribute->validate();
44
    }
45
46
    public function testItSchemaAttributeWithInvalidDataTypePropertiesThrowException(): void
47
    {
48
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class);
49
        $this->expectExceptionMessage('DataType:');
50
51
        $schemaAttribute = new SchemaAttribute();
52
        $schemaAttribute->setName('foo');
53
        $schemaAttribute->setDataType('bar');
54
        $schemaAttribute->validate();
55
    }
56
57
    public function testItSchemaAttributeWithRequiredPropertiesSetValues(): void
58
    {
59
        $name = 'foo';
60
        $dataType = 'string';
61
62
        $schemaAttribute = new SchemaAttribute();
63
        $schemaAttribute->setName($name);
64
        $schemaAttribute->setDataType($dataType);
65
        $schemaAttribute->validate();
66
67
        $this->assertEquals($name, $schemaAttribute->name());
68
        $this->assertEquals($dataType, $schemaAttribute->dataType());
69
    }
70
71
    public function testItSchemaAttributePropertiesSetValues(): void
72
    {
73
        $name = 'foo';
74
        $dataType = 'string';
75
        $constraints = 'required|min:8|max:10|type:number';
76
        $type = 'text';
77
78
        $schemaAttribute = new SchemaAttribute();
79
        $schemaAttribute->setName($name);
80
        $schemaAttribute->setDataType($dataType);
81
        $schemaAttribute->setConstraints($constraints);
82
        $schemaAttribute->setType($type);
83
        $schemaAttribute->validate();
84
85
        $this->assertEquals($name, $schemaAttribute->name());
86
        $this->assertEquals($dataType, $schemaAttribute->dataType());
87
        $this->assertEquals([
88
            'required' => true,
89
            'min' => 8,
90
            'max' => 10,
91
            'type' => 'number',
92
        ], $schemaAttribute->constraints());
93
        $this->assertEquals($type, $schemaAttribute->type());
94
    }
95
96
    public function testItSchemaAttributePropertiesSetConstraintsAsArray(): void
97
    {
98
        $name = 'foo';
99
        $dataType = 'string';
100
        $constraints = [
101
            'required' => true,
102
            'minlength' => 1,
103
            'maxlength' => '2',
104
            'mincheck' => 3,
105
            'maxcheck' => '4',
106
            'min' => 5,
107
            'max' => '6',
108
            'equalto' => '#foo',
109
            'type' => 'number',
110
        ];
111
        $type = 'text';
112
113
        $schemaAttribute = new SchemaAttribute();
114
        $schemaAttribute->setName($name);
115
        $schemaAttribute->setDataType($dataType);
116
        $schemaAttribute->setConstraints($constraints);
117
        $schemaAttribute->setType($type);
118
        $schemaAttribute->validate();
119
120
        $this->assertEquals($name, $schemaAttribute->name());
121
        $this->assertEquals($dataType, $schemaAttribute->dataType());
122
        $this->assertEquals($constraints, $schemaAttribute->constraints());
123
        $this->assertEquals($type, $schemaAttribute->type());
124
125
        $this->assertTrue($schemaAttribute->isRequired());
126
        $this->assertEquals($constraints['minlength'], $schemaAttribute->minLength());
127
        $this->assertEquals($constraints['maxlength'], $schemaAttribute->maxLength());
128
        $this->assertEquals($constraints['mincheck'], $schemaAttribute->minCheck());
129
        $this->assertEquals($constraints['maxcheck'], $schemaAttribute->maxCheck());
130
        $this->assertEquals($constraints['min'], $schemaAttribute->min());
131
        $this->assertEquals($constraints['max'], $schemaAttribute->max());
132
        $this->assertEquals($constraints['equalto'], $schemaAttribute->equalTo());
133
    }
134
}
135