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

Unit   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

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