Completed
Push — master ( 461024...58a10f )
by Timothy
05:24 queued 02:19
created

RandomComplimentCommandTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 68
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A testRunWithDefaultValues() 0 10 1
A testRunWithDirectoryInsteadOfFile() 0 13 1
A testRunWithNonReadableFile() 0 15 1
1
<?php
2
3
namespace AbacaphiliacTest\Compliments;
4
5
use Abacaphiliac\Compliments\RandomComplimentCommand;
6
use org\bovigo\vfs\vfsStream;
7
use Symfony\Component\Console\Input\ArgvInput;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class RandomComplimentCommandTest extends \PHPUnit_Framework_TestCase
11
{
12
    /** @var  \PHPUnit_Framework_MockObject_MockObject|OutputInterface */
13
    private $output;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
14
    
15
    /** @var RandomComplimentCommand */
16
    private $sut;
17
18
    protected function setUp()
19
    {
20
        parent::setUp();
21
22
        $this->output = $this->getMock('\Symfony\Component\Console\Output\OutputInterface');
23
        
24
        $this->sut = new RandomComplimentCommand();
25
    }
26
    
27
    public function testRunWithDefaultValues()
28
    {
29
        $this->output->expects(self::atLeastOnce())->method('writeln');
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Console\Output\OutputInterface.

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...
30
31
        $input = new ArgvInput(array());
32
        
33
        $actual = $this->sut->run($input, $this->output);
34
        
35
        self::assertEmpty($actual);
36
    }
37
38
    /**
39
     * @expectedException \InvalidArgumentException
40
     * @throws \InvalidArgumentException
41
     * @throws \Exception
42
     */
43
    public function testRunWithDirectoryInsteadOfFile()
44
    {
45
        $dir = vfsStream::setup('tmp');
46
47
        $input = new ArgvInput(array(
48
            __FILE__,
49
            '--file=' . $dir->url(),
50
        ));
51
        
52
        $actual = $this->sut->run($input, $this->output);
53
54
        self::assertSame(0, $actual);
55
    }
56
57
    /**
58
     * @expectedException \InvalidArgumentException
59
     * @throws \InvalidArgumentException
60
     * @throws \Exception
61
     */
62
    public function testRunWithNonReadableFile()
63
    {
64
        $dir = vfsStream::setup('tmp');
65
        $file = vfsStream::newFile('compliments.txt', 0000);
66
        $dir->addChild($file);
67
68
        $input = new ArgvInput(array(
69
            __FILE__,
70
            '--file=' . $file->url(),
71
        ));
72
        
73
        $actual = $this->sut->run($input, $this->output);
74
75
        self::assertSame(0, $actual);
76
    }
77
}
78