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

Unit   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A getMock() 0 6 1
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
                ['sections', $mockSections],
63
                ['sites', $mockSites],
64
                ['volumes', $mockvolumes],
65
            ]);
66
67
        $mockApp->expects($test->any())
68
            ->method('getPath')
69
            ->willreturn($mockPath);
70
71
        $mockApp->expects($test->any())
72
            ->method('getI18n')
73
            ->willReturn($mockI18n);
74
75
        $mockApp->expects($test->any())
76
            ->method('getMatrix')
77
            ->willreturn($mockMatrix);
78
79
        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...
80
        Schematic::$force = false;
81
    }
82
83
    /**
84
     * Get a mock object for class.
85
     *
86
     * @param TestCase $test
87
     * @param string   $class
88
     *
89
     * @return Mock
90
     */
91
    private function getMock(TestCase $test, string $class)
92
    {
93
        return $test->getMockBuilder($class)
94
                ->disableOriginalConstructor()
95
                ->getMock();
96
    }
97
}
98