EditCommandTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * This file is part of the DigitalOcean library.
  4. *
  5. * (c) Antoine Corcy <contact@sbin.dk>
  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 DigitalOcean\Tests\CLI\SSHKeys;
  11. use Symfony\Component\Console\Application;
  12. use Symfony\Component\Console\Tester\CommandTester;
  13. use DigitalOcean\Tests\TestCase;
  14. use DigitalOcean\CLI\SSHKeys\EditCommand;
  15. /**
  16. * @author Antoine Corcy <contact@sbin.dk>
  17. */
  18. class EditCommandTest extends TestCase
  19. {
  20. protected $application;
  21. protected $command;
  22. protected $commandTester;
  23. protected function setUp()
  24. {
  25. $this->application = new Application();
  26. $result = (object) array(
  27. 'status' => 'OK',
  28. 'ssh_key' => (object) array(
  29. 'id' => 123,
  30. 'name' => 'my_ssh_key',
  31. 'ssh_pub_key' => 'the new ssh key string',
  32. ),
  33. );
  34. $EditCommand = $this->getMock('\DigitalOcean\CLI\SSHKeys\EditCommand', array('getDigitalOcean'));
  35. $EditCommand
  36. ->expects($this->any())
  37. ->method('getDigitalOcean')
  38. ->will($this->returnValue($this->getMockDigitalOcean('sshkeys', $this->getMockSSHKeys('edit', $result))));
  39. $this->application->add($EditCommand);
  40. $this->command = $this->application->find('ssh-keys:edit');
  41. $this->commandTester = new CommandTester($this->command);
  42. }
  43. /**
  44. * @expectedException \RuntimeException
  45. * @expectedExceptionMessage Not enough arguments.
  46. */
  47. public function testExecuteNotEnoughArguments()
  48. {
  49. $this->commandTester->execute(array(
  50. 'command' => $this->command->getName(),
  51. ));
  52. }
  53. /**
  54. * @expectedException \RuntimeException
  55. * @expectedExceptionMessage Not enough arguments.
  56. */
  57. public function testExecuteNotEnoughArgumentsWithoutSSHKeyId()
  58. {
  59. $this->commandTester->execute(array(
  60. 'command' => $this->command->getName(),
  61. 'ssh_pub_key' => 'foo',
  62. ));
  63. }
  64. /**
  65. * @expectedException \RuntimeException
  66. * @expectedExceptionMessage Not enough arguments.
  67. */
  68. public function testExecuteNotEnoughArgumentsWithoutNewSSHKey()
  69. {
  70. $this->commandTester->execute(array(
  71. 'command' => $this->command->getName(),
  72. 'id' => 123,
  73. ));
  74. }
  75. public function testExecuteNotConfirmed()
  76. {
  77. $dialog = $this->getDialogAskConfirmation(false);
  78. $this->command->getHelperSet()->set($dialog, 'dialog');
  79. $this->commandTester->execute(array(
  80. 'command' => $this->command->getName(),
  81. 'id' => 123,
  82. 'ssh_pub_key' => 'foo',
  83. ));
  84. $this->assertTrue(is_string($this->commandTester->getDisplay()));
  85. $this->assertRegExp('/Aborted!/', $this->commandTester->getDisplay());
  86. }
  87. public function testExecuteConfirmed()
  88. {
  89. $dialog = $this->getDialogAskConfirmation(true);
  90. $this->command->getHelperSet()->set($dialog, 'dialog');
  91. $this->commandTester->execute(array(
  92. 'command' => $this->command->getName(),
  93. 'id' => 123,
  94. 'ssh_pub_key' => 'foo',
  95. ));
  96. $this->assertTrue(is_string($this->commandTester->getDisplay()));
  97. $this->assertRegExp('/\| OK \|/', $this->commandTester->getDisplay());
  98. }
  99. }