ModuleConfigTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Config;
  6. use Magento\TestFramework\ObjectManager;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. /**
  9. * Temando Config Model Test
  10. *
  11. * @package Temando\Shipping\Test\Integration
  12. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  13. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  14. * @link http://www.temando.com/
  15. */
  16. class ModuleConfigTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var ObjectManager
  20. */
  21. private $objectManager;
  22. /**
  23. * @var ModuleConfig
  24. */
  25. private $config;
  26. /**
  27. * @var \Magento\Framework\App\Config\Storage\Writer | \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $configWriterMock;
  30. /**
  31. * Init object manager
  32. */
  33. protected function setUp()
  34. {
  35. parent::setUp();
  36. $this->objectManager = Bootstrap::getObjectManager();
  37. $this->configWriterMock = $this->getMockBuilder(\Magento\Framework\App\Config\Storage\Writer::class)
  38. ->setMethods(['save', 'delete'])
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $configAccessor = Bootstrap::getObjectManager()->create(ConfigAccessor::class, [
  42. 'configWriter' => $this->configWriterMock,
  43. ]);
  44. $this->config = $this->objectManager->create(ModuleConfig::class, [
  45. 'configAccessor' => $configAccessor,
  46. ]);
  47. }
  48. /**
  49. * @test
  50. * @magentoConfigFixture default/carriers/temando/logging_enabled 1
  51. */
  52. public function logIsEnabled()
  53. {
  54. $this->assertTrue($this->config->isLoggingEnabled());
  55. }
  56. /**
  57. * @test
  58. * @magentoConfigFixture default/carriers/temando/logging_enabled 0
  59. */
  60. public function logIsDisabled()
  61. {
  62. $this->assertFalse($this->config->isLoggingEnabled());
  63. }
  64. /**
  65. * @test
  66. * @magentoConfigFixture default/general/store_information/name Info Name
  67. * @magentoConfigFixture default/general/store_information/phone Info Phone
  68. * @magentoConfigFixture default/general/store_information/country_id Info Contry
  69. * @magentoConfigFixture default/general/store_information/postcode Info Postcode
  70. * @magentoConfigFixture default/general/store_information/city Info City
  71. * @magentoConfigFixture default/general/store_information/street_line1 Info Street
  72. */
  73. public function getStoreInformation()
  74. {
  75. $info = $this->config->getStoreInformation();
  76. $this->assertInstanceOf(\Magento\Framework\DataObject::class, $info);
  77. $this->assertEquals('Info Name', $info->getData('name'));
  78. $this->assertEquals('Info Phone', $info->getData('phone'));
  79. $this->assertEquals('Info Contry', $info->getData('country_id'));
  80. $this->assertEquals('Info Postcode', $info->getData('postcode'));
  81. $this->assertEquals('Info City', $info->getData('city'));
  82. $this->assertEquals('Info Street', $info->getData('street_line1'));
  83. }
  84. /**
  85. * @test
  86. * @magentoConfigFixture default/shipping/origin/postcode Origin Postcode
  87. * @magentoConfigFixture default/shipping/origin/city Origin City
  88. * @magentoConfigFixture default/shipping/origin/street_line1 Origin Street
  89. */
  90. public function getShippingOrigin()
  91. {
  92. $origin = $this->config->getShippingOrigin();
  93. $this->assertInstanceOf(\Magento\Framework\DataObject::class, $origin);
  94. $this->assertEquals('Origin Postcode', $origin->getData('postcode'));
  95. $this->assertEquals('Origin City', $origin->getData('city'));
  96. $this->assertEquals('Origin Street', $origin->getData('street_line1'));
  97. }
  98. /**
  99. * @test
  100. * @magentoConfigFixture default/general/locale/weight_unit Foo Unit
  101. */
  102. public function getWeightUnit()
  103. {
  104. $this->assertEquals('Foo Unit', $this->config->getWeightUnit());
  105. }
  106. /**
  107. * @test
  108. * @magentoConfigFixture default/carriers/temando/sovereign_endpoint endpoint.com
  109. */
  110. public function getApiEndpoint()
  111. {
  112. $this->assertEquals('endpoint.com', $this->config->getApiEndpoint());
  113. }
  114. /**
  115. * @test
  116. * @magentoConfigFixture default/carriers/temando/account_id 23
  117. */
  118. public function getAccountId()
  119. {
  120. $this->assertEquals('23', $this->config->getAccountId());
  121. }
  122. /**
  123. * @test
  124. */
  125. public function saveAccountId()
  126. {
  127. $this->configWriterMock
  128. ->expects($this->once())
  129. ->method('save')
  130. ->with(ModuleConfig::CONFIG_XML_PATH_ACCOUNT_ID, '12');
  131. $this->config->saveAccountId('12');
  132. }
  133. /**
  134. * @test
  135. * @magentoConfigFixture default/carriers/temando/bearer_token 808
  136. */
  137. public function getBearerToken()
  138. {
  139. $this->assertEquals('808', $this->config->getBearerToken());
  140. }
  141. /**
  142. * @test
  143. */
  144. public function saveBearerToken()
  145. {
  146. $this->configWriterMock
  147. ->expects($this->once())
  148. ->method('save')
  149. ->with(ModuleConfig::CONFIG_XML_PATH_BEARER_TOKEN, 'bearerToken');
  150. $this->config->saveBearerToken('bearerToken');
  151. }
  152. /**
  153. * @test
  154. * @magentoConfigFixture default/carriers/temando/account_id accountId
  155. * @magentoConfigFixture default/carriers/temando/bearer_token bearerToken
  156. */
  157. public function credentialsAreAvailable()
  158. {
  159. $this->assertTrue($this->config->isRegistered());
  160. }
  161. /**
  162. * @test
  163. * @magentoConfigFixture default/carriers/temando/bearer_token bearerToken
  164. */
  165. public function credentialsAccountMissing()
  166. {
  167. $this->assertFalse($this->config->isRegistered());
  168. }
  169. /**
  170. * @test
  171. * @magentoConfigFixture default/carriers/temando/account_id accountId
  172. */
  173. public function credentialsBearerTokenMissing()
  174. {
  175. $this->assertFalse($this->config->isRegistered());
  176. }
  177. /**
  178. * @test
  179. */
  180. public function credentialsAreNotAvailable()
  181. {
  182. $this->assertFalse($this->config->isRegistered());
  183. }
  184. /**
  185. * @test
  186. * @magentoConfigFixture default/carriers/temando/bearer_token_expiry 123456
  187. */
  188. public function getBearerTokenExpiry()
  189. {
  190. $this->assertEquals('123456', $this->config->getBearerTokenExpiry());
  191. }
  192. /**
  193. * @test
  194. */
  195. public function saveBearerTokenExpiry()
  196. {
  197. $this->configWriterMock
  198. ->expects($this->once())
  199. ->method('save')
  200. ->with(ModuleConfig::CONFIG_XML_PATH_BEARER_TOKEN_EXPIRY, '123456');
  201. $this->config->saveBearerTokenExpiry('123456');
  202. }
  203. /**
  204. * @test
  205. */
  206. public function setAccount()
  207. {
  208. $this->configWriterMock
  209. ->expects($this->exactly(3))
  210. ->method('save')
  211. ->withConsecutive(
  212. ['carriers/temando/account_id', '12'],
  213. ['carriers/temando/bearer_token', 'bearerToken'],
  214. ['carriers/temando/bearer_token_expiry', '123456']
  215. );
  216. $this->config->setAccount('12', 'bearerToken', '123456');
  217. }
  218. /**
  219. * @test
  220. */
  221. public function unsetAccount()
  222. {
  223. $this->configWriterMock
  224. ->expects($this->exactly(3))
  225. ->method('delete')
  226. ->withConsecutive(
  227. ['carriers/temando/account_id'],
  228. ['carriers/temando/bearer_token'],
  229. ['carriers/temando/bearer_token_expiry']
  230. );
  231. $this->config->unsetAccount();
  232. }
  233. }