Passed
Push — master ( a80842...bbbee0 )
by Caen
03:37 queued 13s
created

PublicationFieldDefinitionTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 94
rs 10
c 0
b 0
f 0
wmc 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Publications\Testing\Feature;
6
7
use ValueError;
8
use Hyde\Testing\TestCase;
9
use Hyde\Publications\Concerns\PublicationFieldTypes;
10
use Hyde\Publications\Models\PublicationFieldDefinition;
11
12
/**
13
 * @covers \Hyde\Publications\Models\PublicationFieldDefinition
14
 */
15
class PublicationFieldDefinitionTest extends TestCase
16
{
17
    public function test_can_instantiate_class()
18
    {
19
        $field = new PublicationFieldDefinition('string', 'test');
20
        $this->assertInstanceOf(PublicationFieldDefinition::class, $field);
21
22
        $this->assertSame(PublicationFieldTypes::String, $field->type);
23
        $this->assertSame('test', $field->name);
24
    }
25
26
    public function test_from_array_method()
27
    {
28
        $field = PublicationFieldDefinition::fromArray([
29
            'type' => 'string',
30
            'name' => 'test',
31
        ]);
32
33
        $this->assertInstanceOf(PublicationFieldDefinition::class, $field);
34
35
        $this->assertSame(PublicationFieldTypes::String, $field->type);
36
        $this->assertSame('test', $field->name);
37
    }
38
39
    public function test_can_get_field_as_array()
40
    {
41
        $this->assertSame([
42
            'type' => 'string',
43
            'name' => 'test',
44
        ], (new PublicationFieldDefinition('string', 'test'))->toArray());
45
    }
46
47
    public function test_can_get_field_with_optional_properties_as_array()
48
    {
49
        $this->assertSame([
50
            'type' => 'string',
51
            'name' => 'test',
52
            'rules' => ['required'],
53
        ], (new PublicationFieldDefinition('string', 'test', ['required']))->toArray());
54
    }
55
56
    public function test_can_encode_field_as_json()
57
    {
58
        $this->assertSame('{"type":"string","name":"test"}', json_encode(new PublicationFieldDefinition('string', 'test')));
59
    }
60
61
    public function test_can_get_field_with_optional_properties_as_json()
62
    {
63
        $this->assertSame('{"type":"string","name":"test","rules":["required"]}', json_encode(new PublicationFieldDefinition('string',
64
            'test',
65
            ['required']
66
        )));
67
    }
68
69
    public function test_can_construct_type_using_enum_case()
70
    {
71
        $field1 = new PublicationFieldDefinition(PublicationFieldTypes::String, 'test');
72
        $this->assertSame(PublicationFieldTypes::String, $field1->type);
73
74
        $field2 = new PublicationFieldDefinition('string', 'test');
75
        $this->assertSame(PublicationFieldTypes::String, $field2->type);
76
77
        $this->assertEquals($field1, $field2);
78
    }
79
80
    public function test_type_must_be_valid()
81
    {
82
        $this->expectException(ValueError::class);
83
84
        new PublicationFieldDefinition('invalid', 'test');
85
    }
86
87
    public function test_type_input_is_case_insensitive()
88
    {
89
        $field = new PublicationFieldDefinition('STRING', 'test');
90
        $this->assertSame(PublicationFieldTypes::String, $field->type);
91
    }
92
93
    public function test_name_gets_stored_as_kebab_case()
94
    {
95
        $field = new PublicationFieldDefinition('string', 'Test Field');
96
        $this->assertSame('test-field', $field->name);
97
    }
98
99
    public function test_get_rules()
100
    {
101
        $field = new PublicationFieldDefinition('string', 'test');
102
        $this->assertSame(['string'], $field->getRules());
103
    }
104
105
    public function test_get_rules_with_custom_type_rules()
106
    {
107
        $field = new PublicationFieldDefinition('string', 'test', ['required', 'foo']);
108
        $this->assertSame(['string', 'required', 'foo'], $field->getRules());
109
    }
110
}
111