Completed
Push — master ( ae47f3...33ec04 )
by Bob Olde
11s
created

AssetTransformTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 126
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A _before() 0 4 1
A testGetRecordDefinition() 0 6 1
A testSaveRecord() 0 11 1
A testDeleteRecord() 0 8 1
A provideAssetTransforms() 0 11 1
A getMockAssetTransform() 0 8 1
A getMockAssetTransformDefinition() 0 17 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Models;
4
5
use Craft;
6
use craft\models\AssetTransform as AssetTransformModel;
7
use Codeception\Test\Unit;
8
9
/**
10
 * Class AssetTransformTest.
11
 *
12
 * @author    Nerds & Company
13
 * @copyright Copyright (c) 2015-2017, Nerds & Company
14
 * @license   MIT
15
 *
16
 * @see      http://www.nerds.company
17
 */
18
class AssetTransformTest extends Unit
19
{
20
    /**
21
     * @var AssetTransforms
22
     */
23
    private $converter;
24
25
    /**
26
     * Set the converter.
27
     *
28
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
29
     */
30
    protected function _before()
31
    {
32
        $this->converter = new AssetTransform();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \NerdsAndCompany\Sch...Models\AssetTransform() of type object<NerdsAndCompany\S...\Models\AssetTransform> is incompatible with the declared type object<NerdsAndCompany\S...Models\AssetTransforms> of property $converter.

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...
33
    }
34
35
    //==============================================================================================================
36
    //=================================================  TESTS  ====================================================
37
    //==============================================================================================================
38
39
    /**
40
     * @dataProvider provideAssetTransforms
41
     *
42
     * @param AssetTransformModel $transform
43
     * @param array               $definition
44
     */
45
    public function testGetRecordDefinition(AssetTransformModel $transform, array $definition)
46
    {
47
        $result = $this->converter->getRecordDefinition($transform);
48
49
        $this->assertSame($definition, $result);
50
    }
51
52
    /**
53
     * @dataProvider provideAssetTransforms
54
     *
55
     * @param AssetTransformModel $transform
56
     * @param array               $definition
57
     */
58
    public function testSaveRecord(AssetTransformModel $transform, array $definition)
59
    {
60
        Craft::$app->assetTransforms->expects($this->exactly(1))
61
                                    ->method('saveTransform')
62
                                    ->with($transform)
63
                                    ->willReturn(true);
64
65
        $result = $this->converter->saveRecord($transform, $definition);
66
67
        $this->assertTrue($result);
68
    }
69
70
    /**
71
     * @dataProvider provideAssetTransforms
72
     *
73
     * @param AssetTransformModel $transform
74
     */
75
    public function testDeleteRecord(AssetTransformModel $transform)
76
    {
77
        Craft::$app->assetTransforms->expects($this->exactly(1))
78
                                    ->method('deleteTransform')
79
                                    ->with($transform->id);
80
81
        $this->converter->deleteRecord($transform);
82
    }
83
84
    //==============================================================================================================
85
    //==============================================  PROVIDERS  ===================================================
86
    //==============================================================================================================
87
88
    /**
89
     * @return array
90
     */
91
    public function provideAssetTransforms()
92
    {
93
        $mockAssetTransform = $this->getMockAssetTransform(1);
94
95
        return [
96
            'valid transform existing group' => [
97
                'transform' => $mockAssetTransform,
98
                'definition' => $this->getMockAssetTransformDefinition($mockAssetTransform),
99
            ],
100
        ];
101
    }
102
103
    //==============================================================================================================
104
    //================================================  HELPERS  ===================================================
105
    //==============================================================================================================
106
107
    /**
108
     * @param int $assetTransformId
109
     *
110
     * @return AssetTransformModel
111
     */
112
    private function getMockAssetTransform(int $assetTransformId)
113
    {
114
        return new AssetTransformModel([
115
            'id' => $assetTransformId,
116
            'handle' => 'assetTransformHandle'.$assetTransformId,
117
            'name' => 'assetTransformName'.$assetTransformId,
118
        ]);
119
    }
120
121
    /**
122
     * @param AssetTransformModel $assetTransform
123
     *
124
     * @return array
125
     */
126
    private function getMockAssetTransformDefinition(AssetTransformModel $assetTransform)
127
    {
128
        return [
129
          'class' => get_class($assetTransform),
130
          'attributes' => [
131
              'name' => 'assetTransformName'.$assetTransform->id,
132
              'handle' => 'assetTransformHandle'.$assetTransform->id,
133
              'width' => null,
134
              'height' => null,
135
              'format' => null,
136
              'mode' => 'crop',
137
              'position' => 'center-center',
138
              'interlace' => 'none',
139
              'quality' => null,
140
          ],
141
        ];
142
    }
143
}
144