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

FieldTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 7
dl 0
loc 179
rs 10
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Base;
4
5
use Craft;
6
use craft\base\Field as FieldModel;
7
use Codeception\Test\Unit;
8
9
/**
10
 * Class FieldTest.
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 FieldTest extends Unit
19
{
20
    /**
21
     * @var Field
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 Field();
34
    }
35
36
    //==============================================================================================================
37
    //=================================================  TESTS  ====================================================
38
    //==============================================================================================================
39
40
    /**
41
     * @dataProvider provideFields
42
     *
43
     * @param FieldModel $field
44
     * @param array      $definition
45
     */
46
    public function testGetRecordDefinition(FieldModel $field, array $definition)
47
    {
48
        $result = $this->converter->getRecordDefinition($field);
49
50
        $this->assertSame($definition, $result);
51
    }
52
53
    /**
54
     * @dataProvider provideFields
55
     *
56
     * @param FieldModel $field
57
     * @param array      $definition
58
     * @param string     $groupStatus existing|new|invalid
59
     */
60
    public function testSaveRecord(FieldModel $field, array $definition, string $groupStatus)
61
    {
62
        Craft::$app->fields->expects($this->exactly(1))
63
                            ->method('getAllGroups')
64
                            ->willReturn([$this->getMockFieldGroup(1)]);
65
66
        Craft::$app->fields->expects($this->exactly('existing' == $groupStatus ? 0 : 1))
67
                          ->method('saveGroup')
68
                          ->willReturn('invalid' !== $groupStatus);
69
70
        Craft::$app->fields->expects($this->exactly(1))
71
                          ->method('saveField')
72
                          ->with($field)
73
                          ->willReturn(true);
74
75
        $result = $this->converter->saveRecord($field, $definition);
76
77
        $this->assertTrue($result);
78
    }
79
80
    /**
81
     * @dataProvider provideFields
82
     *
83
     * @param FieldModel $field
84
     */
85
    public function testDeleteRecord(FieldModel $field)
86
    {
87
        Craft::$app->fields->expects($this->exactly(1))
88
                            ->method('deleteField')
89
                            ->with($field);
90
91
        $this->converter->deleteRecord($field);
92
    }
93
94
    //==============================================================================================================
95
    //==============================================  PROVIDERS  ===================================================
96
    //==============================================================================================================
97
98
    /**
99
     * @return array
100
     */
101
    public function provideFields()
102
    {
103
        $mockField1 = $this->getMockField(1, 1);
104
        $mockField2 = $this->getMockField(1, 2);
105
106
        return [
107
            'valid field existing group' => [
108
                'field' => $mockField1,
109
                'definition' => $this->getMockFieldDefinition($mockField1),
110
                'groupStatus' => 'existing',
111
            ],
112
            'valid field new group' => [
113
                'field' => $mockField2,
114
                'definition' => $this->getMockFieldDefinition($mockField2),
115
                'groupStatus' => 'new',
116
            ],
117
            'valid field invalid group' => [
118
                'field' => $mockField2,
119
                'definition' => $this->getMockFieldDefinition($mockField2),
120
                'groupStatus' => 'invalid',
121
            ],
122
        ];
123
    }
124
125
    //==============================================================================================================
126
    //================================================  HELPERS  ===================================================
127
    //==============================================================================================================
128
129
    /**
130
     * @param FieldModel $mockField
131
     *
132
     * @return array
133
     */
134
    private function getMockFieldDefinition(FieldModel $mockField)
135
    {
136
        return [
137
            'class' => get_class($mockField),
138
            'attributes' => [
139
                'name' => 'fieldName'.$mockField->id,
140
                'handle' => 'fieldHandle'.$mockField->id,
141
                'instructions' => null,
142
                'translationMethod' => 'none',
143
                'translationKeyFormat' => null,
144
                'oldHandle' => null,
145
                'columnPrefix' => null,
146
                'required' => false,
147
                'sortOrder' => null,
148
            ],
149
            'group' => $mockField->group->name,
150
        ];
151
    }
152
153
    /**
154
     * @param int $fieldId
155
     * @param int $groupId
156
     *
157
     * @return Mock|FieldModel
158
     */
159
    private function getMockField(int $fieldId, int $groupId)
160
    {
161
        $mockField = $this->getMockBuilder(FieldModel::class)
162
                           ->setMethods(['getGroup'])
163
                           ->disableOriginalConstructor()
164
                           ->getMock();
165
166
        $mockField->id = $fieldId;
167
        $mockField->groupId = $fieldId;
168
        $mockField->handle = 'fieldHandle'.$fieldId;
169
        $mockField->name = 'fieldName'.$fieldId;
170
171
        $mockField->expects($this->any())
172
                 ->method('getGroup')
173
                 ->willReturn($this->getMockFieldGroup($groupId));
174
175
        return $mockField;
176
    }
177
178
    /**
179
     * Get a mock field group.
180
     *
181
     * @param int $groupId
182
     *
183
     * @return Mock|FieldGroup
184
     */
185
    private function getMockFieldGroup(int $groupId)
186
    {
187
        $mockGroup = $this->getMockBuilder(FieldGroup::class)
188
                        ->disableOriginalConstructor()
189
                        ->getMock();
190
191
        $mockGroup->id = $groupId;
192
        $mockGroup->name = 'fieldGroup'.$groupId;
193
194
        return $mockGroup;
195
    }
196
}
197