Completed
Pull Request — master (#114)
by Bart
02:00
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\Volumes;
18
use Codeception\Module;
19
use Codeception\TestCase;
20
use NerdsAndCompany\Schematic\Schematic;
21
use NerdsAndCompany\Schematic\Services\ModelProcessor;
22
23
/**
24
 * UnitTest helper.
25
 *
26
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27
 */
28
class Unit extends Module
29
{
30
    /**
31
     * Mock craft services.
32
     *
33
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
34
     *
35
     * @param TestCase $test
36
     */
37
    public function _before(TestCase $test)
38
    {
39
        $mockApp = $this->getMock($test, Application::class);
40
        $mockAssetTransforms = $this->getMock($test, AssetTransforms::class);
41
        $mockCategoryGroups = $this->getMock($test, Categories::class);
42
        $mockElements = $this->getMock($test, Elements::class);
43
        $mockFields = $this->getMock($test, Fields::class);
44
        $mockGlobals = $this->getMock($test, Globals::class);
45
        $mockI18n = $this->getMock($test, I18n::class);
46
        $mockMatrix = $this->getMock($test, Matrix::class);
47
        $mockModelProcessor = $this->getMock($test, ModelProcessor::class);
48
        $mockPath = $this->getMock($test, Path::class);
49
        $mockSections = $this->getMock($test, Sections::class);
50
        $mockSites = $this->getMock($test, Sites::class);
51
        $mockvolumes = $this->getMock($test, Volumes::class);
52
53
        $mockApp->expects($test->any())
54
            ->method('__get')
55
            ->willReturnMap([
56
                ['assetTransforms', $mockAssetTransforms],
57
                ['categories', $mockCategoryGroups],
58
                ['elements', $mockElements],
59
                ['fields', $mockFields],
60
                ['globals', $mockGlobals],
61
                ['schematic_fields', $mockModelProcessor],
62
                ['schematic_sections', $mockModelProcessor],
63
                ['sections', $mockSections],
64
                ['sites', $mockSites],
65
                ['volumes', $mockvolumes],
66
            ]);
67
68
        $mockApp->expects($test->any())
69
            ->method('getPath')
70
            ->willreturn($mockPath);
71
72
        $mockApp->expects($test->any())
73
            ->method('getI18n')
74
            ->willReturn($mockI18n);
75
76
        $mockApp->expects($test->any())
77
            ->method('getMatrix')
78
            ->willreturn($mockMatrix);
79
80
        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...
81
        Schematic::$force = false;
82
    }
83
84
    /**
85
     * Get a mock object for class.
86
     *
87
     * @param TestCase $test
88
     * @param string   $class
89
     *
90
     * @return Mock
91
     */
92
    private function getMock(TestCase $test, string $class)
93
    {
94
        return $test->getMockBuilder($class)
95
                ->disableOriginalConstructor()
96
                ->getMock();
97
    }
98
}
99