Completed
Push — master ( ae47f3...33ec04 )
by Bob Olde
11s
created

VolumeTest::provideVolumes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Base;
4
5
use Craft;
6
use craft\base\Field as FieldModel;
7
use craft\base\Volume as VolumeModel;
8
use craft\models\FieldLayout;
9
use craft\volumes\Local;
10
use Codeception\Test\Unit;
11
12
/**
13
 * Class VolumeTest.
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 VolumeTest extends Unit
22
{
23
    /**
24
     * @var Volumes
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 Volume();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \NerdsAndCompany\Sch...onverters\Base\Volume() of type object<NerdsAndCompany\S...Converters\Base\Volume> is incompatible with the declared type object<NerdsAndCompany\S...onverters\Base\Volumes> 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 provideVolumes
44
     *
45
     * @param VolumeModel $volume
46
     * @param array       $definition
47
     */
48
    public function testGetRecordDefinition(VolumeModel $volume, array $definition)
49
    {
50
        $result = $this->converter->getRecordDefinition($volume);
51
52
        $this->assertSame($definition, $result);
53
    }
54
55
    /**
56
     * @dataProvider provideVolumes
57
     *
58
     * @param VolumeModel $volume
59
     * @param array       $definition
60
     */
61
    public function testSaveRecord(VolumeModel $volume, array $definition)
62
    {
63
        Craft::$app->volumes->expects($this->exactly(1))
64
                            ->method('saveVolume')
65
                            ->with($volume)
66
                            ->willReturn(true);
67
68
        $result = $this->converter->saveRecord($volume, $definition);
69
70
        $this->assertTrue($result);
71
    }
72
73
    /**
74
     * @dataProvider provideVolumes
75
     *
76
     * @param VolumeModel $volume
77
     */
78
    public function testDeleteRecord(VolumeModel $volume)
79
    {
80
        Craft::$app->volumes->expects($this->exactly(1))
81
                            ->method('deleteVolume')
82
                            ->with($volume);
83
84
        $this->converter->deleteRecord($volume);
85
    }
86
87
    //==============================================================================================================
88
    //==============================================  PROVIDERS  ===================================================
89
    //==============================================================================================================
90
91
    /**
92
     * @return array
93
     */
94
    public function provideVolumes()
95
    {
96
        $mockVolume = $this->getMockVolume(1);
97
98
        return [
99
            'local volume' => [
100
                'volume' => $mockVolume,
101
                'definition' => $this->getMockVolumeDefinition($mockVolume),
0 ignored issues
show
Documentation introduced by
$mockVolume is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<craft\base\Volume>.

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 int $volumeId
112
     *
113
     * @return Mock|VolumeModel
114
     */
115
    private function getMockVolume(int $volumeId)
116
    {
117
        $mockVolume = $this->getMockBuilder(Local::class)
118
                           ->setMethods(['getFieldLayout'])
119
                           ->disableOriginalConstructor()
120
                           ->getMock();
121
122
        $mockVolume->id = $volumeId;
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...
123
        $mockVolume->fieldLayoutId = $volumeId;
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...
124
        $mockVolume->handle = 'volumeHandle'.$volumeId;
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...
125
        $mockVolume->name = 'volumeName'.$volumeId;
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...
126
127
        $mockField = $this->getMockbuilder(FieldModel::class)->getMock();
128
        $mockField->id = $volumeId;
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...
129
        $mockField->handle = 'field'.$volumeId;
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...
130
        $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...
131
132
        $mockFieldLayout = $this->getMockBuilder(FieldLayout::class)->getMock();
133
134
        $mockFieldLayout->expects($this->any())
135
                        ->method('getFields')
136
                        ->willReturn([$mockField]);
137
138
        $mockVolume->expects($this->any())
139
                   ->method('getFieldLayout')
140
                   ->willReturn($mockFieldLayout);
141
142
        return $mockVolume;
143
    }
144
145
    /**
146
     * @param VolumeModel $mockVolume
147
     *
148
     * @return array
149
     */
150
    private function getMockVolumeDefinition(VolumeModel $mockVolume)
151
    {
152
        $fieldDefs = [];
153
        foreach ($mockVolume->getFieldLayout()->getFields() as $field) {
154
            $fieldDefs[$field->handle] = $field->required;
155
        }
156
157
        return [
158
            'class' => get_class($mockVolume),
159
            'attributes' => [
160
                'path' => null,
161
                'name' => 'volumeName'.$mockVolume->id,
162
                'handle' => 'volumeHandle'.$mockVolume->id,
163
                'hasUrls' => null,
164
                'url' => null,
165
                'sortOrder' => null,
166
            ],
167
            'fieldLayout' => [
168
                'fields' => $fieldDefs,
169
            ],
170
        ];
171
    }
172
}
173