Completed
Pull Request — master (#114)
by Bart
07:47
created

EntryTypeTest::getMockEntryType()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 26
nc 1
nop 1
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 EntryTypes
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();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \NerdsAndCompany\Sch...ters\Models\EntryType() of type object<NerdsAndCompany\S...rters\Models\EntryType> is incompatible with the declared type object<NerdsAndCompany\S...ters\Models\EntryTypes> 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...
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
        $this->converter->setRecordAttributes($newEntryType, $definition, $defaultAttributes);
72
73
        $this->assertSame($defaultAttributes['sectionId'], $newEntryType->sectionId);
0 ignored issues
show
Bug introduced by
Accessing sectionId on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
74
        $this->assertSame($entryType->name, $newEntryType->name);
0 ignored issues
show
Bug introduced by
Accessing name on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
75
        $this->assertSame($entryType->handle, $newEntryType->handle);
0 ignored issues
show
Bug introduced by
Accessing handle on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
76
    }
77
78
    /**
79
     * @dataProvider provideEntryTypes
80
     *
81
     * @param EntryTypeModel $entryType
82
     * @param array          $definition
83
     */
84
    public function testSaveRecord(EntryTypeModel $entryType, array $definition)
85
    {
86
        Craft::$app->sections->expects($this->exactly(1))
87
                             ->method('saveEntryType')
88
                             ->with($entryType)
89
                             ->willReturn(true);
90
91
        $result = $this->converter->saveRecord($entryType, $definition);
92
93
        $this->assertTrue($result);
94
    }
95
96
    /**
97
     * @dataProvider provideEntryTypes
98
     *
99
     * @param EntryTypeModel $entryType
100
     */
101
    public function testDeleteRecord(EntryTypeModel $entryType)
102
    {
103
        Craft::$app->sections->expects($this->exactly(1))
104
                             ->method('deleteEntryType')
105
                             ->with($entryType);
106
107
        $this->converter->deleteRecord($entryType);
108
    }
109
110
    //==============================================================================================================
111
    //==============================================  PROVIDERS  ===================================================
112
    //==============================================================================================================
113
114
    /**
115
     * @return array
116
     */
117
    public function provideEntryTypes()
118
    {
119
        $mockEntryType = $this->getMockEntryType(1);
120
121
        return [
122
            'valid entryType' => [
123
                'entryType' => $mockEntryType,
124
                'definition' => $this->getMockEntryTypeDefinition($mockEntryType),
0 ignored issues
show
Documentation introduced by
$mockEntryType is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<craft\models\EntryType>.

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...
125
                'defaultAttributes' => ['sectionId' => 1],
126
            ],
127
        ];
128
    }
129
130
    //==============================================================================================================
131
    //================================================  HELPERS  ===================================================
132
    //==============================================================================================================
133
134
    /**
135
     * @param EntryTypeModel $mockEntryType
136
     *
137
     * @return array
138
     */
139
    private function getMockEntryTypeDefinition(EntryTypeModel $mockEntryType)
140
    {
141
        return [
142
            'class' => get_class($mockEntryType),
143
            'attributes' => [
144
                'name' => 'entryTypeName'.$mockEntryType->id,
145
                'handle' => 'entryTypeHandle'.$mockEntryType->id,
146
                'hasTitleField' => true,
147
                'titleLabel' => 'Title',
148
                'titleFormat' => null,
149
            ],
150
            'fieldLayout' => $this->getMockFieldLayoutDefinition($mockEntryType->getFieldLayout()),
151
        ];
152
    }
153
154
    /**
155
     * Get mock field layout definition.
156
     *
157
     * @param FieldLayout $fieldLayout
158
     *
159
     * @return array
160
     */
161
    private function getMockFieldLayoutDefinition(FieldLayout $fieldLayout)
162
    {
163
        $tabsDef = [];
164
        foreach ($fieldLayout->getTabs() as $tab) {
165
            $tabsDef[$tab->name] = [];
166
            foreach ($tab->getFields() as $field) {
167
                $tabsDef[$tab->name][$field->handle] = $field->required;
0 ignored issues
show
Bug introduced by
Accessing handle on the interface craft\base\FieldInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing required on the interface craft\base\FieldInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
168
            }
169
        }
170
171
        return [
172
            'tabs' => $tabsDef,
173
        ];
174
    }
175
176
    /**
177
     * @param int $entryTypeId
178
     *
179
     * @return Mock|EntryTypeModel
180
     */
181
    private function getMockEntryType(int $entryTypeId)
182
    {
183
        $mockEntryType = $this->getMockBuilder(EntryTypeModel::class)
184
                           ->setMethods(['getFieldLayout'])
185
                           ->disableOriginalConstructor()
186
                           ->getMock();
187
188
        $mockEntryType->id = $entryTypeId;
0 ignored issues
show
Bug introduced by
Accessing id on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
189
        $mockEntryType->fieldLayoutId = $entryTypeId;
0 ignored issues
show
Bug introduced by
Accessing fieldLayoutId on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
190
        $mockEntryType->handle = 'entryTypeHandle'.$entryTypeId;
0 ignored issues
show
Bug introduced by
Accessing handle on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
191
        $mockEntryType->name = 'entryTypeName'.$entryTypeId;
0 ignored issues
show
Bug introduced by
Accessing name on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
192
193
        $mockField = $this->getMockbuilder(FieldModel::class)->getMock();
194
        $mockField->id = $entryTypeId;
0 ignored issues
show
Bug introduced by
Accessing id on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
195
        $mockField->handle = 'field'.$entryTypeId;
0 ignored issues
show
Bug introduced by
Accessing handle on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
196
        $mockField->required = true;
0 ignored issues
show
Bug introduced by
Accessing required on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
197
198
        $mockFieldLayout = $this->getMockBuilder(FieldLayout::class)->getMock();
199
        $mockFieldLayoutTab = $this->getMockBuilder(FieldLayoutTab::class)->getMock();
200
        $mockFieldLayoutTab->name = 'Content';
0 ignored issues
show
Bug introduced by
Accessing name on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
201
202
        $mockFieldLayout->expects($this->any())
203
                        ->method('getTabs')
204
                        ->willReturn([$mockFieldLayoutTab]);
205
206
        $mockFieldLayoutTab->expects($this->any())
207
                           ->method('getFields')
208
                           ->willReturn([$mockField]);
209
210
        $mockEntryType->expects($this->any())
211
                   ->method('getFieldLayout')
212
                   ->willReturn($mockFieldLayout);
213
214
        return $mockEntryType;
215
    }
216
}
217