-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathTestSimpleResource.php
132 lines (116 loc) · 3.08 KB
/
TestSimpleResource.php
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
<?php
/**
* Created by PhpStorm.
* @author Tareq Mahmood <tareqtms@yahoo.com>
* Created at: 9/9/16 12:38 PM UTC+06:00
*/
namespace PHPShopify;
class TestSimpleResource extends TestResource
{
/**
* @var string Resource name
*/
public $resourceName;
/**
* @var array sample array for testing post
*/
public $postArray;
/**
* @var array sample array for testing put
*/
public $putArray;
/**
* @var array sample post with invalid data
*/
public $errorPostArray;
/**
* TestSimpleResource constructor.
*/
public function __construct()
{
$this->resourceName = preg_replace('/.+\\\\(\w+)Test$/', '$1', get_called_class());
parent::__construct();
}
/**
* Test post resource
*
* @return int
*/
public function testPost()
{
if ($this->postArray) {
$result = static::$shopify->{$this->resourceName}->post($this->postArray);
$this->assertTrue(is_array($result));
$this->assertNotEmpty($result);
return $result['id'];
}
}
/**
* Test get resource
*
* @depends testPost
*/
public function testGet()
{
$resource = static::$shopify->{$this->resourceName};
$result = $resource->get();
$this->assertTrue(is_array($result));
//Data posted, so cannot be empty
if($this->postArray) {
$this->assertNotEmpty($result);
}
if($resource->countEnabled) {
//Count should match the result array count
$count = static::$shopify->{$this->resourceName}->count();
$this->assertEquals($count, count($result));
}
}
/**
* Test getting single resource by id
*
* @depends testPost
*/
public function testGetSelf($id)
{
if ($id) {
$product = static::$shopify->{$this->resourceName}($id)->get();
$this->assertTrue(is_array($product));
$this->assertNotEmpty($product);
$this->assertEquals($id, $product['id']);
}
}
/**
* Test put resource
*
* @depends testPost
*/
public function testPut($id)
{
if ($this->putArray) {
$result = static::$shopify->{$this->resourceName}($id)->put($this->putArray);
$this->assertTrue(is_array($result));
$this->assertNotEmpty($result);
foreach($this->putArray as $key => $value) {
$this->assertEquals($value, $result[$key]);
}
}
}
/**
* Test delete resource
*
* @depends testPost
*/
public function testDelete($id)
{
if ($id) {
$result = static::$shopify->{$this->resourceName}($id)->delete();
$this->assertEmpty($result);
}
}
public function testPostError() {
if ($this->errorPostArray) {
$this->expectException('PHPShopify\\Exception\\ApiException');
static::$shopify->{$this->resourceName}->post($this->errorPostArray);
}
}
}