AssetsTest::testCssMinification()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
namespace Robo;
3
4
use PHPUnit\Framework\TestCase;
5
use Robo\Traits\TestTasksTrait;
6
7
class AssetsTest extends TestCase
8
{
9
    use TestTasksTrait;
10
    use Task\Assets\loadTasks;
11
12
    protected $fixtures;
13
14
    public function setUp()
15
    {
16
        $this->fixtures = new Fixtures();
17
        $this->initTestTasksTrait();
18
    }
19
20
    public function tearDown()
21
    {
22
        $this->fixtures->cleanup();
23
    }
24
25
    public function testCssMinification()
26
    {
27
        if (version_compare(PHP_VERSION, '7.2.0') >= 0) {
28
          $this->markTestSkipped('natxet/cssmin uses deprecated "each()" function (PHP 7.2+)');
29
        }
30
31
        $this->fixtures->createAndCdToSandbox();
32
33
				$sampleCss = $this->fixtures->dataFile('sample.css');
34
				$outputCss = 'minifiedSample.css';
35
36
				$initialFileSize = filesize($sampleCss);
37
38
				$result = $this->taskMinify($sampleCss)
0 ignored issues
show
Bug introduced by
The method to does only exist in Robo\Task\Assets\Minify, but not in Robo\Collection\CollectionBuilder.

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...
39
				  ->to('minifiedSample.css')
40
				  ->run();
41
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
42
43
				$this->assertFileExists($outputCss);
44
				$minifiedFileSize = filesize($outputCss);
45
				$outputCssContents = file_get_contents($outputCss);
46
47
				$this->assertLessThan($initialFileSize, $minifiedFileSize, 'Minified file is smaller than the source file');
48
				$this->assertGreaterThan(0, $minifiedFileSize, 'Minified file is not empty');
49
				$this->assertContains('body', $outputCssContents, 'Minified file has some content from the source file');
50
				$this->assertNotContains('Sample css file', $outputCssContents, 'Minified file does not contain comment from source file');
51
    }
52
53
}
54