Completed
Push — do-not-scrutinize-tests ( fcf005 )
by Bart
04:15 queued 02:38
created

Unit   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 118
rs 10
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\ElementIndexes;
13
use craft\services\Fields;
14
use craft\services\Globals;
15
use craft\services\Matrix;
16
use craft\services\Path;
17
use craft\services\Plugins;
18
use craft\services\Sections;
19
use craft\services\Sites;
20
use craft\services\SystemSettings;
21
use craft\services\Tags;
22
use craft\services\UserGroups;
23
use craft\services\UserPermissions;
24
use craft\services\Volumes;
25
use Codeception\Module;
26
use Codeception\TestCase;
27
use NerdsAndCompany\Schematic\Schematic;
28
use NerdsAndCompany\Schematic\Mappers\ModelMapper;
29
30
/**
31
 * UnitTest helper.
32
 *
33
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
34
 */
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