Chris@16
|
1 <?php
|
Chris@16
|
2
|
Chris@16
|
3 /*
|
Chris@16
|
4 * This file is part of Psy Shell.
|
Chris@16
|
5 *
|
Chris@16
|
6 * (c) 2012-2018 Justin Hileman
|
Chris@16
|
7 *
|
Chris@16
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@16
|
9 * file that was distributed with this source code.
|
Chris@16
|
10 */
|
Chris@16
|
11
|
Chris@16
|
12 namespace Symfony\Component\Console\Tests\Command;
|
Chris@16
|
13
|
Chris@16
|
14 use Psy\Command\ThrowUpCommand;
|
Chris@16
|
15 use Psy\Shell;
|
Chris@16
|
16 use Symfony\Component\Console\Tester\CommandTester;
|
Chris@16
|
17
|
Chris@16
|
18 class ThrowUpCommandTest extends \PHPUnit\Framework\TestCase
|
Chris@16
|
19 {
|
Chris@16
|
20 /**
|
Chris@16
|
21 * @dataProvider executeThis
|
Chris@16
|
22 */
|
Chris@16
|
23 public function testExecute($args, $hasCode, $expect, $addSilent = true)
|
Chris@16
|
24 {
|
Chris@16
|
25 $shell = $this->getMockBuilder('Psy\\Shell')
|
Chris@16
|
26 ->setMethods(['hasCode', 'addCode'])
|
Chris@16
|
27 ->getMock();
|
Chris@16
|
28
|
Chris@16
|
29 $shell->expects($this->once())->method('hasCode')->willReturn($hasCode);
|
Chris@16
|
30 $shell->expects($this->once())
|
Chris@16
|
31 ->method('addCode')
|
Chris@16
|
32 ->with($this->equalTo($expect), $this->equalTo($addSilent));
|
Chris@16
|
33
|
Chris@16
|
34 $command = new ThrowUpCommand();
|
Chris@16
|
35 $command->setApplication($shell);
|
Chris@16
|
36 $tester = new CommandTester($command);
|
Chris@16
|
37 $tester->execute($args);
|
Chris@16
|
38 $this->assertEquals('', $tester->getDisplay());
|
Chris@16
|
39 }
|
Chris@16
|
40
|
Chris@16
|
41 public function executeThis()
|
Chris@16
|
42 {
|
Chris@16
|
43 $throw = 'throw \Psy\Exception\ThrowUpException::fromThrowable';
|
Chris@16
|
44
|
Chris@16
|
45 return [
|
Chris@16
|
46 [[], false, $throw . '($_e);'],
|
Chris@16
|
47
|
Chris@16
|
48 [['exception' => '$ex'], false, $throw . '($ex);'],
|
Chris@16
|
49 [['exception' => 'getException()'], false, $throw . '(getException());'],
|
Chris@16
|
50 [['exception' => 'new \\Exception("WAT")'], false, $throw . '(new \\Exception("WAT"));'],
|
Chris@16
|
51
|
Chris@16
|
52 [['exception' => '\'some string\''], false, $throw . '(new \\Exception(\'some string\'));'],
|
Chris@16
|
53 [['exception' => '"WHEEEEEEE!"'], false, $throw . '(new \\Exception("WHEEEEEEE!"));'],
|
Chris@16
|
54
|
Chris@16
|
55 // Everything should work with or without semicolons.
|
Chris@16
|
56 [['exception' => '$ex;'], false, $throw . '($ex);'],
|
Chris@16
|
57 [['exception' => '"WHEEEEEEE!";'], false, $throw . '(new \\Exception("WHEEEEEEE!"));'],
|
Chris@16
|
58
|
Chris@16
|
59 // Don't add as silent code if we've already got code.
|
Chris@16
|
60 [[], true, $throw . '($_e);', false],
|
Chris@16
|
61 [['exception' => 'getException()'], true, $throw . '(getException());', false],
|
Chris@16
|
62 [['exception' => '\'some string\''], true, $throw . '(new \\Exception(\'some string\'));', false],
|
Chris@16
|
63 ];
|
Chris@16
|
64 }
|
Chris@16
|
65
|
Chris@16
|
66 /**
|
Chris@16
|
67 * @expectedException \InvalidArgumentException
|
Chris@16
|
68 * @expectedExceptionMessage No idea how to throw this
|
Chris@16
|
69 */
|
Chris@16
|
70 public function testMultipleArgsThrowsException()
|
Chris@16
|
71 {
|
Chris@16
|
72 $command = new ThrowUpCommand();
|
Chris@16
|
73 $command->setApplication(new Shell());
|
Chris@16
|
74 $tester = new CommandTester($command);
|
Chris@16
|
75 $tester->execute(['exception' => 'foo(); bar()']);
|
Chris@16
|
76 }
|
Chris@16
|
77
|
Chris@16
|
78 /**
|
Chris@16
|
79 * @expectedException \PhpParser\Error
|
Chris@16
|
80 * @expectedExceptionMessage Syntax error, unexpected ')' on line 1
|
Chris@16
|
81 */
|
Chris@16
|
82 public function testParseErrorThrowsException()
|
Chris@16
|
83 {
|
Chris@16
|
84 $command = new ThrowUpCommand();
|
Chris@16
|
85 $command->setApplication(new Shell());
|
Chris@16
|
86 $tester = new CommandTester($command);
|
Chris@16
|
87 $tester->execute(['exception' => 'foo)']);
|
Chris@16
|
88 }
|
Chris@16
|
89 }
|