ThrowUpCommandTest.php 3.0 KB

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