BowerphpConsoleOutputTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
cbo 5
dl 0
loc 123
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 8 2
A testWritelnInfoPackage() 0 18 1
A testWritelnInstalledPackage() 0 17 1
A testWritelnNoBowerJsonFile() 0 11 1
A testWritelnJson() 0 11 1
A testWritelnJsonText() 0 11 1
A testWritelnListPackage() 0 24 1
A testWritelnSearchOrLookup() 0 13 1
1
<?php
2
3
/*
4
 * This file is part of the Bowerphp package.
5
 *
6
 * (c) Mauro D'Alatri <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bowerphp\Test\Output;
13
14
use Bowerphp\Output\BowerphpConsoleOutput;
15
use Mockery;
16
use PHPUnit\Framework\TestCase;
17
18
class BowerphpConsoleOutputTest extends TestCase
19
{
20
    protected function tearDown()
21
    {
22
        // this is to make Mockery assertion count as PHPUnit assertion, avoiding "risky"
23
        if (!is_null($container = Mockery::getContainer())) {
24
            $this->addToAssertionCount($container->mockery_getExpectationCount());
25
        }
26
        Mockery::close();
27
    }
28
29
    public function testWritelnInfoPackage()
30
    {
31
        $output = Mockery::mock('Symfony\Component\Console\Output\OutputInterface');
32
        $package = Mockery::mock('Bowerphp\Package\PackageInterface');
33
34
        $package
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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...
35
            ->shouldReceive('getVersion')->andReturn('2.1')
36
            ->shouldReceive('getName')->andReturn('jquery')
37
            ->shouldReceive('getRequiredVersion')->andReturn('2.1')
38
        ;
39
40
        $output
41
            ->shouldReceive('writeln')->with('bower <info>jquery#2.1           </info> <fg=cyan>          </fg=cyan> ')
42
        ;
43
44
        $bConsoleOutput = new BowerphpConsoleOutput($output);
45
        $bConsoleOutput->writelnInfoPackage($package);
46
    }
47
48
    public function testWritelnInstalledPackage()
49
    {
50
        $output = Mockery::mock('Symfony\Component\Console\Output\OutputInterface');
51
        $package = Mockery::mock('Bowerphp\Package\PackageInterface');
52
53
        $package
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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...
54
            ->shouldReceive('getVersion')->andReturn('2.1')
55
            ->shouldReceive('getName')->andReturn('jquery')
56
        ;
57
58
        $output
59
            ->shouldReceive('writeln')->with('bower <info>jquery#2.1           </info> <fg=cyan>   install</fg=cyan>')
60
        ;
61
62
        $bConsoleOutput = new BowerphpConsoleOutput($output);
63
        $bConsoleOutput->writelnInstalledPackage($package);
64
    }
65
66
    public function testWritelnNoBowerJsonFile()
67
    {
68
        $output = Mockery::mock('Symfony\Component\Console\Output\OutputInterface');
69
70
        $output
71
            ->shouldReceive('writeln')->with('bower <info>                     </info> <fg=yellow>   no-json</fg=yellow> No bower.json file to save to, use bower init to create one')
72
        ;
73
74
        $bConsoleOutput = new BowerphpConsoleOutput($output);
75
        $bConsoleOutput->writelnNoBowerJsonFile();
76
    }
77
78
    public function testWritelnJson()
79
    {
80
        $output = Mockery::mock('Symfony\Component\Console\Output\OutputInterface');
81
82
        $output
83
            ->shouldReceive('writeln')->with('{<info>name</info>: <fg=cyan>\'foo\'</fg=cyan>}')
84
        ;
85
86
        $bConsoleOutput = new BowerphpConsoleOutput($output);
87
        $bConsoleOutput->writelnJson('{"name": "foo"}');
88
    }
89
90
    public function testWritelnJsonText()
91
    {
92
        $output = Mockery::mock('Symfony\Component\Console\Output\OutputInterface');
93
94
        $output
95
            ->shouldReceive('writeln')->with('<fg=cyan>"name"</fg=cyan>')
96
        ;
97
98
        $bConsoleOutput = new BowerphpConsoleOutput($output);
99
        $bConsoleOutput->writelnJsonText('name');
100
    }
101
102
    public function testWritelnListPackage()
103
    {
104
        $output = Mockery::mock('Symfony\Component\Console\Output\OutputInterface');
105
        $package = Mockery::mock('Bowerphp\Package\PackageInterface');
106
        $bowerphp = Mockery::mock('Bowerphp\Bowerphp');
107
108
        $package
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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...
109
            ->shouldReceive('getName')->andReturn('jquery', 'fonts.css')
110
            ->shouldReceive('getVersion')->andReturn('1.2', '1.0.0')
111
        ;
112
113
        $bowerphp
114
            ->shouldReceive('isPackageExtraneous')->with($package)->andReturn(true, false)
115
        ;
116
117
        $output
118
            ->shouldReceive('writeln')->with('jquery#1.2<info> extraneous</info>')
119
            ->shouldReceive('writeln')->with('fonts.css#1.0.0<info></info>')
120
        ;
121
122
        $bConsoleOutput = new BowerphpConsoleOutput($output);
123
        $bConsoleOutput->writelnListPackage($package, $bowerphp);
124
        $bConsoleOutput->writelnListPackage($package, $bowerphp);
125
    }
126
127
    public function testWritelnSearchOrLookup()
128
    {
129
        $output = Mockery::mock('Symfony\Component\Console\Output\OutputInterface');
130
131
        $output
132
            ->shouldReceive('writeln')->with('<fg=cyan>foo</fg=cyan> bar')
133
            ->shouldReceive('writeln')->with('   <fg=cyan>foo</fg=cyan> bar')
134
        ;
135
136
        $bConsoleOutput = new BowerphpConsoleOutput($output);
137
        $bConsoleOutput->writelnSearchOrLookup('foo', 'bar');
138
        $bConsoleOutput->writelnSearchOrLookup('foo', 'bar', 3);
139
    }
140
}
141