|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Converters\Base; |
|
4
|
|
|
|
|
5
|
|
|
use Craft; |
|
6
|
|
|
use craft\base\Field as FieldModel; |
|
7
|
|
|
use craft\base\Volume as VolumeModel; |
|
8
|
|
|
use craft\models\FieldLayout; |
|
9
|
|
|
use craft\volumes\Local; |
|
10
|
|
|
use Codeception\Test\Unit; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class VolumeTest. |
|
14
|
|
|
* |
|
15
|
|
|
* @author Nerds & Company |
|
16
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
|
17
|
|
|
* @license MIT |
|
18
|
|
|
* |
|
19
|
|
|
* @see http://www.nerds.company |
|
20
|
|
|
*/ |
|
21
|
|
|
class VolumeTest extends Unit |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var Volumes |
|
25
|
|
|
*/ |
|
26
|
|
|
private $converter; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Set the converter. |
|
30
|
|
|
* |
|
31
|
|
|
* @SuppressWarnings(PHPMD.CamelCaseMethodName) |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function _before() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->converter = new Volume(); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
//============================================================================================================== |
|
39
|
|
|
//================================================= TESTS ==================================================== |
|
40
|
|
|
//============================================================================================================== |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @dataProvider provideVolumes |
|
44
|
|
|
* |
|
45
|
|
|
* @param VolumeModel $volume |
|
46
|
|
|
* @param array $definition |
|
47
|
|
|
*/ |
|
48
|
|
|
public function testGetRecordDefinition(VolumeModel $volume, array $definition) |
|
49
|
|
|
{ |
|
50
|
|
|
$result = $this->converter->getRecordDefinition($volume); |
|
51
|
|
|
|
|
52
|
|
|
$this->assertSame($definition, $result); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @dataProvider provideVolumes |
|
57
|
|
|
* |
|
58
|
|
|
* @param VolumeModel $volume |
|
59
|
|
|
* @param array $definition |
|
60
|
|
|
*/ |
|
61
|
|
|
public function testSaveRecord(VolumeModel $volume, array $definition) |
|
62
|
|
|
{ |
|
63
|
|
|
Craft::$app->volumes->expects($this->exactly(1)) |
|
64
|
|
|
->method('saveVolume') |
|
65
|
|
|
->with($volume) |
|
66
|
|
|
->willReturn(true); |
|
67
|
|
|
|
|
68
|
|
|
$result = $this->converter->saveRecord($volume, $definition); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertTrue($result); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @dataProvider provideVolumes |
|
75
|
|
|
* |
|
76
|
|
|
* @param VolumeModel $volume |
|
77
|
|
|
*/ |
|
78
|
|
|
public function testDeleteRecord(VolumeModel $volume) |
|
79
|
|
|
{ |
|
80
|
|
|
Craft::$app->volumes->expects($this->exactly(1)) |
|
81
|
|
|
->method('deleteVolume') |
|
82
|
|
|
->with($volume); |
|
83
|
|
|
|
|
84
|
|
|
$this->converter->deleteRecord($volume); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
//============================================================================================================== |
|
88
|
|
|
//============================================== PROVIDERS =================================================== |
|
89
|
|
|
//============================================================================================================== |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
public function provideVolumes() |
|
95
|
|
|
{ |
|
96
|
|
|
$mockVolume = $this->getMockVolume(1); |
|
97
|
|
|
|
|
98
|
|
|
return [ |
|
99
|
|
|
'local volume' => [ |
|
100
|
|
|
'volume' => $mockVolume, |
|
101
|
|
|
'definition' => $this->getMockVolumeDefinition($mockVolume), |
|
|
|
|
|
|
102
|
|
|
], |
|
103
|
|
|
]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
//============================================================================================================== |
|
107
|
|
|
//================================================ HELPERS =================================================== |
|
108
|
|
|
//============================================================================================================== |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param int $volumeId |
|
112
|
|
|
* |
|
113
|
|
|
* @return Mock|VolumeModel |
|
114
|
|
|
*/ |
|
115
|
|
|
private function getMockVolume(int $volumeId) |
|
116
|
|
|
{ |
|
117
|
|
|
$mockVolume = $this->getMockBuilder(Local::class) |
|
118
|
|
|
->setMethods(['getFieldLayout']) |
|
119
|
|
|
->disableOriginalConstructor() |
|
120
|
|
|
->getMock(); |
|
121
|
|
|
|
|
122
|
|
|
$mockVolume->id = $volumeId; |
|
|
|
|
|
|
123
|
|
|
$mockVolume->fieldLayoutId = $volumeId; |
|
|
|
|
|
|
124
|
|
|
$mockVolume->handle = 'volumeHandle'.$volumeId; |
|
|
|
|
|
|
125
|
|
|
$mockVolume->name = 'volumeName'.$volumeId; |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
$mockField = $this->getMockbuilder(FieldModel::class)->getMock(); |
|
128
|
|
|
$mockField->id = $volumeId; |
|
|
|
|
|
|
129
|
|
|
$mockField->handle = 'field'.$volumeId; |
|
|
|
|
|
|
130
|
|
|
$mockField->required = true; |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
$mockFieldLayout = $this->getMockBuilder(FieldLayout::class)->getMock(); |
|
133
|
|
|
|
|
134
|
|
|
$mockFieldLayout->expects($this->any()) |
|
135
|
|
|
->method('getFields') |
|
136
|
|
|
->willReturn([$mockField]); |
|
137
|
|
|
|
|
138
|
|
|
$mockVolume->expects($this->any()) |
|
139
|
|
|
->method('getFieldLayout') |
|
140
|
|
|
->willReturn($mockFieldLayout); |
|
141
|
|
|
|
|
142
|
|
|
return $mockVolume; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param VolumeModel $mockVolume |
|
147
|
|
|
* |
|
148
|
|
|
* @return array |
|
149
|
|
|
*/ |
|
150
|
|
|
private function getMockVolumeDefinition(VolumeModel $mockVolume) |
|
151
|
|
|
{ |
|
152
|
|
|
$fieldDefs = []; |
|
153
|
|
|
foreach ($mockVolume->getFieldLayout()->getFields() as $field) { |
|
154
|
|
|
$fieldDefs[$field->handle] = $field->required; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return [ |
|
158
|
|
|
'class' => get_class($mockVolume), |
|
159
|
|
|
'attributes' => [ |
|
160
|
|
|
'path' => null, |
|
161
|
|
|
'name' => 'volumeName'.$mockVolume->id, |
|
162
|
|
|
'handle' => 'volumeHandle'.$mockVolume->id, |
|
163
|
|
|
'hasUrls' => null, |
|
164
|
|
|
'url' => null, |
|
165
|
|
|
'sortOrder' => null, |
|
166
|
|
|
], |
|
167
|
|
|
'fieldLayout' => [ |
|
168
|
|
|
'fields' => $fieldDefs, |
|
169
|
|
|
], |
|
170
|
|
|
]; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..