1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the curl-library package. |
5
|
|
|
* |
6
|
|
|
* (c) 2017 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Library\CURL\Tests\Request; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use WBW\Library\Core\Exception\Argument\StringArgumentException; |
16
|
|
|
use WBW\Library\CURL\Request\CURLPostRequest; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* cURL POST request test. |
20
|
|
|
* |
21
|
|
|
* @author webeweb <https://github.com/webeweb/> |
22
|
|
|
* @package WBW\Library\CURL\Tests\Request |
23
|
|
|
* @final |
24
|
|
|
*/ |
25
|
|
|
final class CURLPostRequestTest extends AbstractCURLRequestTest { |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Tests __construct() method. |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public function testConstruct() { |
33
|
|
|
|
34
|
|
|
$obj = new CURLPostRequest($this->configuration, self::RESOURCE_PATH); |
35
|
|
|
|
36
|
|
|
$this->assertSame($this->configuration, $obj->getConfiguration()); |
37
|
|
|
$this->assertEquals([], $obj->getHeaders()); |
38
|
|
|
$this->assertEquals(CURLPostRequest::HTTP_METHOD_POST, $obj->getMethod()); |
39
|
|
|
$this->assertEquals([], $obj->getPostData()); |
40
|
|
|
$this->assertEquals([], $obj->getQueryData()); |
41
|
|
|
$this->assertEquals("testCall.php", $obj->getResourcePath()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Tests addPostData() method. |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function testAddPostData() { |
50
|
|
|
|
51
|
|
|
$obj = new CURLPostRequest($this->configuration, self::RESOURCE_PATH); |
52
|
|
|
|
53
|
|
|
// === |
54
|
|
|
try { |
55
|
|
|
|
56
|
|
|
$obj->addPostData(1, "value"); |
57
|
|
|
} catch (Exception $ex) { |
58
|
|
|
|
59
|
|
|
$this->assertInstanceOf(StringArgumentException::class, $ex); |
60
|
|
|
$this->assertEquals("The argument \"1\" is not a string", $ex->getMessage()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// === |
64
|
|
|
$obj->addPostData("name", "value"); |
65
|
|
|
|
66
|
|
|
$res = ["name" => "value"]; |
67
|
|
|
$this->assertEquals($res, $obj->getPostData()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Tests call() method. |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function testCall() { |
76
|
|
|
|
77
|
|
|
$obj = new CURLPostRequest($this->configuration, self::RESOURCE_PATH); |
78
|
|
|
|
79
|
|
|
$obj->addHeader("header", "header"); |
80
|
|
|
$obj->addQueryData("queryData", "queryData"); |
81
|
|
|
|
82
|
|
|
$res = $obj->call(); |
83
|
|
|
|
84
|
|
|
$this->assertContains("header: header", $res->getRequestHeader()); |
85
|
|
|
$this->assertContains("queryData=queryData", $res->getRequestURL()); |
86
|
|
|
$this->assertEquals(CURLPostRequest::HTTP_METHOD_POST, json_decode($res->getResponseBody(), true)["method"]); |
87
|
|
|
$this->assertEquals(200, $res->getResponseInfo()["http_code"]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Tests the clearPostData() method. |
92
|
|
|
* |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
public function testClearPostData() { |
96
|
|
|
|
97
|
|
|
$obj = new CURLPostRequest($this->configuration, self::RESOURCE_PATH); |
98
|
|
|
|
99
|
|
|
$obj->addPostData("name", "value"); |
100
|
|
|
$this->assertCount(1, $obj->getPostData()); |
101
|
|
|
|
102
|
|
|
$obj->clearPostData(); |
103
|
|
|
$this->assertCount(0, $obj->getPostData()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Tests removePostData() method. |
108
|
|
|
* |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
public function testRemovePostData() { |
112
|
|
|
|
113
|
|
|
$obj = new CURLPostRequest($this->configuration, self::RESOURCE_PATH); |
114
|
|
|
|
115
|
|
|
$obj->addPostData("name", "value"); |
116
|
|
|
$this->assertCount(1, $obj->getPostData()); |
117
|
|
|
|
118
|
|
|
$obj->removePostData("Name"); |
119
|
|
|
$this->assertCount(1, $obj->getPostData()); |
120
|
|
|
|
121
|
|
|
$obj->removePostData("name"); |
122
|
|
|
$this->assertCount(0, $obj->getPostData()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|