Completed
Push — master ( 5017d8...561e2a )
by Christian
07:36 queued 03:33
created

ClearCommandTest::testExecuteClearAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace N98\Magento\Command\Developer\Asset;
4
5
use Magento\Deploy\Model\Mode;
6
use Magento\Framework\App\State;
7
use Magento\Framework\App\View\Asset\Publisher as AssetPublisher;
8
use Magento\Framework\View\Asset\Repository as AssetRepo;
9
use N98\Magento\Command\TestCase as BaseTestCase;
10
use Symfony\Component\Console\Input\ArgvInput;
11
use Symfony\Component\Console\Output\ConsoleOutput;
12
13
class ClearCommandTest extends BaseTestCase
14
{
15
    /**
16
     * @return void
17
     */
18
    protected function setUp()
19
    {
20
        if ($this->runsInProductionMode()) {
21
            $this->markTestSkipped('This command is not available in production mode');
22
        }
23
24
        parent::setUp();
25
    }
26
27
    /**
28
     * @return bool
29
     */
30 View Code Duplication
    private function runsInProductionMode()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
    {
32
        $objectManager = $this->getApplication()->getObjectManager();
0 ignored issues
show
Bug introduced by
The method getObjectManager does only exist in N98\Magento\Application, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
33
        $mode = $objectManager->create(
34
            Mode::class,
35
            [
36
                'input'  => new ArgvInput(),
37
                'output' => new ConsoleOutput(),
38
            ]
39
        );
40
41
        return $mode->getMode() === State::MODE_PRODUCTION;
42
    }
43
44
    /**
45
     * @return void
46
     */
47
    private function deployAsset()
48
    {
49
        $objectManager = $this->getApplication()->getObjectManager();
0 ignored issues
show
Bug introduced by
The method getObjectManager does only exist in N98\Magento\Application, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
50
        $assetRepo = $objectManager->get(AssetRepo::class);
51
        $assetPublisher = $objectManager->create(AssetPublisher::class);
52
        $asset = $assetRepo->createAsset(
53
            'js/block-loader.js',
54
            [
55
                'area'   => 'frontend',
56
                'theme'  => 'Magento/blank',
57
                'locale' => 'en_US',
58
                'module' => 'Magento_Ui',
59
            ]
60
        );
61
        $assetPublisher->publish($asset);
62
    }
63
64
    /**
65
     * @return void
66
     */
67
    public function testExecuteClearAll()
68
    {
69
        $this->deployAsset();
70
        $this->assertDisplayContains(
71
            [
72
                'command' => 'dev:asset:clear',
73
            ],
74
            'static/frontend deleted'
75
        );
76
    }
77
78
    /**
79
     * @return void
80
     */
81
    public function testExecuteClearTheme()
82
    {
83
        $this->deployAsset();
84
        $this->assertDisplayContains(
85
            [
86
                'command' => 'dev:asset:clear',
87
                '--theme' => ['Magento/blank'],
88
            ],
89
            'static/frontend/Magento/blank'
90
        );
91
    }
92
}
93