Completed
Pull Request — master (#114)
by Bart
09:25
created

provideValidAssetTransformDefinitions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft;
6
use craft\models\AssetTransform;
7
use Codeception\Test\Unit;
8
use NerdsAndCompany\Schematic\Schematic;
9
10
/**
11
 * Class AssetTransformsTest.
12
 *
13
 * @author    Nerds & Company
14
 * @copyright Copyright (c) 2015-2018, Nerds & Company
15
 * @license   MIT
16
 *
17
 * @see      http://www.nerds.company
18
 */
19
class ModelProcessorTest extends Unit
20
{
21
    /**
22
     * @var AssetTransforms
23
     */
24
    private $service;
25
26
    /**
27
     * Set the service.
28
     *
29
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
30
     */
31
    protected function _before()
32
    {
33
        Schematic::$force = false;
34
35
        $this->service = new ModelProcessor();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \NerdsAndCompany\Sch...rvices\ModelProcessor() of type object<NerdsAndCompany\S...ervices\ModelProcessor> is incompatible with the declared type object<NerdsAndCompany\S...rvices\AssetTransforms> of property $service.

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..

Loading history...
36
    }
37
38
    //==============================================================================================================
39
    //=================================================  TESTS  ====================================================
40
    //==============================================================================================================
41
42
    /**
43
     * @dataProvider provideValidAssetTransforms
44
     *
45
     * @param AssetTransform[] $assetTransforms
46
     * @param array            $expectedResult
47
     */
48
    public function testSuccessfulExport(array $assetTransforms, array $expectedResult = [])
49
    {
50
        $actualResult = $this->service->export($assetTransforms);
51
52
        $this->assertSame($expectedResult, $actualResult);
53
    }
54
55
    /**
56
     * @dataProvider provideValidAssetTransformDefinitions
57
     *
58
     * @param array            $assetTransformDefinitions
59
     * @param AssetTransform[] $existingTransforms
60
     * @param int              $saveCount
61
     */
62
    public function testSuccessfulImport(array $assetTransformDefinitions, array $existingTransforms, int $saveCount)
63
    {
64
        $this->expectSaves($saveCount);
65
        $this->expectDeletes(0);
66
67
        $this->service->import($assetTransformDefinitions, $existingTransforms);
68
    }
69
70
    /**
71
     * @dataProvider provideValidAssetTransformDefinitions
72
     *
73
     * @param array            $assetTransformDefinitions
74
     * @param AssetTransform[] $existingTransforms
75
     * @param int              $saveCount
76
     * @param int              $deleteCount
77
     */
78
    public function testImportWithForceOption(array $assetTransformDefinitions, array $existingTransforms, int $saveCount, int $deleteCount)
79
    {
80
        Schematic::$force = true;
81
        $this->expectSaves($saveCount);
82
        $this->expectDeletes($deleteCount);
83
84
        $this->service->import($assetTransformDefinitions, $existingTransforms);
85
    }
86
87
    //==============================================================================================================
88
    //==============================================  PROVIDERS  ===================================================
89
    //==============================================================================================================
90
91
    /**
92
     * @return array
93
     */
94
    public function provideValidAssetTransforms()
95
    {
96
        return [
97
            'emptyArray' => [
98
                'assetTransforms' => [],
99
                'expectedResult' => [],
100
            ],
101
            'single asset transform' => [
102
                'assetTransforms' => [
103
                    $this->getAssetTransform(1),
104
                ],
105
                'expectedResult' => [
106
                    'assetTransformHandle1' => $this->getAssetTransformDefinition(1),
107
                ],
108
            ],
109
            'multiple asset transforms' => [
110
                'assetTransforms' => [
111
                    $this->getAssetTransform(1),
112
                    $this->getAssetTransform(2),
113
                ],
114
                'expectedResult' => [
115
                    'assetTransformHandle1' => $this->getAssetTransformDefinition(1),
116
                    'assetTransformHandle2' => $this->getAssetTransformDefinition(2),
117
                ],
118
            ],
119
        ];
120
    }
121
122
    /**
123
     * @return array
124
     */
125
    public function provideValidAssetTransformDefinitions()
126
    {
127
        return [
128
            'emptyArray' => [
129
                'assetTransformDefinitions' => [],
130
                'existingTransforms' => [
131
                    $this->getAssetTransform(1),
132
                ],
133
                'saveCount' => 0,
134
                'deleteCount' => 1,
135
            ],
136
            'single new group' => [
137
                'assetTransformDefinitions' => [
138
                    'assetTransformHandle1' => $this->getAssetTransformDefinition(1),
139
                    'assetTransformHandle2' => $this->getAssetTransformDefinition(2),
140
                ],
141
                'existingTransforms' => [
142
                    $this->getAssetTransform(1),
143
                ],
144
                'saveCount' => 1,
145
                'deleteCount' => 0,
146
            ],
147
        ];
148
    }
149
150
    //==============================================================================================================
151
    //================================================  HELPERS  ===================================================
152
    //==============================================================================================================
153
154
    /**
155
     * @param int $assetTransformId
156
     *
157
     * @return AssetTransform
158
     */
159
    private function getAssetTransform(int $assetTransformId)
160
    {
161
        return new AssetTransform([
162
            'id' => $assetTransformId,
163
            'handle' => 'assetTransformHandle'.$assetTransformId,
164
            'name' => 'assetTransformName'.$assetTransformId,
165
        ]);
166
    }
167
168
    /**
169
     * @param int $assetTransformId
170
     *
171
     * @return array
172
     */
173
    private function getAssetTransformDefinition(int $assetTransformId)
174
    {
175
        return [
176
          'class' => 'craft\models\AssetTransform',
177
          'attributes' => [
178
              'name' => 'assetTransformName'.$assetTransformId,
179
              'handle' => 'assetTransformHandle'.$assetTransformId,
180
              'width' => null,
181
              'height' => null,
182
              'format' => null,
183
              'mode' => 'crop',
184
              'position' => 'center-center',
185
              'interlace' => 'none',
186
              'quality' => null,
187
          ],
188
        ];
189
    }
190
191
    /**
192
     * Expect a number of transform saves.
193
     *
194
     * @param int $saveCount
195
     */
196
    private function expectSaves(int $saveCount)
197
    {
198
        Craft::$app->assetTransforms
199
                   ->expects($this->exactly($saveCount))
200
                   ->method('saveTransform')
201
                   ->willReturn(true);
202
    }
203
204
    /**
205
     * Expect a number of transform deletes.
206
     *
207
     * @param int $deleteCount
208
     */
209
    private function expectDeletes(int $deleteCount)
210
    {
211
        Craft::$app->assetTransforms
212
                    ->expects($this->exactly($deleteCount))
213
                    ->method('deleteTransform');
214
    }
215
}
216