AddCommandTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\AddCommand;
  15. /**
  16. * @author Antoine Corcy <contact@sbin.dk>
  17. */
  18. class AddCommandTest 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' => 'office-imac',
  31. 'ssh_pub_key' => 'ssh-dss AHJASDBVY6723bgBVhusadkih238723kjLKFnbkjGFklaslkhfgBAFFHGBJbju8)H3hnNGjASGFkjgZn86ZCqk02NX3BTcMV4YI2I4/sebg8VnuebDn0XUbbmVrAq4YqGiobn86ZCqk02NX3BTcMp4QGmyL4/sebg8Vnusytv93cA2PsXOxvbU0CdebDn0XUbbmVrAq4YqGiob48KzCT/NT6L6VoD5n+jSZvQAAAIAspspAelh4bW5ncO5+CedFZPZn86ZCqk02NX3BTcMV4YIaSCO43Y+ghI2of4+E1TDJ1R9Znk9XJsald/U0u0uXwtyHXP2sommNWuAGtzp4QGmyL4/sebg8Vnusytv93cA2PsXOxvbU0CdebDn0XUbbmVrAq4YqGiob48KzCT/NT6L6VoD5n+jSZflFD684gdLsW1+gjVoFBk0MZWuGSXEQyIwlBRq/8jAAAAFQDrxI/h35BewJUmVjid8Qk1NprMvQAAAIAspspAelh4bW5ncO5+CedFZPZn86ZCqk02NX3BTcMV4YI2IEzb6R2vzZkjCTuZVy6dcH3ag6JlEfju67euWT5yMnT1I0Ow== me@office-imac',
  32. ),
  33. );
  34. $AddCommand = $this->getMock('\DigitalOcean\CLI\SSHKeys\AddCommand', array('getDigitalOcean'));
  35. $AddCommand
  36. ->expects($this->any())
  37. ->method('getDigitalOcean')
  38. ->will($this->returnValue($this->getMockDigitalOcean('sshkeys', $this->getMockSSHKeys('add', $result))));
  39. $this->application->add($AddCommand);
  40. $this->command = $this->application->find('ssh-keys:add');
  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 testExecuteNotEnoughArgumentsWithoutName()
  58. {
  59. $this->commandTester->execute(array(
  60. 'command' => $this->command->getName(),
  61. 'ssh_pub_key' => 'bar',
  62. ));
  63. }
  64. /**
  65. * @expectedException \RuntimeException
  66. * @expectedExceptionMessage Not enough arguments.
  67. */
  68. public function testExecuteNotEnoughArgumentsWithoutSshKey()
  69. {
  70. $this->commandTester->execute(array(
  71. 'command' => $this->command->getName(),
  72. 'name' => 'foo',
  73. ));
  74. }
  75. public function testExecute()
  76. {
  77. $this->commandTester->execute(array(
  78. 'command' => $this->command->getName(),
  79. 'name' => 'foo',
  80. 'ssh_pub_key' => 'bar',
  81. ));
  82. $expected = <<<EOT
  83. +--------+-----+-------------+----------------------------------------------------+
  84. | Status | ID | Name | Pub Key |
  85. +--------+-----+-------------+----------------------------------------------------+
  86. | OK | 123 | office-imac | ssh-dss |
  87. | | | | AHJASDBVY6723bgBVhusadkih238723kjLKFnbkjGFklaslkhf |
  88. | | | | gBAFFHGBJbju8)H3hnNGjASGFkjgZn86ZCqk02NX3BTcMV4YI2 |
  89. | | | | I4/sebg8VnuebDn0XUbbmVrAq4YqGiobn86ZCqk02NX3BTcMp4 |
  90. | | | | QGmyL4/sebg8Vnusytv93cA2PsXOxvbU0CdebDn0XUbbmVrAq4 |
  91. | | | | YqGiob48KzCT/NT6L6VoD5n+jSZvQAAAIAspspAelh4bW5ncO5 |
  92. | | | | +CedFZPZn86ZCqk02NX3BTcMV4YIaSCO43Y+ghI2of4+E1TDJ1 |
  93. | | | | R9Znk9XJsald/U0u0uXwtyHXP2sommNWuAGtzp4QGmyL4/sebg |
  94. | | | | 8Vnusytv93cA2PsXOxvbU0CdebDn0XUbbmVrAq4YqGiob48KzC |
  95. | | | | T/NT6L6VoD5n+jSZflFD684gdLsW1+gjVoFBk0MZWuGSXEQyIw |
  96. | | | | lBRq/8jAAAAFQDrxI/h35BewJUmVjid8Qk1NprMvQAAAIAspsp |
  97. | | | | Aelh4bW5ncO5+CedFZPZn86ZCqk02NX3BTcMV4YI2IEzb6R2vz |
  98. | | | | ZkjCTuZVy6dcH3ag6JlEfju67euWT5yMnT1I0Ow== |
  99. | | | | me@office-imac |
  100. +--------+-----+-------------+----------------------------------------------------+
  101. EOT
  102. ;
  103. $this->assertTrue(is_string($this->commandTester->getDisplay()));
  104. $this->assertSame($expected, $this->commandTester->getDisplay());
  105. }
  106. }