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