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

Unit   A

Complexity

Total Complexity 2

Size/Duplication

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