ThrowUpCommandTest.php 3.1 KB

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