Completed
Push — master ( 91b512...bab6b4 )
by Martin
13:02 queued 07:59
created

PluginTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 52
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
B testAddNewCommand() 0 42 1
1
<?php
2
3
/*
4
 * This is part of the webuni/composer-yaml-plugin package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 * (c) Webuni s.r.o. <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Webuni\ComposerYamlPlugin\Tests;
14
15
use Composer\Composer;
16
use Composer\Console\Application;
17
use Composer\IO\BufferIO;
18
use Composer\Plugin\PluginManager;
19
use Symfony\Component\Console\Input\ArrayInput;
20
use Symfony\Component\Console\Output\BufferedOutput;
21
use Webuni\ComposerYamlPlugin\Plugin;
22
23
final class PluginTest extends \PHPUnit_Framework_TestCase
24
{
25
    private $plugin;
26
27
    protected function setUp()
28
    {
29
        $this->plugin = new Plugin();
30
    }
31
32
    public function testAddNewCommand()
33
    {
34
        /* @var $pm PluginManager|\PHPUnit_Framework_MockObject_MockObject */
35
        $pm = $this
36
            ->getMockBuilder(PluginManager::class)
37
            ->setMethods(['getPlugins'])
38
            ->disableOriginalConstructor()
39
            ->getMock()
40
        ;
41
42
        $pm
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Composer\Plugin\PluginManager.

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...
43
            ->expects($this->any())
44
            ->method('getPlugins')
45
            ->willReturn([$this->plugin])
46
        ;
47
48
        $composer = $this->getMockBuilder(Composer::class)->getMock();
49
        $composer
50
            ->expects($this->any())
51
            ->method('getPluginManager')
52
            ->willReturn($pm)
53
        ;
54
55
        $this->plugin->activate($composer, new BufferIO());
56
57
        /* @var $app Application|\PHPUnit_Framework_MockObject_MockObject */
58
        $app = $this->getMockBuilder(Application::class)
59
            ->setMethods(['getComposer'])
60
            ->getMock()
61
        ;
62
63
        $app
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Composer\Console\Application.

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...
64
            ->expects($this->any())
65
            ->method('getComposer')
66
            ->willReturn($composer)
67
        ;
68
69
        $output = new BufferedOutput();
70
71
        $app->doRun(new ArrayInput(['-d' => sys_get_temp_dir()]), $output);
0 ignored issues
show
Bug introduced by
The method doRun does only exist in Composer\Console\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...
72
        $this->assertRegExp('/yaml-convert +Converts a composer\.yml to json or vice-versa/', $output->fetch());
73
    }
74
}
75