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

SiteTest::getMockSiteDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Models;
4
5
use Craft;
6
use craft\models\Site as SiteModel;
7
use craft\models\SiteGroup;
8
use Codeception\Test\Unit;
9
10
/**
11
 * Class SiteTest.
12
 *
13
 * @author    Nerds & Company
14
 * @copyright Copyright (c) 2015-2017, Nerds & Company
15
 * @license   MIT
16
 *
17
 * @see      http://www.nerds.company
18
 */
19
class SiteTest extends Unit
20
{
21
    /**
22
     * @var Sites
23
     */
24
    private $converter;
25
26
    /**
27
     * Set the converter.
28
     *
29
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
30
     */
31
    protected function _before()
32
    {
33
        $this->converter = new Site();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \NerdsAndCompany\Sch...onverters\Models\Site() of type object<NerdsAndCompany\S...Converters\Models\Site> is incompatible with the declared type object<NerdsAndCompany\S...onverters\Models\Sites> 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...
34
    }
35
36
    //==============================================================================================================
37
    //=================================================  TESTS  ====================================================
38
    //==============================================================================================================
39
40
    /**
41
     * @dataProvider provideSites
42
     *
43
     * @param SiteModel $site
44
     * @param array     $definition
45
     */
46
    public function testGetRecordDefinition(SiteModel $site, array $definition)
47
    {
48
        $result = $this->converter->getRecordDefinition($site);
49
50
        $this->assertSame($definition, $result);
51
    }
52
53
    /**
54
     * @dataProvider provideSites
55
     *
56
     * @param SiteModel $site
57
     * @param array     $definition
58
     * @param string    $groupStatus existing|new|invalid
59
     */
60
    public function testSaveRecord(SiteModel $site, array $definition, string $groupStatus)
61
    {
62
        Craft::$app->sites->expects($this->exactly(1))
63
                          ->method('getAllGroups')
64
                          ->willReturn([$this->getMockSiteGroup(1)]);
65
66
        Craft::$app->sites->expects($this->exactly($groupStatus == 'existing' ? 0 : 1))
67
                          ->method('saveGroup')
68
                          ->willReturn($groupStatus !== 'invalid');
69
70
        Craft::$app->sites->expects($this->exactly(1))
71
                          ->method('saveSite')
72
                          ->with($site)
73
                          ->willReturn(true);
74
75
        $result = $this->converter->saveRecord($site, $definition);
76
77
        $this->assertTrue($result);
78
    }
79
80
    /**
81
     * @dataProvider provideSites
82
     *
83
     * @param SiteModel $site
84
     */
85
    public function testDeleteRecord(SiteModel $site)
86
    {
87
        Craft::$app->sites->expects($this->exactly(1))
88
                          ->method('deleteSiteById')
89
                          ->with($site->id);
90
91
        $this->converter->deleteRecord($site);
92
    }
93
94
    //==============================================================================================================
95
    //==============================================  PROVIDERS  ===================================================
96
    //==============================================================================================================
97
98
    /**
99
     * @return array
100
     */
101
    public function provideSites()
102
    {
103
        $mockSite1 = $this->getMockSite(1, 1);
104
        $mockSite2 = $this->getMockSite(1, 2);
105
106
        return [
107
            'valid site existing group' => [
108
                'site' => $mockSite1,
109
                'definition' => $this->getMockSiteDefinition($mockSite1),
0 ignored issues
show
Documentation introduced by
$mockSite1 is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<craft\models\Site>.

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...
110
                'groupStatus' => 'existing',
111
            ],
112
            'valid site new group' => [
113
                'site' => $mockSite2,
114
                'definition' => $this->getMockSiteDefinition($mockSite2),
0 ignored issues
show
Documentation introduced by
$mockSite2 is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<craft\models\Site>.

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...
115
                'groupStatus' => 'new',
116
            ],
117
            'valid site invalid group' => [
118
                'site' => $mockSite2,
119
                'definition' => $this->getMockSiteDefinition($mockSite2),
0 ignored issues
show
Documentation introduced by
$mockSite2 is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<craft\models\Site>.

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...
120
                'groupStatus' => 'invalid',
121
            ],
122
        ];
123
    }
124
125
    //==============================================================================================================
126
    //================================================  HELPERS  ===================================================
127
    //==============================================================================================================
128
129
    /**
130
     * @param SiteModel $mockSite
131
     *
132
     * @return array
133
     */
134
    private function getMockSiteDefinition(SiteModel $mockSite)
135
    {
136
        return [
137
            'class' => get_class($mockSite),
138
            'attributes' => [
139
                'name' => $mockSite->name,
140
                'handle' => $mockSite->handle,
141
                'language' => 'nl',
142
                'primary' => true,
143
                'hasUrls' => true,
144
                'originalName' => null,
145
                'originalBaseUrl' => null,
146
                'baseUrl' => '@web/',
147
                'sortOrder' => 1,
148
            ],
149
            'group' => $mockSite->group->name,
150
        ];
151
    }
152
153
    /**
154
     * @param int $siteId
155
     *
156
     * @return Mock|SiteModel
157
     */
158
    private function getMockSite(int $siteId, int $groupId)
159
    {
160
        $mockSite = $this->getMockBuilder(SiteModel::class)
161
                                    ->setMethods(['getGroup'])
162
                                    ->disableOriginalConstructor()
163
                                    ->getMock();
164
165
        $mockSite->id = $siteId;
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...
166
        $mockSite->groupId = $groupId;
0 ignored issues
show
Bug introduced by
Accessing groupId 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...
167
        $mockSite->handle = 'siteHandle'.$siteId;
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...
168
        $mockSite->language = 'nl';
0 ignored issues
show
Bug introduced by
Accessing language 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...
169
        $mockSite->primary = true;
0 ignored issues
show
Bug introduced by
Accessing primary 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...
170
        $mockSite->hasUrls = true;
0 ignored issues
show
Bug introduced by
Accessing hasUrls 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...
171
        $mockSite->originalName = null;
0 ignored issues
show
Bug introduced by
Accessing originalName 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...
172
        $mockSite->originalBaseUrl = null;
0 ignored issues
show
Bug introduced by
Accessing originalBaseUrl 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...
173
        $mockSite->baseUrl = '@web/';
0 ignored issues
show
Bug introduced by
Accessing baseUrl 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...
174
        $mockSite->sortOrder = 1;
0 ignored issues
show
Bug introduced by
Accessing sortOrder 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...
175
176
        $mockSite->expects($this->any())
177
                 ->method('getGroup')
178
                 ->willReturn($this->getMockSiteGroup($groupId));
179
180
        return $mockSite;
181
    }
182
183
    /**
184
     * Get a mock site group.
185
     *
186
     * @param int $groupId
187
     *
188
     * @return Mock|SiteGroup
189
     */
190
    private function getMockSiteGroup(int $groupId)
191
    {
192
        $mockGroup = $this->getMockBuilder(SiteGroup::class)
193
                        ->disableOriginalConstructor()
194
                        ->getmock();
195
196
        $mockGroup->id = $groupId;
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...
197
        $mockGroup->name = 'siteGroup'.$groupId;
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...
198
199
        return $mockGroup;
200
    }
201
}
202