Completed
Push — do-not-scrutinize-tests ( fcf005 )
by Bart
04:15 queued 02:38
created

VolumeTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 2
cbo 6
dl 0
loc 153
rs 10
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 Volume
25
     */
26
    private $converter;
27
28
    /**
29
     * Set the converter.
30
     *
31
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
32
     * phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
33
     */
34
    protected function _before()
35
    {
36
        $this->converter = new Volume();
37
    }
38
39
    //==============================================================================================================
40
    //=================================================  TESTS  ====================================================
41
    //==============================================================================================================
42
43
    /**
44
     * @dataProvider provideVolumes
45
     *
46
     * @param VolumeModel $volume
47
     * @param array       $definition
48
     */
49
    public function testGetRecordDefinition(VolumeModel $volume, array $definition)
50
    {
51
        $result = $this->converter->getRecordDefinition($volume);
52
53
        $this->assertSame($definition, $result);
54
    }
55
56
    /**
57
     * @dataProvider provideVolumes
58
     *
59
     * @param VolumeModel $volume
60
     * @param array       $definition
61
     */
62
    public function testSaveRecord(VolumeModel $volume, array $definition)
63
    {
64
        Craft::$app->volumes->expects($this->exactly(1))
65
                            ->method('saveVolume')
66
                            ->with($volume)
67
                            ->willReturn(true);
68
69
        $result = $this->converter->saveRecord($volume, $definition);
70
71
        $this->assertTrue($result);
72
    }
73
74
    /**
75
     * @dataProvider provideVolumes
76
     *
77
     * @param VolumeModel $volume
78
     */
79
    public function testDeleteRecord(VolumeModel $volume)
80
    {
81
        Craft::$app->volumes->expects($this->exactly(1))
82
                            ->method('deleteVolume')
83
                            ->with($volume);
84
85
        $this->converter->deleteRecord($volume);
86
    }
87
88
    //==============================================================================================================
89
    //==============================================  PROVIDERS  ===================================================
90
    //==============================================================================================================
91
92
    /**
93
     * @return array
94
     */
95
    public function provideVolumes()
96
    {
97
        $mockVolume = $this->getMockVolume(1);
98
99
        return [
100
            'local volume' => [
101
                'volume' => $mockVolume,
102
                'definition' => $this->getMockVolumeDefinition($mockVolume),
103
            ],
104
        ];
105
    }
106
107
    //==============================================================================================================
108
    //================================================  HELPERS  ===================================================
109
    //==============================================================================================================
110
111
    /**
112
     * @param int $volumeId
113
     *
114
     * @return Mock|VolumeModel
115
     */
116
    private function getMockVolume(int $volumeId)
117
    {
118
        $mockVolume = $this->getMockBuilder(Local::class)
119
                           ->setMethods(['getFieldLayout'])
120
                           ->disableOriginalConstructor()
121
                           ->getMock();
122
123
        $mockVolume->id = $volumeId;
124
        $mockVolume->fieldLayoutId = $volumeId;
125
        $mockVolume->handle = 'volumeHandle'.$volumeId;
126
        $mockVolume->name = 'volumeName'.$volumeId;
127
128
        $mockField = $this->getMockbuilder(FieldModel::class)->getMock();
129
        $mockField->id = $volumeId;
130
        $mockField->handle = 'field'.$volumeId;
131
        $mockField->required = true;
132
133
        $mockFieldLayout = $this->getMockBuilder(FieldLayout::class)->getMock();
134
135
        $mockFieldLayout->expects($this->any())
136
                        ->method('getFields')
137
                        ->willReturn([$mockField]);
138
139
        $mockVolume->expects($this->any())
140
                   ->method('getFieldLayout')
141
                   ->willReturn($mockFieldLayout);
142
143
        return $mockVolume;
144
    }
145
146
    /**
147
     * @param VolumeModel $mockVolume
148
     *
149
     * @return array
150
     */
151
    private function getMockVolumeDefinition(VolumeModel $mockVolume)
152
    {
153
        $fieldDefs = [];
154
        foreach ($mockVolume->getFieldLayout()->getFields() as $field) {
155
            $fieldDefs[$field->handle] = $field->required;
156
        }
157
158
        return [
159
            'class' => get_class($mockVolume),
160
            'attributes' => [
161
                'path' => null,
162
                'name' => 'volumeName'.$mockVolume->id,
163
                'handle' => 'volumeHandle'.$mockVolume->id,
164
                'hasUrls' => null,
165
                'url' => null,
166
                'sortOrder' => null,
167
            ],
168
            'fieldLayout' => [
169
                'fields' => $fieldDefs,
170
            ],
171
        ];
172
    }
173
}
174