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
|
|
|
* phpcs:disable PSR2.Methods.MethodDeclaration.Underscore |
34
|
|
|
*/ |
35
|
|
|
protected function _before() |
36
|
|
|
{ |
37
|
|
|
$this->module = new Schematic('schematic'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
//============================================================================================================== |
41
|
|
|
//================================================= TESTS ==================================================== |
42
|
|
|
//============================================================================================================== |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @dataProvider provideDataTypes |
46
|
|
|
* |
47
|
|
|
* @param string $dataTypeHandle |
48
|
|
|
* @param bool $valid |
49
|
|
|
* @param string $dataTypeClass |
50
|
|
|
*/ |
51
|
|
|
public function testGetDataType(string $dataTypeHandle, bool $valid, string $dataTypeClass) |
52
|
|
|
{ |
53
|
|
|
if ($dataTypeClass) { |
54
|
|
|
$this->module->dataTypes[$dataTypeHandle] = $dataTypeClass; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$result = $this->module->getDataType($dataTypeHandle); |
58
|
|
|
|
59
|
|
|
if ($valid) { |
60
|
|
|
$this->assertInstanceOf(DataTypeInterface::class, $result); |
61
|
|
|
} else { |
62
|
|
|
$this->assertNull($result); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @dataProvider provideMappers |
68
|
|
|
* |
69
|
|
|
* @param string $mapperHandle |
70
|
|
|
* @param bool $valid |
71
|
|
|
* @param string $mapperClass |
72
|
|
|
*/ |
73
|
|
|
public function testCheckMapper(string $mapperHandle, bool $valid, string $mapperClass) |
74
|
|
|
{ |
75
|
|
|
if ($mapperClass) { |
76
|
|
|
$this->module->setComponents([ |
77
|
|
|
$mapperHandle => [ |
78
|
|
|
'class' => $mapperClass, |
79
|
|
|
], |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$result = $this->module->checkMapper($mapperHandle); |
84
|
|
|
|
85
|
|
|
$this->assertSame($valid, $result); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @dataProvider provideConverters |
90
|
|
|
* |
91
|
|
|
* @param string $modelClass |
92
|
|
|
* @param bool $valid |
93
|
|
|
* @param string $converterClass |
94
|
|
|
*/ |
95
|
|
|
public function testGetConverter(string $modelClass, bool $valid, string $converterClass) |
96
|
|
|
{ |
97
|
|
|
$result = $this->module->getConverter($modelClass); |
98
|
|
|
|
99
|
|
|
if ($valid) { |
100
|
|
|
$this->assertInstanceOf($converterClass, $result); |
101
|
|
|
} else { |
102
|
|
|
$this->assertNull($result); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
//============================================================================================================== |
107
|
|
|
//============================================== PROVIDERS =================================================== |
108
|
|
|
//============================================================================================================== |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
public function provideDataTypes() |
114
|
|
|
{ |
115
|
|
|
return [ |
116
|
|
|
'existing dataType' => [ |
117
|
|
|
'dataTypeHandle' => 'sections', |
118
|
|
|
'valid' => true, |
119
|
|
|
'dataTypeClass' => '', |
120
|
|
|
], |
121
|
|
|
'dataType not registerd' => [ |
122
|
|
|
'dataTypeHandle' => 'unregistered', |
123
|
|
|
'valid' => false, |
124
|
|
|
'dataTypeClass' => '', |
125
|
|
|
], |
126
|
|
|
'dataTypeClass does not exist' => [ |
127
|
|
|
'dataTypeHandle' => 'notExists', |
128
|
|
|
'valid' => false, |
129
|
|
|
'dataTypeClass' => 'NotExists', |
130
|
|
|
], |
131
|
|
|
'dataTypeClass does not implement interface' => [ |
132
|
|
|
'dataTypeHandle' => 'implements', |
133
|
|
|
'valid' => false, |
134
|
|
|
'dataTypeClass' => \stdClass::class, |
135
|
|
|
], |
136
|
|
|
]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
public function provideMappers() |
143
|
|
|
{ |
144
|
|
|
return [ |
145
|
|
|
'existing mapper' => [ |
146
|
|
|
'mapperHandle' => 'modelMapper', |
147
|
|
|
'valid' => true, |
148
|
|
|
'mapper' => '', |
149
|
|
|
], |
150
|
|
|
'mapper not registerd' => [ |
151
|
|
|
'mapperHandle' => 'unregistered', |
152
|
|
|
'valid' => false, |
153
|
|
|
'mapper' => '', |
154
|
|
|
], |
155
|
|
|
'mapper does not implement interface' => [ |
156
|
|
|
'mapperHandle' => 'fakeMapper', |
157
|
|
|
'valid' => false, |
158
|
|
|
'mapper' => \stdClass::class, |
159
|
|
|
], |
160
|
|
|
]; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
public function provideConverters() |
167
|
|
|
{ |
168
|
|
|
return [ |
169
|
|
|
'direct match' => [ |
170
|
|
|
'modelClass' => Section::class, |
171
|
|
|
'valid' => true, |
172
|
|
|
'converterClass' => SectionConverter::class, |
173
|
|
|
], |
174
|
|
|
'parent match' => [ |
175
|
|
|
'modelClass' => PlainText::class, |
176
|
|
|
'valid' => true, |
177
|
|
|
'converterClass' => FieldConverter::class, |
178
|
|
|
], |
179
|
|
|
'no match' => [ |
180
|
|
|
'modelClass' => Element::class, |
181
|
|
|
'valid' => false, |
182
|
|
|
'converterClass' => '', |
183
|
|
|
], |
184
|
|
|
]; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|