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