Completed
Push — master ( dd8846...82fafa )
by Amine
03:04
created

CommandTestCase   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 8
dl 0
loc 94
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
B command() 0 24 1
A prints() 0 8 1
A printsExactly() 0 5 1
A printsError() 0 8 1
A argsEqual() 0 5 1
A optionsEqual() 0 5 1
A withStdin() 0 4 1
A havingFile() 0 5 1
A havingDir() 0 5 1
1
<?php namespace Tarsana\Tester;
2
3
use PHPUnit\Framework\TestCase;
4
use Tarsana\Command\Command;
5
use Tarsana\Command\Console\Console;
6
use Tarsana\IO\Filesystem;
7
use Tarsana\IO\Filesystem\Adapters\Memory;
8
use Tarsana\IO\Resource\Buffer;
9
use Tarsana\Tester\Mocks\Transformer;
10
11
class CommandTestCase extends TestCase {
12
13
    protected $fs;
14
    protected $cmd;
15
    protected $stdin;
16
    protected $stdout;
17
    protected $stderr;
18
19
    public function setUp()
20
    {
21
        $adapter = new Memory;
22
        $adapter->mkdir('.', 0777, true);
23
        $this->fs = new Filesystem('.', $adapter);
24
    }
25
26
    protected function command(
27
        Command $command, array $args = [],
28
        array $options = [], bool $rawArgs = true
29
    ) {
30
        $stdin = new Buffer;
31
        $stdin->write($this->stdin);
32
        $console = (new Console)
0 ignored issues
show
Bug introduced by
The method stdout does only exist in Tarsana\Command\Console\Console, but not in Tarsana\IO\Resource\Reader.

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...
33
            ->stdin($stdin)
34
            ->stdout(new Buffer)
35
            ->stderr(new Buffer)
36
            ->outTransformer(new Transformer);
37
38
        $this->fs->dir($command->fs()->path(), true);
39
40
        $command->console($console)
41
                ->fs($this->fs)
42
                ->run($args, $options, $rawArgs);
43
44
        $this->cmd    = $command;
45
        $this->stdout = $console->stdout()->read();
46
        $this->stderr = $console->stderr()->read();
47
48
        return $this;
49
    }
50
51
    protected function prints(string $text)
52
    {
53
        $this->assertTrue(
54
            false !== strpos($this->stdout, $text),
55
            "Failed asserting that '{$this->stdout}' Contains '{$text}'"
56
        );
57
        return $this;
58
    }
59
60
    protected function printsExactly(string $text)
61
    {
62
        $this->assertEquals($text, $this->stdout);
63
        return $this;
64
    }
65
66
    protected function printsError(string $text)
67
    {
68
        $this->assertTrue(
69
            false !== strpos($this->stderr, $text),
70
            "Failed asserting that '{$this->stderr}' Contains '{$text}'"
71
        );
72
        return $this;
73
    }
74
75
    protected function argsEqual($args)
76
    {
77
        $this->assertEquals($args, $this->cmd->args());
78
        return $this;
79
    }
80
81
    protected function optionsEqual(array $options)
82
    {
83
        $this->assertEquals($options, $this->cmd->options());
84
        return $this;
85
    }
86
87
    protected function withStdin(string $text) {
88
        $this->stdin = $text;
89
        return $this;
90
    }
91
92
    protected function havingFile(string $path, string $content = '')
93
    {
94
        $this->fs->file($path, true)->content($content);
95
        return $this;
96
    }
97
98
    protected function havingDir(string $path)
99
    {
100
        $this->fs->dir($path, true);
101
        return $this;
102
    }
103
104
}
105