-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathUnit.php
164 lines (150 loc) · 5.12 KB
/
Unit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Helper;
use Craft;
use Yii;
use craft\console\Application;
use yii\console\Controller;
use craft\i18n\I18n;
use craft\services\Assets;
use craft\services\AssetTransforms;
use craft\services\Categories;
use craft\services\Content;
use craft\services\Elements;
use craft\services\ElementIndexes;
use craft\services\Fields;
use craft\services\Globals;
use craft\services\Matrix;
use craft\services\Path;
use craft\services\Plugins;
use craft\services\Sections;
use craft\services\Sites;
use craft\services\SystemSettings;
use craft\services\Tags;
use craft\services\UserGroups;
use craft\services\UserPermissions;
use craft\services\Volumes;
use Codeception\Module;
use Codeception\TestCase;
use NerdsAndCompany\Schematic\Schematic;
use NerdsAndCompany\Schematic\Mappers\ModelMapper;
/**
* UnitTest helper.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Unit extends Module
{
/**
* Mock craft Mappers.
*
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
* phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
*
* @param TestCase $test
*/
public function _before(TestCase $test)
{
$mockApp = $this->getMockApp($test);
$mockApp->controller = $this->getMock($test, Controller::class);
$mockApp->controller->module = $this->getMockModule($test);
Craft::$app = $mockApp;
Yii::$app = $mockApp;
Schematic::$force = false;
}
/**
* Get a preconfigured mock module.
*
* @param TestCase $test
*
* @return Mock|Schematic
*/
private function getMockModule(TestCase $test)
{
$mockModule = $this->getMock($test, Schematic::class);
$mockModelMapper = $this->getMock($test, ModelMapper::class);
$mockModule->expects($test->any())
->method('__get')
->willReturnMap([
['modelMapper', $mockModelMapper],
]);
return $mockModule;
}
/**
* Get a preconfigured mock app.
*
* @param TestCase $test
*
* @return Mock|Application
*/
private function getMockApp(TestCase $test)
{
$mockApp = $this->getMock($test, Application::class);
$mockAssets = $this->getMock($test, Assets::class);
$mockAssetTransforms = $this->getMock($test, AssetTransforms::class);
$mockCategoryGroups = $this->getMock($test, Categories::class);
$mockContent = $this->getMock($test, Content::class);
$mockElements = $this->getMock($test, Elements::class);
$mockElementIndexes = $this->getMock($test, ElementIndexes::class);
$mockFields = $this->getMock($test, Fields::class);
$mockGlobals = $this->getMock($test, Globals::class);
$mockI18n = $this->getMock($test, I18n::class);
$mockMatrix = $this->getMock($test, Matrix::class);
$mockPath = $this->getMock($test, Path::class);
$mockPlugins = $this->getMock($test, Plugins::class);
$mockSections = $this->getMock($test, Sections::class);
$mockSites = $this->getMock($test, Sites::class);
$mockSystemSettings = $this->getMock($test, SystemSettings::class);
$mockTags = $this->getMock($test, Tags::class);
$mockUserGroups = $this->getMock($test, UserGroups::class);
$mockUserPermissions = $this->getMock($test, UserPermissions::class);
$mockVolumes = $this->getMock($test, Volumes::class);
$mockApp->expects($test->any())
->method('__get')
->willReturnMap([
['assets', $mockAssets],
['assetTransforms', $mockAssetTransforms],
['categories', $mockCategoryGroups],
['content', $mockContent],
['elements', $mockElements],
['elementIndexes', $mockElementIndexes],
['fields', $mockFields],
['globals', $mockGlobals],
['matrix', $mockMatrix],
['plugins', $mockPlugins],
['sections', $mockSections],
['sites', $mockSites],
['systemSettings', $mockSystemSettings],
['tags', $mockTags],
['userGroups', $mockUserGroups],
['userPermissions', $mockUserPermissions],
['volumes', $mockVolumes],
]);
$mockApp->expects($test->any())
->method('getPath')
->willReturn($mockPath);
$mockApp->expects($test->any())
->method('getI18n')
->willReturn($mockI18n);
$mockApp->expects($test->any())
->method('getMatrix')
->willReturn($mockMatrix);
$mockApp->expects($test->any())
->method('getFields')
->willReturn($mockFields);
return $mockApp;
}
/**
* Get a mock object for class.
*
* @param TestCase $test
* @param string $class
*
* @return Mock
*/
private function getMock(TestCase $test, string $class)
{
return $test->getMockBuilder($class)
->disableOriginalConstructor()
->getMock();
}
}