Completed
Pull Request — master (#114)
by Bart
09:10
created

Unit   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 54
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 54
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\services\Categories;
8
use craft\services\Elements;
9
use craft\services\Globals;
10
use craft\services\Fields;
11
use craft\services\Sites;
12
use craft\i18n\I18n;
13
use Codeception\Module;
14
use Codeception\TestCase;
15
use NerdsAndCompany\Schematic\Schematic;
16
17
class Unit extends Module
18
{
19
    /**
20
     * Mock craft services.
21
     *
22
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
23
     *
24
     * @param TestCase $test
25
     */
26
    public function _before(TestCase $test)
27
    {
28
        $mockApp = $this->getMock($test, Application::class);
29
        $mockAssetTransforms = $this->getMock($test, AssetTransforms::class);
0 ignored issues
show
Unused Code introduced by
$mockAssetTransforms is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
30
        $mockvolumes = $this->getMock($test, Volumes::class);
0 ignored issues
show
Unused Code introduced by
$mockvolumes is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
31
        $mockCategoryGroups = $this->getMock($test, Categories::class);
32
        $mockElements = $this->getMock($test, Elements::class);
33
        $mockFields = $this->getMock($test, Fields::class);
34
        $mockGlobals = $this->getMock($test, Globals::class);
35
        $mockSites = $this->getMock($test, Sites::class);
36
        $mockI18n = $this->getMock($test, I18n::class);
37
38
        $mockApp->expects($test->any())
39
            ->method('__get')
40
            ->willReturnMap([
41
                ['categories', $mockCategoryGroups],
42
                ['elements', $mockElements],
43
                ['globals', $mockGlobals],
44
                ['fields', $mockFields],
45
                ['sites', $mockSites],
46
            ]);
47
48
        $mockApp->expects($test->any())
49
            ->method('getI18n')
50
            ->willReturn($mockI18n);
51
52
        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...
53
        Schematic::$force = false;
54
    }
55
56
    /**
57
     * Get a mock object for class.
58
     *
59
     * @param TestCase $test
60
     * @param string   $class
61
     *
62
     * @return Mock
63
     */
64
    private function getMock(TestCase $test, string $class)
65
    {
66
        return $test->getMockBuilder($class)
67
                ->disableOriginalConstructor()
68
                ->getMock();
69
    }
70
}
71