Completed
Push — master ( 33ec04...ed9cfc )
by Bart
11s
created

tests/unit/Converters/Models/EntryTypeTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Models;
4
5
use Craft;
6
use craft\base\Field as FieldModel;
7
use craft\models\EntryType as EntryTypeModel;
8
use craft\models\FieldLayout;
9
use craft\models\FieldLayoutTab;
10
use Codeception\Test\Unit;
11
12
/**
13
 * Class EntryTypeTest.
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 EntryTypeTest extends Unit
22
{
23
    /**
24
     * @var EntryType
25
     */
26
    private $converter;
27
28
    /**
29
     * Set the converter.
30
     *
31
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
32
     */
33
    protected function _before()
34
    {
35
        $this->converter = new EntryType();
36
    }
37
38
    //==============================================================================================================
39
    //=================================================  TESTS  ====================================================
40
    //==============================================================================================================
41
42
    /**
43
     * @dataProvider provideEntryTypes
44
     *
45
     * @param EntryTypeModel $entryType
46
     * @param array          $definition
47
     */
48
    public function testGetRecordDefinition(EntryTypeModel $entryType, array $definition)
49
    {
50
        $result = $this->converter->getRecordDefinition($entryType);
51
52
        $this->assertSame($definition, $result);
53
    }
54
55
    /**
56
     * @dataProvider provideEntryTypes
57
     *
58
     * @param EntryTypeModel $entryType
59
     * @param array          $definition
60
     * @param array          $defaultAttributes
61
     */
62
    public function testSetRecordAttributes(EntryTypeModel $entryType, array $definition, array $defaultAttributes)
63
    {
64
        $newEntryType = $this->getMockBuilder(EntryTypeModel::class)
65
                             ->setMethods(['setFieldLayout'])
66
                             ->getMock();
67
68
        $newEntryType->expects($this->exactly(1))
69
                     ->method('setFieldLayout');
70
71
        Craft::$app->fields->expects($this->any())
72
                           ->method('getFieldByHandle')
73
                           ->willReturn($this->getMockField($entryType->id));
74
75
        $this->converter->setRecordAttributes($newEntryType, $definition, $defaultAttributes);
0 ignored issues
show
$newEntryType is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<craft\base\Model>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
77
        $this->assertSame($defaultAttributes['sectionId'], $newEntryType->sectionId);
78
        $this->assertSame($entryType->name, $newEntryType->name);
79
        $this->assertSame($entryType->handle, $newEntryType->handle);
80
    }
81
82
    /**
83
     * @dataProvider provideEntryTypes
84
     *
85
     * @param EntryTypeModel $entryType
86
     * @param array          $definition
87
     */
88
    public function testSaveRecord(EntryTypeModel $entryType, array $definition)
89
    {
90
        Craft::$app->sections->expects($this->exactly(1))
91
                             ->method('saveEntryType')
92
                             ->with($entryType)
93
                             ->willReturn(true);
94
95
        $result = $this->converter->saveRecord($entryType, $definition);
96
97
        $this->assertTrue($result);
98
    }
99
100
    /**
101
     * @dataProvider provideEntryTypes
102
     *
103
     * @param EntryTypeModel $entryType
104
     */
105
    public function testDeleteRecord(EntryTypeModel $entryType)
106
    {
107
        Craft::$app->sections->expects($this->exactly(1))
108
                             ->method('deleteEntryType')
109
                             ->with($entryType);
110
111
        $this->converter->deleteRecord($entryType);
112
    }
113
114
    //==============================================================================================================
115
    //==============================================  PROVIDERS  ===================================================
116
    //==============================================================================================================
117
118
    /**
119
     * @return array
120
     */
121
    public function provideEntryTypes()
122
    {
123
        $mockEntryType = $this->getMockEntryType(1);
124
125
        return [
126
            'valid entryType' => [
127
                'entryType' => $mockEntryType,
128
                'definition' => $this->getMockEntryTypeDefinition($mockEntryType),
129
                'defaultAttributes' => ['sectionId' => 1],
130
            ],
131
        ];
132
    }
133
134
    //==============================================================================================================
135
    //================================================  HELPERS  ===================================================
136
    //==============================================================================================================
137
138
    /**
139
     * @param EntryTypeModel $mockEntryType
140
     *
141
     * @return array
142
     */
143
    private function getMockEntryTypeDefinition(EntryTypeModel $mockEntryType)
144
    {
145
        return [
146
            'class' => get_class($mockEntryType),
147
            'attributes' => [
148
                'name' => 'entryTypeName'.$mockEntryType->id,
149
                'handle' => 'entryTypeHandle'.$mockEntryType->id,
150
                'hasTitleField' => true,
151
                'titleLabel' => 'Title',
152
                'titleFormat' => null,
153
            ],
154
            'fieldLayout' => $this->getMockFieldLayoutDefinition($mockEntryType->getFieldLayout()),
155
        ];
156
    }
157
158
    /**
159
     * Get mock field layout definition.
160
     *
161
     * @param FieldLayout $fieldLayout
162
     *
163
     * @return array
164
     */
165
    private function getMockFieldLayoutDefinition(FieldLayout $fieldLayout)
166
    {
167
        $tabsDef = [];
168
        foreach ($fieldLayout->getTabs() as $tab) {
169
            $tabsDef[$tab->name] = [];
170
            foreach ($tab->getFields() as $field) {
171
                $tabsDef[$tab->name][$field->handle] = $field->required;
172
            }
173
        }
174
175
        return [
176
            'tabs' => $tabsDef,
177
        ];
178
    }
179
180
    /**
181
     * @param int $entryTypeId
182
     *
183
     * @return Mock|EntryTypeModel
184
     */
185
    private function getMockEntryType(int $entryTypeId)
186
    {
187
        $mockEntryType = $this->getMockBuilder(EntryTypeModel::class)
188
                              ->setMethods(['getFieldLayout'])
189
                              ->disableOriginalConstructor()
190
                              ->getMock();
191
192
        $mockEntryType->id = $entryTypeId;
193
        $mockEntryType->fieldLayoutId = $entryTypeId;
194
        $mockEntryType->handle = 'entryTypeHandle'.$entryTypeId;
195
        $mockEntryType->name = 'entryTypeName'.$entryTypeId;
196
197
        $mockField = $this->getMockField($entryTypeId);
198
199
        $mockFieldLayout = $this->getMockBuilder(FieldLayout::class)->getMock();
200
        $mockFieldLayoutTab = $this->getMockBuilder(FieldLayoutTab::class)->getMock();
201
        $mockFieldLayoutTab->name = 'Content';
202
203
        $mockFieldLayout->expects($this->any())
204
                        ->method('getTabs')
205
                        ->willReturn([$mockFieldLayoutTab]);
206
207
        $mockFieldLayoutTab->expects($this->any())
208
                           ->method('getFields')
209
                           ->willReturn([$mockField]);
210
211
        $mockEntryType->expects($this->any())
212
                   ->method('getFieldLayout')
213
                   ->willReturn($mockFieldLayout);
214
215
        return $mockEntryType;
216
    }
217
218
    /**
219
     * Get a mock field.
220
     *
221
     * @param int $fieldId
222
     *
223
     * @return Mock|FieldModel
224
     */
225
    private function getMockField(int $fieldId)
226
    {
227
        $mockField = $this->getMockbuilder(FieldModel::class)
228
                         ->setMethods([])
229
                         ->getMock();
230
231
        $mockField->id = $fieldId;
232
        $mockField->handle = 'field'.$fieldId;
233
        $mockField->required = true;
234
235
        return $mockField;
236
    }
237
}
238