PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/phpunit/ProductAPISecuredUnitTest.php

https://github.com/octolab/Ecwid
PHP | 152 lines | 95 code | 5 blank | 52 comment | 4 complexity | 97da9b99e9a54a0cb00e62989d912531 MD5 | raw file
  1. <?php
  2. /**
  3. * @author Samigullin Kamil <feedback@kamilsk.com>
  4. * @link http://www.kamilsk.com/
  5. */
  6. namespace Ecwid\tests\phpunit;
  7. use Ecwid\api\ProductAPI,
  8. \Exception;
  9. require 'UnitTest.php';
  10. /**
  11. * @package Ecwid.tests.phpunit
  12. * @since 1.0
  13. */
  14. class ProductAPISecuredUnitTest extends UnitTest
  15. {
  16. /**
  17. * @return array
  18. * @throws Exception
  19. */
  20. public function secured_product_api_provider()
  21. {
  22. $config = array(
  23. 'store_id' => null,
  24. 'protocol' => 'https',
  25. 'secure_auth_key' => null,
  26. );
  27. if (is_null($config['store_id']) or is_null($config['secure_auth_key'])) {
  28. throw new Exception('Set your store id and Product API secret key.');
  29. }
  30. return array($config);
  31. }
  32. /**
  33. * Test products updating.
  34. *
  35. * @dataProvider secured_product_api_provider
  36. * @param int $store_id
  37. * @param string $protocol
  38. * @param string $secure_auth_key
  39. */
  40. public function testPutProducts($store_id, $protocol, $secure_auth_key)
  41. {
  42. $product_api = new ProductAPI(array(
  43. 'store_id' => $store_id,
  44. 'protocol' => $protocol,
  45. 'secure_auth_key' => $secure_auth_key,
  46. ));
  47. $products = $product_api->getProducts();
  48. $response = $product_api->putProducts($products);
  49. $this->assertNotEmpty($response);
  50. $this->assertTrue(is_string($response));
  51. }
  52. /**
  53. * Test product updating.
  54. *
  55. * @dataProvider secured_product_api_provider
  56. * @param int $store_id
  57. * @param string $protocol
  58. * @param string $secure_auth_key
  59. */
  60. public function testPutProduct($store_id, $protocol, $secure_auth_key)
  61. {
  62. $product_api = new ProductAPI(array(
  63. 'store_id' => $store_id,
  64. 'protocol' => $protocol,
  65. 'secure_auth_key' => $secure_auth_key,
  66. ));
  67. $products = $product_api->getProducts();
  68. $products = json_decode($products, true);
  69. $product = array_pop($products);
  70. $response = $product_api->putProduct($product['id'], json_encode($product));
  71. $this->assertNotEmpty($response);
  72. $this->assertTrue(is_string($response));
  73. }
  74. /**
  75. * Test class creating.
  76. *
  77. * @dataProvider secured_product_api_provider
  78. * @param int $store_id
  79. * @param string $protocol
  80. * @param string $secure_auth_key
  81. */
  82. public function testPostClass($store_id, $protocol, $secure_auth_key)
  83. {
  84. $product_api = new ProductAPI(array(
  85. 'store_id' => $store_id,
  86. 'protocol' => $protocol,
  87. 'secure_auth_key' => $secure_auth_key,
  88. ));
  89. $new_class = array('name' => '_test_class');
  90. $new_class = json_encode($new_class);
  91. $response = $product_api->postClass($new_class);
  92. $this->assertNotEmpty($response);
  93. $this->assertTrue(is_string($response));
  94. }
  95. /**
  96. * Test class updating.
  97. *
  98. * @dataProvider secured_product_api_provider
  99. * @param int $store_id
  100. * @param string $protocol
  101. * @param string $secure_auth_key
  102. */
  103. public function testPutClass($store_id, $protocol, $secure_auth_key)
  104. {
  105. $product_api = new ProductAPI(array(
  106. 'store_id' => $store_id,
  107. 'protocol' => $protocol,
  108. 'secure_auth_key' => $secure_auth_key,
  109. ));
  110. $classes = $product_api->getClasses();
  111. $classes = json_decode($classes, true);
  112. $class = array_pop($classes);
  113. $response = $product_api->putClass($class['id'], json_encode($class));
  114. $this->assertNotEmpty($response);
  115. $this->assertTrue(is_string($response));
  116. }
  117. /**
  118. * Test class deleting.
  119. *
  120. * @dataProvider secured_product_api_provider
  121. * @param int $store_id
  122. * @param string $protocol
  123. * @param string $secure_auth_key
  124. */
  125. public function testDeleteClass($store_id, $protocol, $secure_auth_key)
  126. {
  127. $product_api = new ProductAPI(array(
  128. 'store_id' => $store_id,
  129. 'protocol' => $protocol,
  130. 'secure_auth_key' => $secure_auth_key,
  131. ));
  132. $classes = $product_api->getClasses();
  133. $classes = json_decode($classes, true);
  134. foreach ($classes as $class) {
  135. if (array_key_exists('name', $class) && $class['name'] === '_test_class') {
  136. $found = $class;
  137. }
  138. }
  139. if (empty($found)) {
  140. $this->fail('_test_class_not_found');
  141. return;
  142. }
  143. $response = $product_api->deleteClass($found['id']);
  144. $this->assertNotEmpty($response);
  145. $this->assertTrue(is_string($response));
  146. }
  147. }