Completed
Pull Request — master (#114)
by Bart
02:01
created

EntryTypeTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 2
cbo 7
dl 0
loc 151
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A _before() 0 4 1
A testGetRecordDefinition() 0 6 1
A testSaveRecord() 0 11 1
A testDeleteRecord() 0 8 1
A provideEntryTypes() 0 11 1
A getMockEntryTypeDefinition() 0 21 2
B getMockEntryType() 0 29 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\entryTypes\Local;
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
     */
61
    public function testSaveRecord(EntryTypeModel $entryType, array $definition)
62
    {
63
        Craft::$app->sections->expects($this->exactly(1))
64
                             ->method('saveEntryType')
65
                             ->with($entryType)
66
                             ->willReturn(true);
67
68
        $result = $this->converter->saveRecord($entryType, $definition);
69
70
        $this->assertTrue($result);
71
    }
72
73
    /**
74
     * @dataProvider provideEntryTypes
75
     *
76
     * @param EntryTypeModel $entryType
77
     */
78
    public function testDeleteRecord(EntryTypeModel $entryType)
79
    {
80
        Craft::$app->sections->expects($this->exactly(1))
81
                             ->method('deleteEntryType')
82
                             ->with($entryType);
83
84
        $this->converter->deleteRecord($entryType);
85
    }
86
87
    //==============================================================================================================
88
    //==============================================  PROVIDERS  ===================================================
89
    //==============================================================================================================
90
91
    /**
92
     * @return array
93
     */
94
    public function provideEntryTypes()
95
    {
96
        $mockEntryType = $this->getMockEntryType(1);
97
98
        return [
99
            'local entryType' => [
100
                'entryType' => $mockEntryType,
101
                '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...
102
            ],
103
        ];
104
    }
105
106
    //==============================================================================================================
107
    //================================================  HELPERS  ===================================================
108
    //==============================================================================================================
109
110
    /**
111
     * @param EntryTypeModel $mockEntryType
112
     *
113
     * @return array
114
     */
115
    private function getMockEntryTypeDefinition(EntryTypeModel $mockEntryType)
116
    {
117
        $fieldDefs = [];
118
        foreach ($mockEntryType->getFieldLayout()->getFields() as $field) {
119
            $fieldDefs[$field->handle] = $field->required;
120
        }
121
122
        return [
123
            'class' => get_class($mockEntryType),
124
            'attributes' => [
125
                'name' => 'entryTypeName'.$mockEntryType->id,
126
                'handle' => 'entryTypeHandle'.$mockEntryType->id,
127
                'hasTitleField' => true,
128
                'titleLabel' => 'Title',
129
                'titleFormat' => null,
130
            ],
131
            'fieldLayout' => [
132
                'fields' => $fieldDefs,
133
            ],
134
        ];
135
    }
136
137
    /**
138
     * @param int $entryTypeId
139
     *
140
     * @return Mock|EntryTypeModel
141
     */
142
    private function getMockEntryType(int $entryTypeId)
143
    {
144
        $mockEntryType = $this->getMockBuilder(EntryTypeModel::class)
145
                           ->setMethods(['getFieldLayout'])
146
                           ->disableOriginalConstructor()
147
                           ->getMock();
148
149
        $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...
150
        $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...
151
        $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...
152
        $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...
153
154
        $mockField = $this->getMockbuilder(FieldModel::class)->getMock();
155
        $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...
156
        $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...
157
        $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...
158
159
        $mockFieldLayout = $this->getMockBuilder(FieldLayout::class)->getMock();
160
161
        $mockFieldLayout->expects($this->any())
162
                        ->method('getFields')
163
                        ->willReturn([$mockField]);
164
165
        $mockEntryType->expects($this->any())
166
                   ->method('getFieldLayout')
167
                   ->willReturn($mockFieldLayout);
168
169
        return $mockEntryType;
170
    }
171
}
172