Completed
Push — master ( 8b8afe...5f2bbe )
by Greg
02:21
created

tests/unit/OutputTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
use Robo\Robo;
3
4
class OutputTest extends \Codeception\TestCase\Test
5
{
6
    use \Robo\Common\IO {
7
        say as public;
8
        yell as public;
9
        ask as public;
10
        output as protected;
11
    }
12
13
    protected $expectedAnswer;
14
15
    /**
16
     * @var \CodeGuy
17
     */
18
    protected $guy;
19
20
    /**
21
     * @vAspectMock\Proxy\ClassProxyroxy
22
     */
23
    protected $dialog;
24
25
    protected function _before()
26
    {
27
        $this->dialog = new Symfony\Component\Console\Helper\QuestionHelper;
28
        $this->setOutput(Robo::service('output'));
29
    }
30
31
    public function testSay()
32
    {
33
        $this->say('Hello, world!');
34
        $this->guy->seeInOutput('>  Hello, world!');
35
    }
36
37
    public function testAskReply()
38
    {
39
        $this->expectedAnswer = 'jon';
40
        verify($this->ask('What is your name?'))->equals('jon');
41
        $this->guy->seeOutputEquals('?  What is your name? ');
42
    }
43 View Code Duplication
    public function testAskMethod()
44
    {
45
        if (method_exists($this, 'createMock')) {
46
            $this->dialog = $this->createMock('\Symfony\Component\Console\Helper\QuestionHelper', ['ask']);
47
        } else {
48
            $this->dialog = $this->getMock('\Symfony\Component\Console\Helper\QuestionHelper', ['ask']);
49
        }
50
        $this->dialog->expects($this->once())
51
            ->method('ask');
52
        $this->ask('What is your name?');
53
    }
54 View Code Duplication
    public function testAskHiddenMethod()
55
    {
56
        if (method_exists($this, 'createMock')) {
57
            $this->dialog = $this->createMock('\Symfony\Component\Console\Helper\QuestionHelper', ['ask']);
58
        } else {
59
            $this->dialog = $this->getMock('\Symfony\Component\Console\Helper\QuestionHelper', ['ask']);
60
        }
61
        $this->dialog->expects($this->once())
62
            ->method('ask');
63
        $this->ask('What is your name?', true);
64
    }
65
    public function testYell()
66
    {
67
        $this->yell('Buuuu!');
68
        $this->guy->seeInOutput('Buuuu!');
69
    }
70
    protected function getDialog()
71
    {
72
        $stream = fopen('php://memory', 'r+', false);
73
        fputs($stream, $this->expectedAnswer);
74
        rewind($stream);
75
        // Use StreamableInputInterface when it's available
76
        if ($this->input() instanceof \Symfony\Component\Console\Input\StreamableInputInterface) {
77
            $this->input()->setStream($stream);
78
        } else {
79
            // setInputStream deprecated in Symfony 3.2.
80
            $this->dialog->setInputStream($stream);
0 ignored issues
show
The method setInputStream does only exist in Symfony\Component\Console\Helper\QuestionHelper, 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...
81
        }
82
        return $this->dialog;
83
    }
84
}
85