PageRenderTime 30ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/codes-php/phpjakarta/tests/unit/WindowsAzure/Common/CloudConfigurationManagerTest.php

http://bukuphpjs.codeplex.com
PHP | 226 lines | 114 code | 30 blank | 82 comment | 8 complexity | aee0f6009a66029c2b0f55de69bf6dac MD5 | raw file
Possible License(s): Apache-2.0, MIT, LGPL-2.1
  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
  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;
  24. use WindowsAzure\Common\CloudConfigurationManager;
  25. use WindowsAzure\Common\Internal\ConnectionStringSource;
  26. /**
  27. * Unit tests for class CloudConfigurationManager
  28. *
  29. * @category Microsoft
  30. * @package Tests\Unit\WindowsAzure\Common
  31. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  32. * @copyright 2012 Microsoft Corporation
  33. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  34. * @version Release: @package_version@
  35. * @link https://github.com/windowsazure/azure-sdk-for-php
  36. */
  37. class CloudConfigurationManagerTest extends \PHPUnit_Framework_TestCase
  38. {
  39. private $_key = 'my_connection_string';
  40. private $_value = 'connection string value';
  41. public function setUp()
  42. {
  43. $isInitialized = new \ReflectionProperty('WindowsAzure\Common\CloudConfigurationManager', '_isInitialized');
  44. $isInitialized->setAccessible(true);
  45. $isInitialized->setValue(false);
  46. $sources = new \ReflectionProperty('WindowsAzure\Common\CloudConfigurationManager', '_sources');
  47. $sources->setAccessible(true);
  48. $sources->setValue(array());
  49. }
  50. /**
  51. * @covers WindowsAzure\Common\CloudConfigurationManager::getConnectionString
  52. * @covers WindowsAzure\Common\CloudConfigurationManager::_init
  53. */
  54. public function testGetConnectionStringFromEnvironmentVariable()
  55. {
  56. // Setup
  57. putenv("$this->_key=$this->_value");
  58. // Test
  59. $actual = CloudConfigurationManager::getConnectionString($this->_key);
  60. // Assert
  61. $this->assertEquals($this->_value, $actual);
  62. // Clean
  63. putenv($this->_key);
  64. }
  65. /**
  66. * @covers WindowsAzure\Common\CloudConfigurationManager::getConnectionString
  67. */
  68. public function testGetConnectionStringDoesNotExist()
  69. {
  70. // Test
  71. $actual = CloudConfigurationManager::getConnectionString('does not exist');
  72. // Assert
  73. $this->assertEmpty($actual);
  74. }
  75. /**
  76. * @covers WindowsAzure\Common\CloudConfigurationManager::registerSource
  77. * @covers WindowsAzure\Common\CloudConfigurationManager::_init
  78. */
  79. public function testRegisterSource()
  80. {
  81. // Setup
  82. $expectedKey = $this->_key;
  83. $expectedValue = $this->_value . "extravalue";
  84. // Test
  85. CloudConfigurationManager::registerSource(
  86. 'my_source',
  87. function ($key) use ($expectedKey, $expectedValue)
  88. {
  89. if ($key == $expectedKey) {
  90. return $expectedValue;
  91. }
  92. }
  93. );
  94. // Assert
  95. $actual = CloudConfigurationManager::getConnectionString($expectedKey);
  96. $this->assertEquals($expectedValue, $actual);
  97. }
  98. /**
  99. * @covers WindowsAzure\Common\CloudConfigurationManager::registerSource
  100. * @covers WindowsAzure\Common\CloudConfigurationManager::_init
  101. */
  102. public function testRegisterSourceWithPrepend()
  103. {
  104. // Setup
  105. $expectedKey = $this->_key;
  106. $expectedValue = $this->_value . "extravalue2";
  107. putenv("$this->_key=wrongvalue");
  108. // Test
  109. CloudConfigurationManager::registerSource(
  110. 'my_source',
  111. function ($key) use ($expectedKey, $expectedValue)
  112. {
  113. if ($key == $expectedKey) {
  114. return $expectedValue;
  115. }
  116. },
  117. true
  118. );
  119. // Assert
  120. $actual = CloudConfigurationManager::getConnectionString($expectedKey);
  121. $this->assertEquals($expectedValue, $actual);
  122. // Clean
  123. putenv($this->_key);
  124. }
  125. /**
  126. * @covers WindowsAzure\Common\CloudConfigurationManager::unregisterSource
  127. * @covers WindowsAzure\Common\CloudConfigurationManager::_init
  128. */
  129. public function testUnRegisterSource()
  130. {
  131. // Setup
  132. $expectedKey = $this->_key;
  133. $expectedValue = $this->_value . "extravalue3";
  134. $name = 'my_source';
  135. CloudConfigurationManager::registerSource(
  136. $name,
  137. function ($key) use ($expectedKey, $expectedValue)
  138. {
  139. if ($key == $expectedKey) {
  140. return $expectedValue;
  141. }
  142. }
  143. );
  144. // Test
  145. $callback = CloudConfigurationManager::unregisterSource($name);
  146. // Assert
  147. $actual = CloudConfigurationManager::getConnectionString($expectedKey);
  148. $this->assertEmpty($actual);
  149. $this->assertNotNull($callback);
  150. }
  151. /**
  152. * @covers WindowsAzure\Common\CloudConfigurationManager::registerSource
  153. * @covers WindowsAzure\Common\CloudConfigurationManager::_init
  154. */
  155. public function testRegisterSourceWithDefaultSource()
  156. {
  157. // Setup
  158. $expectedKey = $this->_key;
  159. $expectedValue = $this->_value . "extravalue5";
  160. CloudConfigurationManager::unregisterSource(ConnectionStringSource::ENVIRONMENT_SOURCE);
  161. putenv("$expectedKey=$expectedValue");
  162. // Test
  163. CloudConfigurationManager::registerSource(ConnectionStringSource::ENVIRONMENT_SOURCE);
  164. // Assert
  165. $actual = CloudConfigurationManager::getConnectionString($expectedKey);
  166. $this->assertEquals($expectedValue, $actual);
  167. // Clean
  168. putenv($expectedKey);
  169. }
  170. /**
  171. * @covers WindowsAzure\Common\CloudConfigurationManager::unregisterSource
  172. * @covers WindowsAzure\Common\CloudConfigurationManager::_init
  173. */
  174. public function testUnRegisterSourceWithDefaultSource()
  175. {
  176. // Setup
  177. $expectedKey = $this->_key;
  178. $expectedValue = $this->_value . "extravalue4";
  179. $name = 'my_source';
  180. CloudConfigurationManager::registerSource(
  181. $name,
  182. function ($key) use ($expectedKey, $expectedValue)
  183. {
  184. if ($key == $expectedKey) {
  185. return $expectedValue;
  186. }
  187. }
  188. );
  189. // Test
  190. $callback = CloudConfigurationManager::unregisterSource(ConnectionStringSource::ENVIRONMENT_SOURCE);
  191. // Assert
  192. $actual = CloudConfigurationManager::getConnectionString($expectedKey);
  193. $this->assertEquals($expectedValue, $actual);
  194. $this->assertNotNull($callback);
  195. }
  196. }