Completed
Pull Request — master (#114)
by Bart
04:32
created

SchematicTest::testGetDataType()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 3
1
<?php
2
3
namespace NerdsAndCompany\Schematic;
4
5
use craft\base\Element;
6
use craft\fields\PlainText;
7
use craft\models\Section;
8
use Codeception\Test\Unit;
9
use NerdsAndCompany\Schematic\Converters\Base\Field as FieldConverter;
10
use NerdsAndCompany\Schematic\Converters\Models\Section as SectionConverter;
11
use NerdsAndCompany\Schematic\Interfaces\DataTypeInterface;
12
13
/**
14
 * Class SchematicTest.
15
 *
16
 * @author    Nerds & Company
17
 * @copyright Copyright (c) 2015-2018, Nerds & Company
18
 * @license   MIT
19
 *
20
 * @see      http://www.nerds.company
21
 */
22
class SchematicTest extends Unit
23
{
24
    /**
25
     * @var Schematic
26
     */
27
    private $module;
28
29
    /**
30
     * Set the mapper.
31
     *
32
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
33
     */
34
    protected function _before()
35
    {
36
        $this->module = new Schematic('schematic');
37
    }
38
39
    //==============================================================================================================
40
    //=================================================  TESTS  ====================================================
41
    //==============================================================================================================
42
43
    /**
44
     * @dataProvider provideDataTypes
45
     *
46
     * @param string $dataTypeHandle
47
     * @param bool   $valid
48
     * @param string $dataTypeClass
49
     */
50
    public function testGetDataType(string $dataTypeHandle, bool $valid, string $dataTypeClass)
51
    {
52
        if ($dataTypeClass) {
53
            $this->module->dataTypes[$dataTypeHandle] = $dataTypeClass;
54
        }
55
56
        $result = $this->module->getDataType($dataTypeHandle);
57
58
        if ($valid) {
59
            $this->assertInstanceOf(DataTypeInterface::class, $result);
60
        } else {
61
            $this->assertNull($result);
62
        }
63
    }
64
65
    /**
66
     * @dataProvider provideMappers
67
     *
68
     * @param string $mapperHandle
69
     * @param bool   $valid
70
     * @param string $mapperClass
71
     */
72
    public function testCheckMapper(string $mapperHandle, bool $valid, string $mapperClass)
73
    {
74
        if ($mapperClass) {
75
            $this->module->setComponents([
76
                $mapperHandle => [
77
                    'class' => $mapperClass,
78
                ],
79
            ]);
80
        }
81
82
        $result = $this->module->checkMapper($mapperHandle);
83
84
        $this->assertSame($valid, $result);
85
    }
86
87
    /**
88
     * @dataProvider provideConverters
89
     *
90
     * @param string $modelClass
91
     * @param bool   $valid
92
     * @param string $converterClass
93
     */
94
    public function testGetConverter(string $modelClass, bool $valid, string $converterClass)
95
    {
96
        $result = $this->module->getConverter($modelClass);
97
98
        if ($valid) {
99
            $this->assertInstanceOf($converterClass, $result);
100
        } else {
101
            $this->assertNull($result);
102
        }
103
    }
104
105
    //==============================================================================================================
106
    //==============================================  PROVIDERS  ===================================================
107
    //==============================================================================================================
108
109
    /**
110
     * @return array
111
     */
112
    public function provideDataTypes()
113
    {
114
        return [
115
            'existing dataType' => [
116
                'dataTypeHandle' => 'sections',
117
                'valid' => true,
118
                'dataTypeClass' => '',
119
            ],
120
            'dataType not registerd' => [
121
                'dataTypeHandle' => 'unregistered',
122
                'valid' => false,
123
                'dataTypeClass' => '',
124
            ],
125
            'dataTypeClass does not exist' => [
126
                'dataTypeHandle' => 'notExists',
127
                'valid' => false,
128
                'dataTypeClass' => 'NotExists',
129
            ],
130
            'dataTypeClass does not implement interface' => [
131
                'dataTypeHandle' => 'implements',
132
                'valid' => false,
133
                'dataTypeClass' => \stdClass::class,
134
            ],
135
        ];
136
    }
137
138
    /**
139
     * @return array
140
     */
141
    public function provideMappers()
142
    {
143
        return [
144
            'existing mapper' => [
145
                'mapperHandle' => 'modelMapper',
146
                'valid' => true,
147
                'mapper' => '',
148
            ],
149
            'mapper not registerd' => [
150
                'mapperHandle' => 'unregistered',
151
                'valid' => false,
152
                'mapper' => '',
153
            ],
154
            'mapper does not implement interface' => [
155
                'mapperHandle' => 'fakeMapper',
156
                'valid' => false,
157
                'mapper' => \stdClass::class,
158
            ],
159
        ];
160
    }
161
162
    /**
163
     * @return array
164
     */
165
    public function provideConverters()
166
    {
167
        return [
168
            'direct match' => [
169
                'modelClass' => Section::class,
170
                'valid' => true,
171
                'converterClass' => SectionConverter::class,
172
            ],
173
            'parent match' => [
174
                'modelClass' => PlainText::class,
175
                'valid' => true,
176
                'converterClass' => FieldConverter::class,
177
            ],
178
            'no match' => [
179
                'modelClass' => Element::class,
180
                'valid' => false,
181
                'converterClass' => '',
182
            ],
183
        ];
184
    }
185
}
186