CreateInteractiveCommandTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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\Droplets;
  11. use Symfony\Component\Console\Application;
  12. use Symfony\Component\Console\Tester\CommandTester;
  13. use DigitalOcean\Tests\TestCase;
  14. use DigitalOcean\CLI\Droplets\CreateInteractiveCommand;
  15. /**
  16. * @author Antoine Corcy <contact@sbin.dk>
  17. */
  18. class CreateInteractiveCommandTest extends TestCase
  19. {
  20. protected $application;
  21. protected function setUp()
  22. {
  23. $this->application = new Application();
  24. }
  25. public function testExecuteWithoutDropletName()
  26. {
  27. $CreateInteractiveCommand = $this->getMock('\DigitalOcean\CLI\Droplets\CreateInteractiveCommand', array('getDigitalOcean'));
  28. $CreateInteractiveCommand
  29. ->expects($this->once())
  30. ->method('getDigitalOcean')
  31. ->will($this->returnValue(
  32. $this->getMockDigitalOcean('droplets', $this->getMockDroplets('create', null))
  33. ));
  34. $this->application->add($CreateInteractiveCommand);
  35. $command = $this->application->find('droplets:create-interactively');
  36. $command->getHelperSet()->set($this->getDialogAsk(null), 'dialog');
  37. $commandTester = new CommandTester($command);
  38. $commandTester->execute(array(
  39. 'command' => $command->getName(),
  40. ));
  41. $this->assertTrue(is_string($commandTester->getDisplay()));
  42. $this->assertRegExp('/Aborted!/', $commandTester->getDisplay());
  43. }
  44. public function testExecuteWithDropletName()
  45. {
  46. $this->markTestIncomplete('Make a test with a droplet name');
  47. }
  48. public function testGetSizes()
  49. {
  50. $sizes = (object) array(
  51. 'sizes' => array(
  52. (object) array('id' => 1, 'name' => 'foo'),
  53. (object) array('id' => 2, 'name' => 'bar'),
  54. )
  55. );
  56. $digitalOcean = $this->getMockDigitalOcean('sizes', $this->getMockSizes($sizes));
  57. $CreateInteractiveCommand = new CreateInteractiveCommand();
  58. $method = new \ReflectionMethod(
  59. $CreateInteractiveCommand, 'getSizes'
  60. );
  61. $method->setAccessible(true);
  62. $results = $method->invoke($CreateInteractiveCommand, $digitalOcean);
  63. $this->assertTrue(is_array($results));
  64. $this->assertCount(2, $results);
  65. $this->assertSame('foo', $results[1]);
  66. $this->assertSame('bar', $results[2]);
  67. }
  68. public function testGetRegions()
  69. {
  70. $regions = (object) array(
  71. 'regions' => array(
  72. (object) array('id' => 1, 'name' => 'foo 1'),
  73. (object) array('id' => 2, 'name' => 'bar 2'),
  74. )
  75. );
  76. $digitalOcean = $this->getMockDigitalOcean('regions', $this->getMockRegions($regions));
  77. $CreateInteractiveCommand = new CreateInteractiveCommand();
  78. $method = new \ReflectionMethod(
  79. $CreateInteractiveCommand, 'getRegions'
  80. );
  81. $method->setAccessible(true);
  82. $results = $method->invoke($CreateInteractiveCommand, $digitalOcean);
  83. $this->assertTrue(is_array($results));
  84. $this->assertCount(2, $results);
  85. $this->assertSame('foo 1', $results[1]);
  86. $this->assertSame('bar 2', $results[2]);
  87. }
  88. public function testGetGlobalImages()
  89. {
  90. $images = (object) array(
  91. 'images' => array(
  92. (object) array('id' => 1, 'name' => 'foo', 'distribution' => 'foobar dist'),
  93. (object) array('id' => 2, 'name' => 'bar', 'distribution' => 'barqmx dist'),
  94. )
  95. );
  96. $digitalOcean = $this->getMockDigitalOcean('images', $this->getMockImages('getGlobal', $images));
  97. $CreateInteractiveCommand = new CreateInteractiveCommand();
  98. $method = new \ReflectionMethod(
  99. $CreateInteractiveCommand, 'getImages'
  100. );
  101. $method->setAccessible(true);
  102. $results = $method->invoke($CreateInteractiveCommand, $digitalOcean, 0);
  103. $this->assertTrue(is_array($results));
  104. $this->assertCount(2, $results);
  105. $this->assertSame('foo, foobar dist', $results[1]);
  106. $this->assertSame('bar, barqmx dist', $results[2]);
  107. }
  108. public function testGetMyImages()
  109. {
  110. $images = (object) array(
  111. 'images' => array(
  112. (object) array('id' => 1, 'name' => 'foo', 'distribution' => 'foobar dist'),
  113. (object) array('id' => 2, 'name' => 'bar', 'distribution' => 'barqmx dist'),
  114. )
  115. );
  116. $digitalOcean = $this->getMockDigitalOcean('images', $this->getMockImages('getMyImages', $images));
  117. $CreateInteractiveCommand = new CreateInteractiveCommand();
  118. $method = new \ReflectionMethod(
  119. $CreateInteractiveCommand, 'getImages'
  120. );
  121. $method->setAccessible(true);
  122. $results = $method->invoke($CreateInteractiveCommand, $digitalOcean, 1);
  123. $this->assertTrue(is_array($results));
  124. $this->assertCount(2, $results);
  125. $this->assertSame('foo, foobar dist', $results[1]);
  126. $this->assertSame('bar, barqmx dist', $results[2]);
  127. }
  128. public function testGetAllImages()
  129. {
  130. $images = (object) array(
  131. 'images' => array(
  132. (object) array('id' => 1, 'name' => 'foo', 'distribution' => 'foobar dist'),
  133. (object) array('id' => 2, 'name' => 'bar', 'distribution' => 'barqmx dist'),
  134. )
  135. );
  136. $digitalOcean = $this->getMockDigitalOcean('images', $this->getMockImages('getAll', $images));
  137. $CreateInteractiveCommand = new CreateInteractiveCommand();
  138. $method = new \ReflectionMethod(
  139. $CreateInteractiveCommand, 'getImages'
  140. );
  141. $method->setAccessible(true);
  142. $results = $method->invoke($CreateInteractiveCommand, $digitalOcean, 2);
  143. $this->assertTrue(is_array($results));
  144. $this->assertCount(2, $results);
  145. $this->assertSame('foo, foobar dist', $results[1]);
  146. $this->assertSame('bar, barqmx dist', $results[2]);
  147. }
  148. public function testGetSshKays()
  149. {
  150. $sshKeys = (object) array(
  151. 'ssh_keys' => array(
  152. (object) array('id' => 123, 'name' => 'office-imac'),
  153. (object) array('id' => 456, 'name' => 'macbook-pro'),
  154. )
  155. );
  156. $digitalOcean = $this->getMockDigitalOcean('sshkeys', $this->getMockSSHKeys('getAll', $sshKeys));
  157. $CreateInteractiveCommand = new CreateInteractiveCommand();
  158. $method = new \ReflectionMethod(
  159. $CreateInteractiveCommand, 'getSshKeys'
  160. );
  161. $method->setAccessible(true);
  162. $results = $method->invoke($CreateInteractiveCommand, $digitalOcean);
  163. $this->assertTrue(is_array($results));
  164. $this->assertCount(3, $results);
  165. $this->assertSame('None (default)', $results[0]);
  166. $this->assertSame('office-imac', $results[123]);
  167. $this->assertSame('macbook-pro', $results[456]);
  168. }
  169. }