Completed
Pull Request — master (#114)
by Bart
03:27 queued 13s
created

Unit   A

Complexity

Total Complexity 2

Size/Duplication

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