Completed
Pull Request — master (#114)
by Bart
02:16
created

Unit   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 117
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getMockModule() 0 12 1
A getMockApp() 0 55 1
A getMock() 0 6 1
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
     *
42
     * @param TestCase $test
43
     */
44
    public function _before(TestCase $test)
45
    {
46
        $mockApp = $this->getMockApp($test);
47
        $mockApp->controller = $this->getMock($test, Controller::class);
48
        $mockApp->controller->module = $this->getmockModule($test);
49
50
        Craft::$app = $mockApp;
0 ignored issues
show
Documentation Bug introduced by
It seems like $mockApp can also be of type object<Helper\Mock>. However, the property $app is declared as type object<craft\web\Applica...ft\console\Application>. Maybe add an additional type check?

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 $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

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