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

Unit::getMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace Helper;
4
5
use Craft;
6
use craft\console\Application;
7
use craft\i18n\I18n;
8
use craft\services\AssetTransforms;
9
use craft\services\Categories;
10
use craft\services\Elements;
11
use craft\services\Fields;
12
use craft\services\Globals;
13
use craft\services\Matrix;
14
use craft\services\Path;
15
use craft\services\Sections;
16
use craft\services\Sites;
17
use craft\services\Tags;
18
use craft\services\UserGroups;
19
use craft\services\UserPermissions;
20
use craft\services\Volumes;
21
use Codeception\Module;
22
use Codeception\TestCase;
23
use NerdsAndCompany\Schematic\Schematic;
24
use NerdsAndCompany\Schematic\Services\ModelProcessor;
25
26
/**
27
 * UnitTest helper.
28
 *
29
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
30
 */
31
class Unit extends Module
32
{
33
    /**
34
     * Mock craft services.
35
     *
36
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
37
     *
38
     * @param TestCase $test
39
     */
40
    public function _before(TestCase $test)
41
    {
42
        $mockApp = $this->getMock($test, Application::class);
43
        $mockAssetTransforms = $this->getMock($test, AssetTransforms::class);
44
        $mockCategoryGroups = $this->getMock($test, Categories::class);
45
        $mockElements = $this->getMock($test, Elements::class);
46
        $mockFields = $this->getMock($test, Fields::class);
47
        $mockGlobals = $this->getMock($test, Globals::class);
48
        $mockI18n = $this->getMock($test, I18n::class);
49
        $mockMatrix = $this->getMock($test, Matrix::class);
50
        $mockModelProcessor = $this->getMock($test, ModelProcessor::class);
51
        $mockPath = $this->getMock($test, Path::class);
52
        $mockSections = $this->getMock($test, Sections::class);
53
        $mockSites = $this->getMock($test, Sites::class);
54
        $mockTags = $this->getMock($test, Tags::class);
55
        $mockUserGroups = $this->getMock($test, UserGroups::class);
56
        $mockUserPermissions = $this->getMock($test, UserPermissions::class);
57
        $mockVolumes = $this->getMock($test, Volumes::class);
58
59
        $mockApp->expects($test->any())
60
            ->method('__get')
61
            ->willReturnMap([
62
                ['assetTransforms', $mockAssetTransforms],
63
                ['categories', $mockCategoryGroups],
64
                ['elements', $mockElements],
65
                ['fields', $mockFields],
66
                ['globals', $mockGlobals],
67
                ['matrix', $mockMatrix],
68
                ['schematic_fields', $mockModelProcessor],
69
                ['schematic_sections', $mockModelProcessor],
70
                ['sections', $mockSections],
71
                ['sites', $mockSites],
72
                ['tags', $mockTags],
73
                ['userGroups', $mockUserGroups],
74
                ['userPermissions', $mockUserPermissions],
75
                ['volumes', $mockVolumes],
76
            ]);
77
78
        $mockApp->expects($test->any())
79
            ->method('getPath')
80
            ->willreturn($mockPath);
81
82
        $mockApp->expects($test->any())
83
            ->method('getI18n')
84
            ->willReturn($mockI18n);
85
86
        $mockApp->expects($test->any())
87
            ->method('getMatrix')
88
            ->willreturn($mockMatrix);
89
90
        Craft::$app = $mockApp;
0 ignored issues
show
Documentation Bug introduced by
It seems like $mockApp of type object<Helper\Mock> is incompatible with the declared type object<craft\web\Applica...ft\console\Application> of property $app.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
91
        Schematic::$force = false;
92
    }
93
94
    /**
95
     * Get a mock object for class.
96
     *
97
     * @param TestCase $test
98
     * @param string   $class
99
     *
100
     * @return Mock
101
     */
102
    private function getMock(TestCase $test, string $class)
103
    {
104
        return $test->getMockBuilder($class)
105
                ->disableOriginalConstructor()
106
                ->getMock();
107
    }
108
}
109