Completed
Push — master ( 649bdf...e902d1 )
by Amine
01:28
created

Transformer::alias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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 $stdout;
16
    protected $stderr;
17
18
    public function setUp() {
19
        $adapter = new Memory;
20
        $adapter->mkdir('.', 0777, true);
21
        $this->fs = new Filesystem('.', $adapter);
22
    }
23
24
    public function command(
25
        Command $command, array $args = [],
26
        array $options = [], bool $rawArgs = true
27
    ) {
28
        $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...
29
            ->stdin(new Buffer)
30
            ->stdout(new Buffer)
31
            ->stderr(new Buffer)
32
            ->outTransformer(new Transformer);
33
34
        $this->fs->dir($command->fs()->path(), true);
35
36
        $command->console($console)
37
                ->fs($this->fs)
38
                ->run($args, $options, $rawArgs);
39
40
        $this->cmd    = $command;
41
        $this->stdout = $console->stdout()->read();
42
        $this->stderr = $console->stderr()->read();
43
44
        return $this;
45
    }
46
47
    public function prints(string $text) {
48
        $this->assertTrue(
49
            false !== strpos($this->stdout, $text),
50
            "Failed asserting that '{$this->stdout}' Contains '{$text}'"
51
        );
52
        return $this;
53
    }
54
55
    public function printsExactly(string $text) {
56
        $this->assertEquals($text, $this->stdout);
57
        return $this;
58
    }
59
60
    public function printsError(string $text) {
61
        $this->assertTrue(
62
            false !== strpos($this->stderr, $text),
63
            "Failed asserting that '{$this->stderr}' Contains '{$text}'"
64
        );
65
        return $this;
66
    }
67
68
    public function argsEqual($args) {
69
        $this->assertEquals($args, $this->cmd->args());
70
        return $this;
71
    }
72
73
    public function optionsEqual($options) {
74
        $this->assertEquals($options, $this->cmd->options());
75
        return $this;
76
    }
77
}
78