1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SugarAPI\SDK\Tests\EntryPoint; |
4
|
|
|
|
5
|
|
|
use SugarAPI\SDK\Request\DELETE; |
6
|
|
|
use SugarAPI\SDK\Request\GET; |
7
|
|
|
use SugarAPI\SDK\Request\GETFile; |
8
|
|
|
use SugarAPI\SDK\Request\POST; |
9
|
|
|
use SugarAPI\SDK\Request\POSTFile; |
10
|
|
|
use SugarAPI\SDK\Request\PUT; |
11
|
|
|
use SugarAPI\SDK\Tests\Stubs\EntryPointStub; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class AbstractEntryPointTest |
15
|
|
|
* @package SugarAPI\SDK\Tests\EntryPoint |
16
|
|
|
* @coversDefaultClass SugarAPI\SDK\EntryPoint\Abstracts\AbstractEntryPoint |
17
|
|
|
* @group entrypoints |
18
|
|
|
*/ |
19
|
|
|
class AbstractEntryPointTest extends \PHPUnit_Framework_TestCase { |
20
|
|
|
|
21
|
|
|
public static function setUpBeforeClass() |
22
|
|
|
{ |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public static function tearDownAfterClass() |
26
|
|
|
{ |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected $Stub; |
30
|
|
|
protected $url = 'http://localhost/rest/v10/'; |
31
|
|
|
protected $options = array('foo'); |
32
|
|
|
protected $data = array( |
33
|
|
|
'foo' => 'bar' |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
public function setUp() |
37
|
|
|
{ |
38
|
|
|
parent::setUp(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function tearDown() |
42
|
|
|
{ |
43
|
|
|
parent::tearDown(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return EntryPointStub $Stub |
48
|
|
|
* @covers ::__construct |
49
|
|
|
* @group abstractAP |
50
|
|
|
*/ |
51
|
|
|
public function testConstructor(){ |
52
|
|
|
$Stub = new EntryPointStub($this->url); |
53
|
|
|
$this->assertEquals('',$Stub->getUrl()); |
54
|
|
|
$this->assertEquals(array(),$Stub->getOptions()); |
55
|
|
|
$this->assertEmpty($Stub->getData()); |
56
|
|
|
$this->assertEmpty($Stub->getRequest()); |
57
|
|
|
$this->assertEmpty($Stub->getResponse()); |
58
|
|
|
|
59
|
|
|
unset($Stub); |
60
|
|
|
$Stub = new EntryPointStub($this->url,$this->options); |
61
|
|
|
$this->assertEquals($this->url.'foo',$Stub->getUrl()); |
62
|
|
|
$this->assertEquals($this->options,$Stub->getOptions()); |
63
|
|
|
$this->assertEmpty($Stub->getData()); |
64
|
|
|
$this->assertEmpty($Stub->getRequest()); |
65
|
|
|
$this->assertEmpty($Stub->getResponse()); |
66
|
|
|
|
67
|
|
|
return $Stub; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param EntryPointStub $Stub |
72
|
|
|
* @return EntryPointStub $Stub |
73
|
|
|
* @depends testConstructor |
74
|
|
|
* @covers ::setOptions |
75
|
|
|
* @covers ::getOptions |
76
|
|
|
* @covers ::getUrl |
77
|
|
|
* @covers ::configureUrl |
78
|
|
|
* @group abstractEP |
79
|
|
|
*/ |
80
|
|
|
public function testSetOptions($Stub){ |
81
|
|
|
$Stub->setOptions(array()); |
82
|
|
|
$this->assertEquals($this->url.'$test',$Stub->getUrl()); |
83
|
|
|
$this->assertEquals(array(),$Stub->getOptions()); |
84
|
|
|
$Stub->setOptions($this->options); |
85
|
|
|
$this->assertEquals($this->url.'foo',$Stub->getUrl()); |
86
|
|
|
$this->assertEquals($this->options,$Stub->getOptions()); |
87
|
|
|
|
88
|
|
|
return $Stub; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param EntryPointStub $Stub |
93
|
|
|
* @return EntryPointStub $Stub |
94
|
|
|
* @depends testSetOptions |
95
|
|
|
* @covers ::setData |
96
|
|
|
* @covers ::getData |
97
|
|
|
* @group abstractEP |
98
|
|
|
*/ |
99
|
|
|
public function testSetData($Stub){ |
100
|
|
|
$Stub->setData(array()); |
101
|
|
|
$this->assertEquals(array(),$Stub->getData()); |
102
|
|
|
$Stub->setData('string'); |
103
|
|
|
$this->assertEquals('string',$Stub->getData()); |
104
|
|
|
$class = new \stdClass(); |
105
|
|
|
$class->foo = 'bar'; |
106
|
|
|
$Stub->setData($class); |
107
|
|
|
$this->assertEquals($class,$Stub->getData()); |
108
|
|
|
unset($class); |
109
|
|
|
$Stub->setData($this->data); |
110
|
|
|
return $Stub; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param EntryPointStub $Stub |
115
|
|
|
* @return EntryPointStub $Stub |
116
|
|
|
* @depends testSetData |
117
|
|
|
* @covers ::setRequest |
118
|
|
|
* @covers ::getRequest |
119
|
|
|
* @group abstractEP |
120
|
|
|
*/ |
121
|
|
|
public function testSetRequest($Stub){ |
122
|
|
|
$GET = new GET(); |
123
|
|
|
$POST = new POST(); |
124
|
|
|
$POSTFile = new POSTFile(); |
125
|
|
|
$PUT = new PUT(); |
126
|
|
|
$DELETE = new DELETE(); |
127
|
|
|
$GETFile = new GETFile(); |
128
|
|
|
$Stub->setRequest($POST); |
129
|
|
|
$this->assertEquals($POST,$Stub->getRequest()); |
130
|
|
|
unset($POST); |
131
|
|
|
$Stub->setRequest($POSTFile); |
132
|
|
|
$this->assertEquals($POSTFile,$Stub->getRequest()); |
133
|
|
|
unset($POSTFile); |
134
|
|
|
$Stub->setRequest($PUT); |
135
|
|
|
$this->assertEquals($PUT,$Stub->getRequest()); |
136
|
|
|
unset($PUT); |
137
|
|
|
$Stub->setRequest($DELETE); |
138
|
|
|
$this->assertEquals($DELETE,$Stub->getRequest()); |
139
|
|
|
unset($DELETE); |
140
|
|
|
$Stub->setRequest($GETFile); |
141
|
|
|
$this->assertEquals($GETFile,$Stub->getRequest()); |
142
|
|
|
unset($GETFile); |
143
|
|
|
$Stub->setRequest($GET); |
144
|
|
|
$this->assertEquals($GET,$Stub->getRequest()); |
145
|
|
|
return $Stub; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param EntryPointStub $Stub |
150
|
|
|
* @return EntryPointStub $Stub |
151
|
|
|
* @depends testSetRequest |
152
|
|
|
* @covers ::setAuth |
153
|
|
|
* @covers ::authRequired |
154
|
|
|
* @group abstractEP |
155
|
|
|
*/ |
156
|
|
|
public function testSetAuth($Stub){ |
157
|
|
|
$Stub->setAuth('1234'); |
158
|
|
|
$this->assertEquals(true,$Stub->authRequired()); |
159
|
|
|
return $Stub; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param EntryPointStub $Stub |
164
|
|
|
* @return EntryPointStub $Stub |
165
|
|
|
* @depends testSetAuth |
166
|
|
|
* @covers ::execute |
167
|
|
|
* @covers ::configureRequest |
168
|
|
|
* @covers ::verifyUrl |
169
|
|
|
* @covers ::verifyData |
170
|
|
|
* @covers ::verifyRequiredData |
171
|
|
|
* @covers ::verifyDataType |
172
|
|
|
* @covers ::verifyOptions |
173
|
|
|
* @covers ::configureData |
174
|
|
|
* @covers ::configureDefaultData |
175
|
|
|
* @covers ::configureUrl |
176
|
|
|
* @covers ::configureAuth |
177
|
|
|
* @group abstractEP |
178
|
|
|
*/ |
179
|
|
|
public function testExecute($Stub){ |
180
|
|
|
$Stub->execute(); |
181
|
|
|
$this->assertEquals(array( |
182
|
|
|
'foo' => 'bar', |
183
|
|
|
'bar' => 'foo' |
184
|
|
|
),$Stub->getData()); |
185
|
|
|
$this->assertEquals($this->url.'foo?foo=bar&bar=foo',$Stub->getRequest()->getUrl()); |
186
|
|
|
$this->assertEquals(array('OAuth-Token: 1234'),$Stub->getRequest()->getHeaders()); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @covers ::verifyDataType |
191
|
|
|
* @expectedException SugarAPI\SDK\Exception\EntryPoint\RequiredDataException |
192
|
|
|
* @expectedExceptionMessageRegExp /Valid DataType is array/ |
193
|
|
|
* @group abstractEP |
194
|
|
|
*/ |
195
|
|
|
public function testInvalidDataType(){ |
196
|
|
|
$Stub = new EntryPointStub($this->url); |
197
|
|
|
$Stub->setOptions($this->options); |
198
|
|
|
$Stub->setRequest(new POST()); |
199
|
|
|
$class = new \stdClass(); |
200
|
|
|
$class->foo = 'bar'; |
201
|
|
|
$Stub->setData($class); |
202
|
|
|
$Stub->execute(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @covers ::verifyRequiredData |
207
|
|
|
* @expectedException SugarAPI\SDK\Exception\EntryPoint\RequiredDataException |
208
|
|
|
* @expectedExceptionMessageRegExp /Missing data for/ |
209
|
|
|
* @group abstractEP |
210
|
|
|
*/ |
211
|
|
View Code Duplication |
public function testInvalidData(){ |
|
|
|
|
212
|
|
|
$Stub = new EntryPointStub($this->url); |
213
|
|
|
$Stub->setOptions($this->options); |
214
|
|
|
$Stub->setRequest(new POST()); |
215
|
|
|
$Stub->setData(array()); |
216
|
|
|
$Stub->execute(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @covers ::execute |
221
|
|
|
* @expectedException SugarAPI\SDK\Exception\EntryPoint\InvalidRequestException |
222
|
|
|
* @expectedExceptionMessageRegExp /Request property not configured/ |
223
|
|
|
* @group abstractEP |
224
|
|
|
*/ |
225
|
|
View Code Duplication |
public function testInvalidRequest(){ |
|
|
|
|
226
|
|
|
$Stub = new EntryPointStub($this->url); |
227
|
|
|
$Stub->setOptions($this->options); |
228
|
|
|
$Stub->setData($this->data); |
229
|
|
|
$Stub->execute(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.