/tests/Commands/LinodeCommandTest.php

https://bitbucket.org/hampel/linode · PHP · 288 lines · 187 code · 56 blank · 45 comment · 0 complexity · b2ac0f12700b305731508d85452ae07d MD5 · raw file

  1. <?php namespace Hampel\Linode\Commands;
  2. use Mockery;
  3. use GuzzleHttp\Client;
  4. use Hampel\Linode\Linode;
  5. use GuzzleHttp\Subscriber\Mock;
  6. class LinodeCommandTest extends \PHPUnit_Framework_TestCase
  7. {
  8. protected $linode;
  9. public function setUp()
  10. {
  11. date_default_timezone_set('UTC');
  12. $this->linode = Linode::make(API_KEY);
  13. $this->mock = new Mock();
  14. $this->client = new Client();
  15. $this->client->getEmitter()->attach($this->mock);
  16. }
  17. /**
  18. * Where we store sample JSON responses.
  19. * @return string
  20. */
  21. protected function getMockPath()
  22. {
  23. return dirname(__FILE__) . DIRECTORY_SEPARATOR . "mock" . DIRECTORY_SEPARATOR;
  24. }
  25. /**
  26. * @group network
  27. */
  28. public function testList()
  29. {
  30. $response = $this->linode->execute(new LinodeCommand('list', []));
  31. $this->assertEquals(200, $this->linode->getLastStatusCode());
  32. $this->assertTrue(is_array($response)); // you may not have any linodes...
  33. }
  34. /**
  35. * @group network
  36. * @expectedException \Hampel\Linode\Exception\LinodeErrorException
  37. * @expectedExceptionMessage Error processing Linode command
  38. */
  39. public function testFailCreate()
  40. {
  41. // Omit missing parameters to trigger an error
  42. $response = $this->linode->execute(new LinodeCommand('create', []));
  43. }
  44. /**
  45. * Mock...
  46. */
  47. public function testMockCreate()
  48. {
  49. $command = Mockery::mock('Hampel\Linode\Commands\CommandInterface');
  50. $command->shouldReceive('getAction')->andReturn('linode.create');
  51. $command->shouldReceive('build')->andReturn([
  52. 'api_action' => 'linode.create',
  53. 'datacenterid' => 2,
  54. 'planid' => 2,
  55. 'paymentterm' => 1]);
  56. $this->mock->addResponse($this->getMockPath() . 'linode_create.json');
  57. $linode = new Linode($this->client);
  58. $response = $linode->execute($command);
  59. $this->assertInstanceOf('GuzzleHttp\Message\Response', $linode->getLastResponse());
  60. $this->assertEquals('?api_action=linode.create&datacenterid=2&planid=2&paymentterm=1', $linode->getLastQuery());
  61. $this->assertEquals(200, $linode->getLastStatusCode());
  62. $this->assertArrayHasKey('LinodeID', $response);
  63. $this->assertEquals('8098', $response['LinodeID']);
  64. }
  65. /**
  66. *
  67. */
  68. public function testMockBoot()
  69. {
  70. $command = Mockery::mock('Hampel\Linode\Commands\CommandInterface');
  71. $command->shouldReceive('getAction')->andReturn('linode.boot');
  72. $command->shouldReceive('build')->andReturn([
  73. 'api_action' => 'linode.boot',
  74. 'linodeid' => 123,
  75. 'configid' => 123]);
  76. $this->mock->addResponse($this->getMockPath() . 'linode_boot.json');
  77. $linode = new Linode($this->client);
  78. $response = $linode->execute($command);
  79. $this->assertInstanceOf('GuzzleHttp\Message\Response', $linode->getLastResponse());
  80. $this->assertEquals('?api_action=linode.boot&linodeid=123&configid=123', $linode->getLastQuery());
  81. $this->assertEquals(200, $linode->getLastStatusCode());
  82. $this->assertArrayHasKey('JobID', $response);
  83. $this->assertEquals('1293', $response['JobID']);
  84. }
  85. /**
  86. *
  87. */
  88. public function testMockClone()
  89. {
  90. $command = Mockery::mock('Hampel\Linode\Commands\CommandInterface');
  91. $command->shouldReceive('getAction')->andReturn('linode.clone');
  92. $command->shouldReceive('build')->andReturn([
  93. 'api_action' => 'linode.clone',
  94. 'linodeid' => 123,
  95. 'datacenterid' => 123,
  96. 'planid' => 123]);
  97. $this->mock->addResponse($this->getMockPath() . 'linode_clone.json');
  98. $linode = new Linode($this->client);
  99. $response = $linode->execute($command);
  100. $this->assertInstanceOf('GuzzleHttp\Message\Response', $linode->getLastResponse());
  101. $this->assertEquals('?api_action=linode.clone&linodeid=123&datacenterid=123&planid=123', $linode->getLastQuery());
  102. $this->assertEquals(200, $linode->getLastStatusCode());
  103. $this->assertArrayHasKey('LinodeID', $response);
  104. $this->assertEquals('8098', $response['LinodeID']);
  105. }
  106. /**
  107. *
  108. */
  109. public function testMockDelete()
  110. {
  111. $command = Mockery::mock('Hampel\Linode\Commands\CommandInterface');
  112. $command->shouldReceive('getAction')->andReturn('linode.delete');
  113. $command->shouldReceive('build')->andReturn([
  114. 'api_action' => 'linode.delete',
  115. 'linodeid' => 123
  116. ]);
  117. $this->mock->addResponse($this->getMockPath() . 'linode_delete.json');
  118. $linode = new Linode($this->client);
  119. $response = $linode->execute($command);
  120. $this->assertInstanceOf('GuzzleHttp\Message\Response', $linode->getLastResponse());
  121. $this->assertEquals('?api_action=linode.delete&linodeid=123', $linode->getLastQuery());
  122. $this->assertEquals(200, $linode->getLastStatusCode());
  123. $this->assertArrayHasKey('LinodeID', $response);
  124. $this->assertEquals('8204', $response['LinodeID']);
  125. }
  126. /**
  127. *
  128. */
  129. public function testMockList()
  130. {
  131. $command = Mockery::mock('Hampel\Linode\Commands\CommandInterface');
  132. $command->shouldReceive('getAction')->andReturn('linode.list');
  133. $command->shouldReceive('build')->andReturn([
  134. 'api_action' => 'linode.list'
  135. ]);
  136. $this->mock->addResponse($this->getMockPath() . 'linode_list.json');
  137. $linode = new Linode($this->client);
  138. $response = $linode->execute($command);
  139. $this->assertInstanceOf('GuzzleHttp\Message\Response', $linode->getLastResponse());
  140. $this->assertEquals('?api_action=linode.list', $linode->getLastQuery());
  141. $this->assertEquals(200, $linode->getLastStatusCode());
  142. $this->assertTrue(is_array($response));
  143. }
  144. /**
  145. *
  146. */
  147. public function testMockReboot()
  148. {
  149. $command = Mockery::mock('Hampel\Linode\Commands\CommandInterface');
  150. $command->shouldReceive('getAction')->andReturn('linode.reboot');
  151. $command->shouldReceive('build')->andReturn([
  152. 'api_action' => 'linode.reboot',
  153. 'linodeid' => 123
  154. ]);
  155. $this->mock->addResponse($this->getMockPath() . 'linode_reboot.json');
  156. $linode = new Linode($this->client);
  157. $response = $linode->execute($command);
  158. $this->assertInstanceOf('GuzzleHttp\Message\Response', $linode->getLastResponse());
  159. $this->assertEquals('?api_action=linode.reboot&linodeid=123', $linode->getLastQuery());
  160. $this->assertEquals(200, $linode->getLastStatusCode());
  161. $this->assertArrayHasKey('JobID', $response);
  162. $this->assertEquals('1295', $response['JobID']);
  163. }
  164. /**
  165. *
  166. */
  167. public function testMockResize()
  168. {
  169. $command = Mockery::mock('Hampel\Linode\Commands\CommandInterface');
  170. $command->shouldReceive('getAction')->andReturn('linode.resize');
  171. $command->shouldReceive('build')->andReturn([
  172. 'api_action' => 'linode.resize',
  173. 'linodeid' => 123,
  174. 'configid' => 123
  175. ]);
  176. $this->mock->addResponse($this->getMockPath() . 'linode_resize.json');
  177. $linode = new Linode($this->client);
  178. $response = $linode->execute($command);
  179. $this->assertInstanceOf('GuzzleHttp\Message\Response', $linode->getLastResponse());
  180. $this->assertEquals('?api_action=linode.resize&linodeid=123&configid=123', $linode->getLastQuery());
  181. $this->assertEquals(200, $linode->getLastStatusCode());
  182. //$this->assertArrayHasKey('JobID', $response);
  183. //$this->assertEquals('1295', $response['JobID']);
  184. }
  185. /**
  186. *
  187. */
  188. public function testMockShutdown()
  189. {
  190. $command = Mockery::mock('Hampel\Linode\Commands\CommandInterface');
  191. $command->shouldReceive('getAction')->andReturn('linode.shutdown');
  192. $command->shouldReceive('build')->andReturn([
  193. 'api_action' => 'linode.shutdown',
  194. 'linodeid' => 123
  195. ]);
  196. $this->mock->addResponse($this->getMockPath() . 'linode_shutdown.json');
  197. $linode = new Linode($this->client);
  198. $response = $linode->execute($command);
  199. $this->assertInstanceOf('GuzzleHttp\Message\Response', $linode->getLastResponse());
  200. $this->assertEquals('?api_action=linode.shutdown&linodeid=123', $linode->getLastQuery());
  201. $this->assertEquals(200, $linode->getLastStatusCode());
  202. $this->assertArrayHasKey('JobID', $response);
  203. $this->assertEquals('1292', $response['JobID']);
  204. }
  205. /**
  206. *
  207. */
  208. public function testMockUpdate()
  209. {
  210. $command = Mockery::mock('Hampel\Linode\Commands\CommandInterface');
  211. $command->shouldReceive('getAction')->andReturn('linode.update');
  212. $command->shouldReceive('build')->andReturn([
  213. 'api_action' => 'linode.update',
  214. 'linodeid' => 123
  215. ]);
  216. $this->mock->addResponse($this->getMockPath() . 'linode_update.json');
  217. $linode = new Linode($this->client);
  218. $response = $linode->execute($command);
  219. $this->assertInstanceOf('GuzzleHttp\Message\Response', $linode->getLastResponse());
  220. $this->assertEquals('?api_action=linode.update&linodeid=123', $linode->getLastQuery());
  221. $this->assertEquals(200, $linode->getLastStatusCode());
  222. $this->assertArrayHasKey('LinodeID', $response);
  223. $this->assertEquals('8098', $response['LinodeID']);
  224. }
  225. /**
  226. *
  227. */
  228. public function tearDown()
  229. {
  230. Mockery::close();
  231. }
  232. }