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

CategoryGroupTest::getMockCategoryGroup()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Models;
4
5
use Craft;
6
use craft\models\CategoryGroup as CategoryGroupModel;
7
use craft\models\CategoryGroup_SiteSettings;
8
use craft\models\FieldLayout;
9
use craft\models\Site;
10
use Codeception\Test\Unit;
11
12
/**
13
 * Class CategoryGroupTest.
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 CategoryGroupTest extends Unit
22
{
23
    /**
24
     * @var CategoryGroup
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 CategoryGroup();
37
    }
38
39
    //==============================================================================================================
40
    //=================================================  TESTS  ====================================================
41
    //==============================================================================================================
42
43
    /**
44
     * @dataProvider provideCategoryGroups
45
     *
46
     * @param CategoryGroupModel $group
47
     * @param array              $definition
48
     */
49
    public function testGetRecordDefinition(CategoryGroupModel $group, array $definition)
50
    {
51
        $result = $this->converter->getRecordDefinition($group);
52
53
        $this->assertSame($definition, $result);
54
    }
55
56
    /**
57
     * @dataProvider provideCategoryGroups
58
     *
59
     * @param CategoryGroupModel $group
60
     * @param array              $definition
61
     * @param Site|null          $site
62
     */
63
    public function testSetRecordAttributes(CategoryGroupModel $group, array $definition, $site)
64
    {
65
        $newGroup = $this->getMockBuilder(CategoryGroupModel::class)
66
                         ->setMethods(['setSiteSettings'])
67
                         ->getMock();
68
69
        $newGroup->expects($this->exactly(1))
70
                 ->method('setSiteSettings');
71
72
        Craft::$app->sites->expects($this->any())
73
                           ->method('getSiteByHandle')
74
                           ->willReturn($site);
75
76
        $this->converter->setRecordAttributes($newGroup, $definition, []);
77
78
        $this->assertSame($group->name, $newGroup->name);
79
        $this->assertSame($group->handle, $newGroup->handle);
80
    }
81
82
    /**
83
     * @dataProvider provideCategoryGroups
84
     *
85
     * @param CategoryGroupModel $group
86
     * @param array              $definition
87
     */
88
    public function testSaveRecord(CategoryGroupModel $group, array $definition)
89
    {
90
        Craft::$app->categories->expects($this->exactly(1))
91
                               ->method('saveGroup')
92
                               ->with($group)
93
                               ->willReturn(true);
94
95
        $result = $this->converter->saveRecord($group, $definition);
96
97
        $this->assertTrue($result);
98
    }
99
100
    /**
101
     * @dataProvider provideCategoryGroups
102
     *
103
     * @param CategoryGroupModel $group
104
     */
105
    public function testDeleteRecord(CategoryGroupModel $group)
106
    {
107
        Craft::$app->categories->expects($this->exactly(1))
108
                               ->method('deleteGroupById')
109
                               ->with($group->id);
110
111
        $this->converter->deleteRecord($group);
112
    }
113
114
    //==============================================================================================================
115
    //==============================================  PROVIDERS  ===================================================
116
    //==============================================================================================================
117
118
    /**
119
     * @return array
120
     */
121
    public function provideCategoryGroups()
122
    {
123
        $mockCategoryGroup = $this->getMockCategoryGroup(1);
124
125
        return [
126
            'category group with site' => [
127
                'group' => $mockCategoryGroup,
128
                'definition' => $this->getMockCategoryGroupDefinition($mockCategoryGroup),
129
                'site' => $this->getMockSite(),
130
            ],
131
            'category group without site' => [
132
                'group' => $mockCategoryGroup,
133
                'definition' => $this->getMockCategoryGroupDefinition($mockCategoryGroup),
134
                'site' => null,
135
            ],
136
        ];
137
    }
138
139
    //==============================================================================================================
140
    //================================================  HELPERS  ===================================================
141
    //==============================================================================================================
142
143
    /**
144
     * @param CategoryGroupModel $mockCategoryGroup
145
     *
146
     * @return array
147
     */
148
    private function getMockCategoryGroupDefinition(CategoryGroupModel $mockCategoryGroup)
149
    {
150
        return [
151
            'class' => get_class($mockCategoryGroup),
152
            'attributes' => [
153
                'name' => $mockCategoryGroup->name,
154
                'handle' => $mockCategoryGroup->handle,
155
                'maxLevels' => 3,
156
            ],
157
            'fieldLayout' => [
158
                'fields' => [],
159
            ],
160
            'siteSettings' => [
161
                '' => [
162
                    'class' => get_class($mockCategoryGroup->getSiteSettings()[0]),
163
                    'attributes' => [
164
                        'hasUrls' => null,
165
                        'uriFormat' => null,
166
                        'template' => null,
167
                    ],
168
                ],
169
            ],
170
        ];
171
    }
172
173
    /**
174
     * @param int $groupId
175
     *
176
     * @return Mock|CategoryGroupModel
177
     */
178
    private function getMockCategoryGroup(int $groupId)
179
    {
180
        $mockGroup = $this->getMockBuilder(CategoryGroupModel::class)
181
                                    ->setMethods(['getFieldLayout', 'getSiteSettings'])
182
                                    ->getMock();
183
        $mockGroup->setAttributes([
184
            'id' => $groupId,
185
            'fieldLayoutId' => $groupId,
186
            'handle' => 'groupHandle'.$groupId,
187
            'name' => 'groupName'.$groupId,
188
            'maxLevels' => 3,
189
        ]);
190
191
        $mockFieldLayout = $this->getMockBuilder(FieldLayout::class)->getMock();
192
193
        $mockGroup->expects($this->any())
194
                  ->method('getFieldLayout')
195
                  ->willReturn($mockFieldLayout);
196
197
        $mockSiteSettings = $this->getMockSiteSettings();
198
199
        $mockGroup->expects($this->any())
200
                  ->method('getSiteSettings')
201
                  ->willReturn([$mockSiteSettings]);
202
203
        return $mockGroup;
204
    }
205
206
    /**
207
     * Get mock siteSettings.
208
     *
209
     * @param string $class
210
     *
211
     * @return Mock|CategoryGroup_SiteSettings
212
     */
213
    private function getMockSiteSettings()
214
    {
215
        $mockSiteSettings = $this->getMockBuilder(CategoryGroup_SiteSettings::class)
216
                                 ->setMethods(['getSite'])
217
                                 ->getMock();
218
219
        $mockSiteSettings->expects($this->any())
220
          ->method('getSite')
221
          ->willReturn($this->getMockSite());
222
223
        return $mockSiteSettings;
224
    }
225
226
    /**
227
     * Get a mock site.
228
     *
229
     * @return Mock|Site
230
     */
231
    private function getMockSite()
232
    {
233
        return $this->getMockBuilder(Site::class)->getMock();
234
    }
235
}
236