AbstractJsAlertTest::buildMock()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 18
loc 18
rs 9.6666
c 0
b 0
f 0
1
<?php
2
namespace FwlibTest\Html\Generator\Element;
3
4
use Fwlib\Html\Generator\Element\AbstractJsAlert;
5
use Fwlib\Html\Generator\ElementMode;
6
use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase;
7
use PHPUnit_Framework_MockObject_MockObject as MockObject;
8
9
/**
10
 * @copyright   Copyright 2014-2015 Fwolf
11
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
12
 */
13
class AbstractJsAlertTest extends PHPUnitTestCase
14
{
15
    /**
16
     * @param   string[] $methods
17
     * @return  MockObject|AbstractJsAlert
18
     */
19 View Code Duplication
    protected function buildMock(array $methods = null)
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...
20
    {
21
        if (is_null($methods)) {
22
            $methods = [];
23
        }
24
        $methods = array_merge($methods, ['getJsPath']);
25
26
        $mock = $this->getMock(
27
            AbstractJsAlert::class,
28
            $methods
29
        );
30
31
        $mock->expects($this->any())
32
            ->method('getJsPath')
33
            ->willReturn('/path/to/js');
34
35
        return $mock;
36
    }
37
38
39
    public function testGetOutputForShowMode()
40
    {
41
        $element = $this->buildMock();
42
43
        $element->setConfigs([
0 ignored issues
show
Bug introduced by
The method setConfigs does only exist in Fwlib\Html\Generator\Element\AbstractJsAlert, 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...
44
            'messages' => [],
45
        ])
46
            ->setTitle('dummy');
47
        $output = $element->getOutput(ElementMode::SHOW);
0 ignored issues
show
Bug introduced by
The method getOutput does only exist in Fwlib\Html\Generator\Element\AbstractJsAlert, 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...
48
        $this->assertEmpty($output);
49
50
        // First output will load js
51
        $element->setConfig('messages', ['foo', 'bar']);
0 ignored issues
show
Bug introduced by
The method setConfig does only exist in Fwlib\Html\Generator\Element\AbstractJsAlert, 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...
52
        $output = $element->getOutput(ElementMode::SHOW);
53
        $expectedOutput = <<<TAG
54
<script type='text/javascript' src='/path/to/js'></script>
55
<script type='text/javascript'>
56
<!--
57
(function () {
58
  JsAlert(
59
    ['foo', 'bar'],
60
    'dummy',
61
    '',
62
    true,
63
    true
64
  );
65
}) ();
66
-->
67
</script>
68
TAG;
69
        $this->assertEquals($expectedOutput, $output);
70
71
        // Second output will not load js
72
        $output = $element->getOutput(ElementMode::SHOW);
73
        $this->assertRegExp(
74
            "/^(?!<script type='text\\/javascript' src=$).*/",
75
            $output
76
        );
77
    }
78
}
79