/tests/unit/WindowsAzure/Common/Internal/ServicesBuilderTest.php

http://github.com/WindowsAzure/azure-sdk-for-php · PHP · 300 lines · 153 code · 33 blank · 114 comment · 0 complexity · e59f30561b5e12a3f00302caabdcb46c MD5 · raw file

  1. <?php
  2. /**
  3. * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. * PHP version 5
  15. *
  16. * @category Microsoft
  17. * @package Tests\Unit\WindowsAzure\Common\Internal
  18. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  19. * @copyright 2012 Microsoft Corporation
  20. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  21. * @link https://github.com/windowsazure/azure-sdk-for-php
  22. */
  23. namespace Tests\Unit\WindowsAzure\Common\Internal;
  24. use Tests\Framework\TestResources;
  25. use WindowsAzure\Common\Internal\Resources;
  26. use WindowsAzure\Common\Internal\ServicesBuilder;
  27. use WindowsAzure\Common\Configuration;
  28. use WindowsAzure\Queue\QueueSettings;
  29. use WindowsAzure\Blob\BlobSettings;
  30. use WindowsAzure\Table\TableSettings;
  31. use WindowsAzure\ServiceManagement\ServiceManagementSettings;
  32. use WindowsAzure\Common\Internal\InvalidArgumentTypeException;
  33. /**
  34. * Unit tests for class ServicesBuilder
  35. *
  36. * @category Microsoft
  37. * @package Tests\Unit\WindowsAzure\Common\Internal
  38. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  39. * @copyright 2012 Microsoft Corporation
  40. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  41. * @version Release: @package_version@
  42. * @link https://github.com/windowsazure/azure-sdk-for-php
  43. */
  44. class ServicesBuilderTest extends \PHPUnit_Framework_TestCase
  45. {
  46. /**
  47. * @covers WindowsAzure\Common\Internal\ServicesBuilder::build
  48. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_buildQueue
  49. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_addHeadersFilter
  50. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfig
  51. */
  52. public function testBuildForQueue()
  53. {
  54. // Setup
  55. $uri = 'http://' . TestResources::accountName() . '.queue.core.windows.net';
  56. $config = new Configuration();
  57. $config->setProperty(QueueSettings::ACCOUNT_KEY, TestResources::accountKey());
  58. $config->setProperty(QueueSettings::ACCOUNT_NAME, TestResources::accountName());
  59. $config->setProperty(QueueSettings::URI, $uri);
  60. $builder = new ServicesBuilder();
  61. // Test
  62. $queueRestProxy = $builder->build($config, Resources::QUEUE_TYPE_NAME);
  63. // Assert
  64. $this->assertInstanceOf('WindowsAzure\Queue\Internal\IQueue', $queueRestProxy);
  65. }
  66. /**
  67. * @covers WindowsAzure\Common\Internal\ServicesBuilder::build
  68. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_buildBlob
  69. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_addHeadersFilter
  70. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfig
  71. */
  72. public function testBuildForBlob()
  73. {
  74. // Setup
  75. $uri = 'http://' . TestResources::accountName() . '.blob.core.windows.net';
  76. $config = new Configuration();
  77. $config->setProperty(BlobSettings::ACCOUNT_KEY, TestResources::accountKey());
  78. $config->setProperty(BlobSettings::ACCOUNT_NAME, TestResources::accountName());
  79. $config->setProperty(BlobSettings::URI, $uri);
  80. $builder = new ServicesBuilder();
  81. // Test
  82. $blobRestProxy = $builder->build($config, Resources::BLOB_TYPE_NAME);
  83. // Assert
  84. $this->assertInstanceOf('WindowsAzure\Blob\Internal\IBlob', $blobRestProxy);
  85. }
  86. /**
  87. * @covers WindowsAzure\Common\Internal\ServicesBuilder::build
  88. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_buildTable
  89. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_addHeadersFilter
  90. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfig
  91. */
  92. public function testBuildForTable()
  93. {
  94. // Setup
  95. $uri = 'http://' . TestResources::accountName() . '.table.core.windows.net';
  96. $config = new Configuration();
  97. $config->setProperty(TableSettings::ACCOUNT_KEY, TestResources::accountKey());
  98. $config->setProperty(TableSettings::ACCOUNT_NAME, TestResources::accountName());
  99. $config->setProperty(TableSettings::URI, $uri);
  100. $builder = new ServicesBuilder();
  101. // Test
  102. $tableRestProxy = $builder->build($config, Resources::TABLE_TYPE_NAME);
  103. // Assert
  104. $this->assertInstanceOf('WindowsAzure\Table\Internal\ITable', $tableRestProxy);
  105. }
  106. /**
  107. * @covers WindowsAzure\Common\Internal\ServicesBuilder::build
  108. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_buildServiceManagement
  109. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_addHeadersFilter
  110. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfig
  111. */
  112. public function testBuildForServiceManagement()
  113. {
  114. // Setup
  115. $config = new Configuration();
  116. $config->setProperty(ServiceManagementSettings::CERTIFICATE_PATH, 'path');
  117. $config->setProperty(ServiceManagementSettings::SUBSCRIPTION_ID, 'sub id');
  118. $config->setProperty(ServiceManagementSettings::URI, 'uri');
  119. $builder = new ServicesBuilder();
  120. // Test
  121. $serviceManagementRestProxy = $builder->build($config, Resources::SERVICE_MANAGEMENT_TYPE_NAME);
  122. // Assert
  123. $this->assertInstanceOf('WindowsAzure\ServiceManagement\Internal\IServiceManagement', $serviceManagementRestProxy);
  124. }
  125. /**
  126. * @covers WindowsAzure\Common\Internal\ServicesBuilder::build
  127. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfig
  128. */
  129. public function testBuildWithInvalidTypeFail()
  130. {
  131. // Setup
  132. $uri = 'http://' . TestResources::accountName() . '.queue.core.windows.net';
  133. $config = new Configuration();
  134. $config->setProperty(QueueSettings::ACCOUNT_KEY, TestResources::accountKey());
  135. $config->setProperty(QueueSettings::ACCOUNT_NAME, TestResources::accountName());
  136. $config->setProperty(QueueSettings::URI, $uri);
  137. $builder = new ServicesBuilder();
  138. $this->setExpectedException(get_class(new InvalidArgumentTypeException('')));
  139. // Test
  140. $builder->build($config, 'FooType');
  141. }
  142. /**
  143. * @covers WindowsAzure\Common\Internal\ServicesBuilder::build
  144. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfig
  145. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfigSetting
  146. */
  147. public function testValidateConfigWithMissingQueueSettingConfig()
  148. {
  149. $missingKeyMsg = sprintf(Resources::MISSING_CONFIG_SETTING_KEY_MSG, 'QueueSettings::ACCOUNT_KEY', 'Queue');
  150. $config = new Configuration();
  151. $config->setProperty(QueueSettings::ACCOUNT_NAME, TestResources::accountName());
  152. $config->setProperty(QueueSettings::URI, 'url');
  153. $builder = new ServicesBuilder();
  154. $this->setExpectedException('\InvalidArgumentException', $missingKeyMsg);
  155. $builder->build($config, Resources::QUEUE_TYPE_NAME);
  156. }
  157. /**
  158. * @covers WindowsAzure\Common\Internal\ServicesBuilder::build
  159. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfig
  160. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfigSetting
  161. */
  162. public function testValidateConfigWithEmptyQueueSettingConfig()
  163. {
  164. $missingValueMsg = sprintf(Resources::MISSING_CONFIG_SETTING_VALUE_MSG, 'QueueSettings::ACCOUNT_KEY');
  165. $config = new Configuration();
  166. $config->setProperty(QueueSettings::ACCOUNT_NAME, TestResources::accountName());
  167. $config->setProperty(QueueSettings::ACCOUNT_KEY, null);
  168. $config->setProperty(QueueSettings::URI, 'url');
  169. $builder = new ServicesBuilder();
  170. $this->setExpectedException('\InvalidArgumentException', $missingValueMsg);
  171. $builder->build($config, Resources::QUEUE_TYPE_NAME);
  172. }
  173. /**
  174. * @covers WindowsAzure\Common\Internal\ServicesBuilder::build
  175. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfig
  176. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfigSetting
  177. */
  178. public function testValidateConfigWithMissingBlobSettingConfig()
  179. {
  180. $missingKeyMsg = sprintf(Resources::MISSING_CONFIG_SETTING_KEY_MSG, 'BlobSettings::ACCOUNT_KEY', 'Blob');
  181. $config = new Configuration();
  182. $config->setProperty(BlobSettings::ACCOUNT_NAME, TestResources::accountName());
  183. $config->setProperty(BlobSettings::URI, 'url');
  184. $builder = new ServicesBuilder();
  185. $this->setExpectedException('\InvalidArgumentException', $missingKeyMsg);
  186. $builder->build($config, Resources::BLOB_TYPE_NAME);
  187. }
  188. /**
  189. * @covers WindowsAzure\Common\Internal\ServicesBuilder::build
  190. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfig
  191. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfigSetting
  192. */
  193. public function testValidateConfigWithEmptyBlobSettingConfig()
  194. {
  195. $missingValueMsg = sprintf(Resources::MISSING_CONFIG_SETTING_VALUE_MSG, 'BlobSettings::ACCOUNT_KEY');
  196. $config = new Configuration();
  197. $config->setProperty(BlobSettings::ACCOUNT_NAME, TestResources::accountName());
  198. $config->setProperty(BlobSettings::ACCOUNT_KEY, null);
  199. $config->setProperty(BlobSettings::URI, 'url');
  200. $builder = new ServicesBuilder();
  201. $this->setExpectedException('\InvalidArgumentException', $missingValueMsg);
  202. $builder->build($config, Resources::BLOB_TYPE_NAME);
  203. }
  204. /**
  205. * @covers WindowsAzure\Common\Internal\ServicesBuilder::build
  206. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfig
  207. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfigSetting
  208. */
  209. public function testValidateConfigWithMissingTableSettingConfig()
  210. {
  211. $missingKeyMsg = sprintf(Resources::MISSING_CONFIG_SETTING_KEY_MSG, 'TableSettings::ACCOUNT_KEY', 'Table');
  212. $config = new Configuration();
  213. $config->setProperty(TableSettings::ACCOUNT_NAME, TestResources::accountName());
  214. $config->setProperty(TableSettings::URI, 'url');
  215. $builder = new ServicesBuilder();
  216. $this->setExpectedException('\InvalidArgumentException', $missingKeyMsg);
  217. $builder->build($config, Resources::TABLE_TYPE_NAME);
  218. }
  219. /**
  220. * @covers WindowsAzure\Common\Internal\ServicesBuilder::build
  221. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfig
  222. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfigSetting
  223. */
  224. public function testValidateConfigWithEmptyTableSettingConfig()
  225. {
  226. $missingValueMsg = sprintf(Resources::MISSING_CONFIG_SETTING_VALUE_MSG, 'TableSettings::ACCOUNT_KEY');
  227. $config = new Configuration();
  228. $config->setProperty(TableSettings::ACCOUNT_NAME, TestResources::accountName());
  229. $config->setProperty(TableSettings::ACCOUNT_KEY, null);
  230. $config->setProperty(TableSettings::URI, 'url');
  231. $builder = new ServicesBuilder();
  232. $this->setExpectedException('\InvalidArgumentException', $missingValueMsg);
  233. $builder->build($config, Resources::TABLE_TYPE_NAME);
  234. }
  235. /**
  236. * @covers WindowsAzure\Common\Internal\ServicesBuilder::build
  237. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfig
  238. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfigSetting
  239. */
  240. public function testValidateConfigWithMissingServiceManagementSettingConfig()
  241. {
  242. $missingKeyMsg = sprintf(Resources::MISSING_CONFIG_SETTING_KEY_MSG, 'ServiceManagementSettings::SUBSCRIPTION_ID', 'ServiceManagement');
  243. $config = new Configuration();
  244. $config->setProperty(ServiceManagementSettings::CERTIFICATE_PATH, 'path');
  245. $config->setProperty(ServiceManagementSettings::URI, 'url');
  246. $builder = new ServicesBuilder();
  247. $this->setExpectedException('\InvalidArgumentException', $missingKeyMsg);
  248. $builder->build($config, Resources::SERVICE_MANAGEMENT_TYPE_NAME);
  249. }
  250. /**
  251. * @covers WindowsAzure\Common\Internal\ServicesBuilder::build
  252. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfig
  253. * @covers WindowsAzure\Common\Internal\ServicesBuilder::_validateConfigSetting
  254. */
  255. public function testValidateConfigWithEmptyServiceManagementSettingConfig()
  256. {
  257. $missingValueMsg = sprintf(Resources::MISSING_CONFIG_SETTING_VALUE_MSG, 'ServiceManagementSettings::SUBSCRIPTION_ID');
  258. $config = new Configuration();
  259. $config->setProperty(ServiceManagementSettings::CERTIFICATE_PATH, 'path');
  260. $config->setProperty(ServiceManagementSettings::SUBSCRIPTION_ID, '');
  261. $config->setProperty(ServiceManagementSettings::URI, 'url');
  262. $builder = new ServicesBuilder();
  263. $this->setExpectedException('\InvalidArgumentException', $missingValueMsg);
  264. $builder->build($config, Resources::SERVICE_MANAGEMENT_TYPE_NAME);
  265. }
  266. }
  267. ?>