4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Tests\Command;
14 use Psy\Command\ThrowUpCommand;
16 use Symfony\Component\Console\Tester\CommandTester;
18 class ThrowUpCommandTest extends \PHPUnit\Framework\TestCase
21 * @dataProvider executeThis
23 public function testExecute($args, $hasCode, $expect, $addSilent = true)
25 $shell = $this->getMockBuilder('Psy\\Shell')
26 ->setMethods(['hasCode', 'addCode'])
29 $shell->expects($this->once())->method('hasCode')->willReturn($hasCode);
30 $shell->expects($this->once())
32 ->with($this->equalTo($expect), $this->equalTo($addSilent));
34 $command = new ThrowUpCommand();
35 $command->setApplication($shell);
36 $tester = new CommandTester($command);
37 $tester->execute($args);
38 $this->assertEquals('', $tester->getDisplay());
41 public function executeThis()
43 $throw = 'throw \Psy\Exception\ThrowUpException::fromThrowable';
46 [[], false, $throw . '($_e);'],
48 [['exception' => '$ex'], false, $throw . '($ex);'],
49 [['exception' => 'getException()'], false, $throw . '(getException());'],
50 [['exception' => 'new \\Exception("WAT")'], false, $throw . '(new \\Exception("WAT"));'],
52 [['exception' => '\'some string\''], false, $throw . '(new \\Exception(\'some string\'));'],
53 [['exception' => '"WHEEEEEEE!"'], false, $throw . '(new \\Exception("WHEEEEEEE!"));'],
55 // Everything should work with or without semicolons.
56 [['exception' => '$ex;'], false, $throw . '($ex);'],
57 [['exception' => '"WHEEEEEEE!";'], false, $throw . '(new \\Exception("WHEEEEEEE!"));'],
59 // Don't add as silent code if we've already got code.
60 [[], true, $throw . '($_e);', false],
61 [['exception' => 'getException()'], true, $throw . '(getException());', false],
62 [['exception' => '\'some string\''], true, $throw . '(new \\Exception(\'some string\'));', false],
67 * @expectedException \InvalidArgumentException
68 * @expectedExceptionMessage No idea how to throw this
70 public function testMultipleArgsThrowsException()
72 $command = new ThrowUpCommand();
73 $command->setApplication(new Shell());
74 $tester = new CommandTester($command);
75 $tester->execute(['exception' => 'foo(); bar()']);
79 * @expectedException \PhpParser\Error
80 * @expectedExceptionMessage Syntax error, unexpected ')' on line 1
82 public function testParseErrorThrowsException()
84 $command = new ThrowUpCommand();
85 $command->setApplication(new Shell());
86 $tester = new CommandTester($command);
87 $tester->execute(['exception' => 'foo)']);