Completed
Push — master ( a75fe8...9d0695 )
by
unknown
13s queued 11s
created

tests/_support/Helper/Unit.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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;
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...
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