Ecwid /tests/phpunit/ProductAPISecuredUnitTest.php

Language PHP Lines 152
MD5 Hash 97da9b99e9a54a0cb00e62989d912531
Repository https://github.com/octolab/Ecwid.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
 * @author Samigullin Kamil <feedback@kamilsk.com>
 * @link http://www.kamilsk.com/
 */
namespace Ecwid\tests\phpunit;
use Ecwid\api\ProductAPI,
	\Exception;
require 'UnitTest.php';
/**
 * @package Ecwid.tests.phpunit
 * @since 1.0
 */
class ProductAPISecuredUnitTest extends UnitTest
{
	/**
	 * @return array
	 * @throws Exception
	 */
	public function secured_product_api_provider()
	{
		$config = array(
			'store_id' => null,
			'protocol' => 'https',
			'secure_auth_key' => null,
		);
		if (is_null($config['store_id']) or is_null($config['secure_auth_key'])) {
			throw new Exception('Set your store id and Product API secret key.');
		}
		return array($config);
	}

	/**
	 * Test products updating.
	 *
	 * @dataProvider secured_product_api_provider
	 * @param int $store_id
	 * @param string $protocol
	 * @param string $secure_auth_key
	 */
	public function testPutProducts($store_id, $protocol, $secure_auth_key)
	{
		$product_api = new ProductAPI(array(
			'store_id' => $store_id,
			'protocol' => $protocol,
			'secure_auth_key' => $secure_auth_key,
		));
		$products = $product_api->getProducts();
		$response = $product_api->putProducts($products);
		$this->assertNotEmpty($response);
		$this->assertTrue(is_string($response));
	}

	/**
	 * Test product updating.
	 *
	 * @dataProvider secured_product_api_provider
	 * @param int $store_id
	 * @param string $protocol
	 * @param string $secure_auth_key
	 */
	public function testPutProduct($store_id, $protocol, $secure_auth_key)
	{
		$product_api = new ProductAPI(array(
			'store_id' => $store_id,
			'protocol' => $protocol,
			'secure_auth_key' => $secure_auth_key,
		));
		$products = $product_api->getProducts();
		$products = json_decode($products, true);
		$product = array_pop($products);
		$response = $product_api->putProduct($product['id'], json_encode($product));
		$this->assertNotEmpty($response);
		$this->assertTrue(is_string($response));
	}

	/**
	 * Test class creating.
	 *
	 * @dataProvider secured_product_api_provider
	 * @param int $store_id
	 * @param string $protocol
	 * @param string $secure_auth_key
	 */
	public function testPostClass($store_id, $protocol, $secure_auth_key)
	{
		$product_api = new ProductAPI(array(
			'store_id' => $store_id,
			'protocol' => $protocol,
			'secure_auth_key' => $secure_auth_key,
		));
		$new_class = array('name' => '_test_class');
		$new_class = json_encode($new_class);
		$response = $product_api->postClass($new_class);
		$this->assertNotEmpty($response);
		$this->assertTrue(is_string($response));
	}

	/**
	 * Test class updating.
	 *
	 * @dataProvider secured_product_api_provider
	 * @param int $store_id
	 * @param string $protocol
	 * @param string $secure_auth_key
	 */
	public function testPutClass($store_id, $protocol, $secure_auth_key)
	{
		$product_api = new ProductAPI(array(
			'store_id' => $store_id,
			'protocol' => $protocol,
			'secure_auth_key' => $secure_auth_key,
		));
		$classes = $product_api->getClasses();
		$classes = json_decode($classes, true);
		$class = array_pop($classes);
		$response = $product_api->putClass($class['id'], json_encode($class));
		$this->assertNotEmpty($response);
		$this->assertTrue(is_string($response));
	}

	/**
	 * Test class deleting.
	 *
	 * @dataProvider secured_product_api_provider
	 * @param int $store_id
	 * @param string $protocol
	 * @param string $secure_auth_key
	 */
	public function testDeleteClass($store_id, $protocol, $secure_auth_key)
	{
		$product_api = new ProductAPI(array(
			'store_id' => $store_id,
			'protocol' => $protocol,
			'secure_auth_key' => $secure_auth_key,
		));
		$classes = $product_api->getClasses();
		$classes = json_decode($classes, true);
		foreach ($classes as $class) {
			if (array_key_exists('name', $class) && $class['name'] === '_test_class') {
				$found = $class;
			}
		}
		if (empty($found)) {
			$this->fail('_test_class_not_found');
			return;
		}
		$response = $product_api->deleteClass($found['id']);
		$this->assertNotEmpty($response);
		$this->assertTrue(is_string($response));
	}
}
Back to Top