|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Craft; |
|
6
|
|
|
use craft\console\Application; |
|
7
|
|
|
use yii\console\Controller; |
|
8
|
|
|
use craft\i18n\I18n; |
|
9
|
|
|
use craft\services\AssetTransforms; |
|
10
|
|
|
use craft\services\Categories; |
|
11
|
|
|
use craft\services\Elements; |
|
12
|
|
|
use craft\services\Fields; |
|
13
|
|
|
use craft\services\Globals; |
|
14
|
|
|
use craft\services\Matrix; |
|
15
|
|
|
use craft\services\Path; |
|
16
|
|
|
use craft\services\Sections; |
|
17
|
|
|
use craft\services\Sites; |
|
18
|
|
|
use craft\services\Tags; |
|
19
|
|
|
use craft\services\UserGroups; |
|
20
|
|
|
use craft\services\UserPermissions; |
|
21
|
|
|
use craft\services\Volumes; |
|
22
|
|
|
use Codeception\Module; |
|
23
|
|
|
use Codeception\TestCase; |
|
24
|
|
|
use NerdsAndCompany\Schematic\Schematic; |
|
25
|
|
|
use NerdsAndCompany\Schematic\Mappers\ModelMapper; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* UnitTest helper. |
|
29
|
|
|
* |
|
30
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
|
31
|
|
|
*/ |
|
32
|
|
|
class Unit extends Module |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* Mock craft Mappers. |
|
36
|
|
|
* |
|
37
|
|
|
* @SuppressWarnings(PHPMD.CamelCaseMethodName) |
|
38
|
|
|
* |
|
39
|
|
|
* @param TestCase $test |
|
40
|
|
|
*/ |
|
41
|
|
|
public function _before(TestCase $test) |
|
42
|
|
|
{ |
|
43
|
|
|
$mockApp = $this->getMockApp($test); |
|
44
|
|
|
$mockApp->controller = $this->getMock($test, Controller::class); |
|
45
|
|
|
$mockApp->controller->module = $this->getmockModule($test); |
|
46
|
|
|
|
|
47
|
|
|
Craft::$app = $mockApp; |
|
|
|
|
|
|
48
|
|
|
Schematic::$force = false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get a preconfigured mock module. |
|
53
|
|
|
* |
|
54
|
|
|
* @param TestCase $test |
|
55
|
|
|
* |
|
56
|
|
|
* @return Mock|Schematic |
|
57
|
|
|
*/ |
|
58
|
|
|
private function getMockModule(TestCase $test) |
|
59
|
|
|
{ |
|
60
|
|
|
$mockModule = $this->getMock($test, Schematic::class); |
|
61
|
|
|
$mockModelMapper = $this->getMock($test, ModelMapper::class); |
|
62
|
|
|
$mockModule->expects($test->any()) |
|
63
|
|
|
->method('__get') |
|
64
|
|
|
->willReturnMap([ |
|
65
|
|
|
['modelMapper', $mockModelMapper], |
|
66
|
|
|
]); |
|
67
|
|
|
|
|
68
|
|
|
return $mockModule; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get a preconfigured mock app. |
|
73
|
|
|
* |
|
74
|
|
|
* @param TestCase $test |
|
75
|
|
|
* |
|
76
|
|
|
* @return Mock|Application |
|
77
|
|
|
*/ |
|
78
|
|
|
private function getMockApp(TestCase $test) |
|
79
|
|
|
{ |
|
80
|
|
|
$mockApp = $this->getMock($test, Application::class); |
|
81
|
|
|
$mockAssetTransforms = $this->getMock($test, AssetTransforms::class); |
|
82
|
|
|
$mockCategoryGroups = $this->getMock($test, Categories::class); |
|
83
|
|
|
$mockElements = $this->getMock($test, Elements::class); |
|
84
|
|
|
$mockFields = $this->getMock($test, Fields::class); |
|
85
|
|
|
$mockGlobals = $this->getMock($test, Globals::class); |
|
86
|
|
|
$mockI18n = $this->getMock($test, I18n::class); |
|
87
|
|
|
$mockMatrix = $this->getMock($test, Matrix::class); |
|
88
|
|
|
$mockPath = $this->getMock($test, Path::class); |
|
89
|
|
|
$mockSections = $this->getMock($test, Sections::class); |
|
90
|
|
|
$mockSites = $this->getMock($test, Sites::class); |
|
91
|
|
|
$mockTags = $this->getMock($test, Tags::class); |
|
92
|
|
|
$mockUserGroups = $this->getMock($test, UserGroups::class); |
|
93
|
|
|
$mockUserPermissions = $this->getMock($test, UserPermissions::class); |
|
94
|
|
|
$mockVolumes = $this->getMock($test, Volumes::class); |
|
95
|
|
|
|
|
96
|
|
|
$mockApp->expects($test->any()) |
|
97
|
|
|
->method('__get') |
|
98
|
|
|
->willReturnMap([ |
|
99
|
|
|
['assetTransforms', $mockAssetTransforms], |
|
100
|
|
|
['categories', $mockCategoryGroups], |
|
101
|
|
|
['elements', $mockElements], |
|
102
|
|
|
['fields', $mockFields], |
|
103
|
|
|
['globals', $mockGlobals], |
|
104
|
|
|
['matrix', $mockMatrix], |
|
105
|
|
|
['sections', $mockSections], |
|
106
|
|
|
['sites', $mockSites], |
|
107
|
|
|
['tags', $mockTags], |
|
108
|
|
|
['userGroups', $mockUserGroups], |
|
109
|
|
|
['userPermissions', $mockUserPermissions], |
|
110
|
|
|
['volumes', $mockVolumes], |
|
111
|
|
|
]); |
|
112
|
|
|
|
|
113
|
|
|
$mockApp->expects($test->any()) |
|
114
|
|
|
->method('getPath') |
|
115
|
|
|
->willreturn($mockPath); |
|
116
|
|
|
|
|
117
|
|
|
$mockApp->expects($test->any()) |
|
118
|
|
|
->method('getI18n') |
|
119
|
|
|
->willReturn($mockI18n); |
|
120
|
|
|
|
|
121
|
|
|
$mockApp->expects($test->any()) |
|
122
|
|
|
->method('getMatrix') |
|
123
|
|
|
->willreturn($mockMatrix); |
|
124
|
|
|
|
|
125
|
|
|
return $mockApp; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Get a mock object for class. |
|
130
|
|
|
* |
|
131
|
|
|
* @param TestCase $test |
|
132
|
|
|
* @param string $class |
|
133
|
|
|
* |
|
134
|
|
|
* @return Mock |
|
135
|
|
|
*/ |
|
136
|
|
|
private function getMock(TestCase $test, string $class) |
|
137
|
|
|
{ |
|
138
|
|
|
return $test->getMockBuilder($class) |
|
139
|
|
|
->disableOriginalConstructor() |
|
140
|
|
|
->getMock(); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.