1 | <?php |
||
35 | class Unit extends Module |
||
36 | { |
||
37 | /** |
||
38 | * Mock craft Mappers. |
||
39 | * |
||
40 | * @SuppressWarnings(PHPMD.CamelCaseMethodName) |
||
41 | * phpcs:disable PSR2.Methods.MethodDeclaration.Underscore |
||
42 | * |
||
43 | * @param TestCase $test |
||
44 | */ |
||
45 | public function _before(TestCase $test) |
||
46 | { |
||
47 | $mockApp = $this->getMockApp($test); |
||
48 | $mockApp->controller = $this->getMock($test, Controller::class); |
||
49 | $mockApp->controller->module = $this->getmockModule($test); |
||
50 | |||
51 | Craft::$app = $mockApp; |
||
52 | Schematic::$force = false; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Get a preconfigured mock module. |
||
57 | * |
||
58 | * @param TestCase $test |
||
59 | * |
||
60 | * @return Mock|Schematic |
||
61 | */ |
||
62 | private function getMockModule(TestCase $test) |
||
63 | { |
||
64 | $mockModule = $this->getMock($test, Schematic::class); |
||
65 | $mockModelMapper = $this->getMock($test, ModelMapper::class); |
||
66 | $mockModule->expects($test->any()) |
||
67 | ->method('__get') |
||
68 | ->willReturnMap([ |
||
69 | ['modelMapper', $mockModelMapper], |
||
70 | ]); |
||
71 | |||
72 | return $mockModule; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Get a preconfigured mock app. |
||
77 | * |
||
78 | * @param TestCase $test |
||
79 | * |
||
80 | * @return Mock|Application |
||
81 | */ |
||
82 | private function getMockApp(TestCase $test) |
||
83 | { |
||
84 | $mockApp = $this->getMock($test, Application::class); |
||
85 | $mockAssetTransforms = $this->getMock($test, AssetTransforms::class); |
||
86 | $mockCategoryGroups = $this->getMock($test, Categories::class); |
||
87 | $mockElements = $this->getMock($test, Elements::class); |
||
88 | $mockElementIndexes = $this->getMock($test, ElementIndexes::class); |
||
89 | $mockFields = $this->getMock($test, Fields::class); |
||
90 | $mockGlobals = $this->getMock($test, Globals::class); |
||
91 | $mockI18n = $this->getMock($test, I18n::class); |
||
92 | $mockMatrix = $this->getMock($test, Matrix::class); |
||
93 | $mockPath = $this->getMock($test, Path::class); |
||
94 | $mockPlugins = $this->getMock($test, Plugins::class); |
||
95 | $mockSections = $this->getMock($test, Sections::class); |
||
96 | $mockSites = $this->getMock($test, Sites::class); |
||
97 | $mockSystemSettings = $this->getMock($test, SystemSettings::class); |
||
98 | $mockTags = $this->getMock($test, Tags::class); |
||
99 | $mockUserGroups = $this->getMock($test, UserGroups::class); |
||
100 | $mockUserPermissions = $this->getMock($test, UserPermissions::class); |
||
101 | $mockVolumes = $this->getMock($test, Volumes::class); |
||
102 | |||
103 | $mockApp->expects($test->any()) |
||
104 | ->method('__get') |
||
105 | ->willReturnMap([ |
||
106 | ['assetTransforms', $mockAssetTransforms], |
||
107 | ['categories', $mockCategoryGroups], |
||
108 | ['elements', $mockElements], |
||
109 | ['elementIndexes', $mockElementIndexes], |
||
110 | ['fields', $mockFields], |
||
111 | ['globals', $mockGlobals], |
||
112 | ['matrix', $mockMatrix], |
||
113 | ['plugins', $mockPlugins], |
||
114 | ['sections', $mockSections], |
||
115 | ['sites', $mockSites], |
||
116 | ['systemSettings', $mockSystemSettings], |
||
117 | ['tags', $mockTags], |
||
118 | ['userGroups', $mockUserGroups], |
||
119 | ['userPermissions', $mockUserPermissions], |
||
120 | ['volumes', $mockVolumes], |
||
121 | ]); |
||
122 | |||
123 | $mockApp->expects($test->any()) |
||
124 | ->method('getPath') |
||
125 | ->willreturn($mockPath); |
||
126 | |||
127 | $mockApp->expects($test->any()) |
||
128 | ->method('getI18n') |
||
129 | ->willReturn($mockI18n); |
||
130 | |||
131 | $mockApp->expects($test->any()) |
||
132 | ->method('getMatrix') |
||
133 | ->willreturn($mockMatrix); |
||
134 | |||
135 | return $mockApp; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Get a mock object for class. |
||
140 | * |
||
141 | * @param TestCase $test |
||
142 | * @param string $class |
||
143 | * |
||
144 | * @return Mock |
||
145 | */ |
||
146 | private function getMock(TestCase $test, string $class) |
||
147 | { |
||
148 | return $test->getMockBuilder($class) |
||
149 | ->disableOriginalConstructor() |
||
150 | ->getMock(); |
||
151 | } |
||
152 | } |
||
153 |