ImagesTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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\Images;
  11. use DigitalOcean\Tests\TestCase;
  12. use DigitalOcean\Images\Images;
  13. use DigitalOcean\Images\ImagesActions;
  14. /**
  15. * @author Antoine Corcy <contact@sbin.dk>
  16. */
  17. class ImagesTest extends TestCase
  18. {
  19. protected $imageId;
  20. protected $image;
  21. protected $imageBuildQueryMethod;
  22. protected function setUp()
  23. {
  24. $this->imageId = 123;
  25. $this->images = new Images($this->getMockCredentials(), $this->getMockAdapter($this->never()));
  26. $this->imageBuildQueryMethod = new \ReflectionMethod(
  27. $this->images, 'buildQuery'
  28. );
  29. $this->imageBuildQueryMethod->setAccessible(true);
  30. }
  31. /**
  32. * @expectedException \RuntimeException
  33. * @expectedExceptionMEssage Impossible to process this query: https://api.digitalocean.com/droplets/?client_id=foo&api_key=bar
  34. */
  35. public function testProcessQuery()
  36. {
  37. $images = new Images($this->getMockCredentials(), $this->getMockAdapterReturns(null));
  38. $images = $images->getAll();
  39. }
  40. public function testGetAllUrl()
  41. {
  42. $this->assertEquals(
  43. 'https://api.digitalocean.com/images/?client_id=foo&api_key=bar',
  44. $this->imageBuildQueryMethod->invoke($this->images)
  45. );
  46. }
  47. public function testGetAll()
  48. {
  49. $response = <<<JSON
  50. {"status":"OK","images":[{"id":429,"name":"Real Backup 10242011","distribution":"Ubuntu"},{"id":430,"name":"test233","distribution":"Ubuntu"},{"id":431,"name":"test888","distribution":"Ubuntu"},{"id":442,"name":"tesah22","distribution":"Ubuntu"},{"id":443,"name":"testah33","distribution":"Ubuntu"},{"id":444,"name":"testah44","distribution":"Ubuntu"},{"id":447,"name":"ahtest55","distribution":"Ubuntu"},{"id":448,"name":"ahtest66","distribution":"Ubuntu"},{"id":449,"name":"ahtest77","distribution":"Ubuntu"},{"id":458,"name":"Rails3-1Ruby1-9-2","distribution":"Ubuntu"},{"id":466,"name":"NYTD Backup 1-18-2012","distribution":"Ubuntu"},{"id":478,"name":"NLP Final","distribution":"Ubuntu"},{"id":540,"name":"API - Final","distribution":"Ubuntu"},{"id":577,"name":"test1-1","distribution":"Ubuntu"},{"id":578,"name":"alec snapshot1","distribution":"Ubuntu"}]}
  51. JSON
  52. ;
  53. $images = new Images($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  54. $images = $images->getAll();
  55. $this->assertTrue(is_object($images));
  56. $this->assertEquals('OK', $images->status);
  57. $this->assertCount(15, $images->images);
  58. $image1 = $images->images[0];
  59. $this->assertSame(429, $image1->id);
  60. $this->assertSame('Real Backup 10242011', $image1->name);
  61. $this->assertSame('Ubuntu', $image1->distribution);
  62. $image2 = $images->images[1];
  63. $this->assertSame(430, $image2->id);
  64. $this->assertSame('test233', $image2->name);
  65. $this->assertSame('Ubuntu', $image2->distribution);
  66. $image15 = $images->images[14];
  67. $this->assertSame(578, $image15->id);
  68. $this->assertSame('alec snapshot1', $image15->name);
  69. $this->assertSame('Ubuntu', $image15->distribution);
  70. }
  71. public function testGetMyImagesUrl()
  72. {
  73. $this->assertEquals(
  74. 'https://api.digitalocean.com/images/?filter=my_images&client_id=foo&api_key=bar',
  75. $this->imageBuildQueryMethod->invoke(
  76. $this->images, null, null, array('filter' => ImagesActions::ACTION_FILTER_MY_IMAGES)
  77. )
  78. );
  79. }
  80. public function testGetMyImages()
  81. {
  82. $response = <<<JSON
  83. {"status":"OK","images":[{"id":429,"name":"Real Backup 10242011","distribution":"Ubuntu"},{"id":430,"name":"test233","distribution":"Ubuntu"},{"id":431,"name":"test888","distribution":"Ubuntu"},{"id":442,"name":"tesah22","distribution":"Ubuntu"},{"id":443,"name":"testah33","distribution":"Ubuntu"},{"id":444,"name":"testah44","distribution":"Ubuntu"},{"id":447,"name":"ahtest55","distribution":"Ubuntu"},{"id":448,"name":"ahtest66","distribution":"Ubuntu"},{"id":449,"name":"ahtest77","distribution":"Ubuntu"},{"id":458,"name":"Rails3-1Ruby1-9-2","distribution":"Ubuntu"},{"id":466,"name":"NYTD Backup 1-18-2012","distribution":"Ubuntu"},{"id":478,"name":"NLP Final","distribution":"Ubuntu"},{"id":540,"name":"API - Final","distribution":"Ubuntu"},{"id":577,"name":"test1-1","distribution":"Ubuntu"},{"id":578,"name":"alec snapshot1","distribution":"Ubuntu"}]}
  84. JSON
  85. ;
  86. $images = new Images($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  87. $myImages = $images->getMyImages();
  88. $this->assertTrue(is_object($myImages));
  89. $this->assertEquals('OK', $myImages->status);
  90. $this->assertCount(15, $myImages->images);
  91. }
  92. public function testGetGlobalUrl()
  93. {
  94. $this->assertEquals(
  95. 'https://api.digitalocean.com/images/?filter=global&client_id=foo&api_key=bar',
  96. $this->imageBuildQueryMethod->invoke(
  97. $this->images, null, null, array('filter' => ImagesActions::ACTION_FILTER_GLOBAL)
  98. )
  99. );
  100. }
  101. public function testGetGlobal()
  102. {
  103. $response = <<<JSON
  104. {"status":"OK","images":[{"id":429,"name":"Real Backup 10242011","distribution":"Ubuntu"},{"id":430,"name":"test233","distribution":"Ubuntu"},{"id":431,"name":"test888","distribution":"Ubuntu"},{"id":442,"name":"tesah22","distribution":"Ubuntu"},{"id":443,"name":"testah33","distribution":"Ubuntu"},{"id":444,"name":"testah44","distribution":"Ubuntu"},{"id":447,"name":"ahtest55","distribution":"Ubuntu"},{"id":448,"name":"ahtest66","distribution":"Ubuntu"},{"id":449,"name":"ahtest77","distribution":"Ubuntu"},{"id":458,"name":"Rails3-1Ruby1-9-2","distribution":"Ubuntu"},{"id":466,"name":"NYTD Backup 1-18-2012","distribution":"Ubuntu"},{"id":478,"name":"NLP Final","distribution":"Ubuntu"},{"id":540,"name":"API - Final","distribution":"Ubuntu"},{"id":577,"name":"test1-1","distribution":"Ubuntu"},{"id":578,"name":"alec snapshot1","distribution":"Ubuntu"}]}
  105. JSON
  106. ;
  107. $images = new Images($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  108. $globalImages = $images->getGlobal();
  109. $this->assertTrue(is_object($globalImages));
  110. $this->assertEquals('OK', $globalImages->status);
  111. $this->assertCount(15, $globalImages->images);
  112. }
  113. public function testShowUrl()
  114. {
  115. $this->assertEquals(
  116. 'https://api.digitalocean.com/images/123/?client_id=foo&api_key=bar',
  117. $this->imageBuildQueryMethod->invoke($this->images, $this->imageId)
  118. );
  119. }
  120. public function testShow()
  121. {
  122. $response = <<<JSON
  123. {"status":"OK","image":{"id":429,"name":"Real Backup 10242011","distribution":"Ubuntu"}}
  124. JSON
  125. ;
  126. $images = new Images($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  127. $show = $images->show($this->imageId);
  128. $this->assertTrue(is_object($show));
  129. $this->assertEquals('OK', $show->status);
  130. $this->assertSame(429, $show->image->id);
  131. $this->assertSame('Real Backup 10242011', $show->image->name);
  132. $this->assertSame('Ubuntu', $show->image->distribution);
  133. }
  134. public function testDestroyUrl()
  135. {
  136. $this->assertEquals(
  137. 'https://api.digitalocean.com/images/123/destroy/?client_id=foo&api_key=bar',
  138. $this->imageBuildQueryMethod->invoke($this->images, $this->imageId, ImagesActions::ACTION_DESTROY_IMAGE)
  139. );
  140. }
  141. public function testDestroy()
  142. {
  143. $response = <<<JSON
  144. {"status":"OK"}
  145. JSON
  146. ;
  147. $images = new Images($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  148. $destroy = $images->destroy($this->imageId);
  149. $this->assertTrue(is_object($destroy));
  150. $this->assertEquals('OK', $destroy->status);
  151. }
  152. public function testTransfertUrl()
  153. {
  154. $newRegion = array(
  155. 'region_id' => 123,
  156. );
  157. $this->assertEquals(
  158. 'https://api.digitalocean.com/images/123/transfert/?region_id=123&client_id=foo&api_key=bar',
  159. $this->imageBuildQueryMethod->invoke(
  160. $this->images, $this->imageId, ImagesActions::ACTION_TRANSFERT, $newRegion
  161. )
  162. );
  163. }
  164. /**
  165. * @expectedException \InvalidArgumentException
  166. * @expectedExceptionMessage You need to provide an integer "region_id".
  167. */
  168. public function testTransfertThrowsRegionIdInvalidArgumentException()
  169. {
  170. $this->images->transfert($this->imageId, array());
  171. }
  172. public function testTransfert()
  173. {
  174. $response = <<<JSON
  175. {"status":"OK","event_id":7501}
  176. JSON
  177. ;
  178. $images = new Images($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  179. $image = $images->transfert($this->imageId, array('region_id' => 123));
  180. $this->assertTrue(is_object($image));
  181. $this->assertEquals('OK', $image->status);
  182. $this->assertSame(7501, $image->event_id);
  183. }
  184. }