Completed
Push — master ( 06ed76...0f3f4f )
by WEBEWEB
06:00 queued 14s
created

CURLPostRequestTest::testConstructor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the curl-library package.
5
 *
6
 * (c) 2017 NdC/WBW
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 NdC/WBW <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 testConstructor() {
33
34
		$obj = new CURLPostRequest($this->configuration, self::RESOURCE_PATH);
35
36
		$this->assertEquals($this->configuration, $obj->getConfiguration());
37
		$this->assertEquals([], $obj->getHeaders());
38
		$this->assertEquals(CURLPostRequest::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
		try {
54
			$obj->addPostData(1, "value");
55
		} catch (Exception $ex) {
56
			$this->assertInstanceOf(StringArgumentException::class, $ex);
57
			$this->assertEquals("The argument \"1\" is not a string", $ex->getMessage());
58
		}
59
60
		$obj->addPostData("name", "value");
61
62
		$res = ["name" => "value"];
63
		$this->assertEquals($res, $obj->getPostData());
64
	}
65
66
	/**
67
	 * Tests call() method.
68
	 *
69
	 * @return void
70
	 */
71
	public function testCall() {
72
73
		$obj = new CURLPostRequest($this->configuration, self::RESOURCE_PATH);
74
75
		$obj->addHeader("header", "header");
76
		$obj->addQueryData("queryData", "queryData");
77
78
		$res = $obj->call();
79
80
		$this->assertContains("header: header", $res->getRequestHeader());
81
		$this->assertContains("queryData=queryData", $res->getRequestURL());
82
		$this->assertEquals(CURLPostRequest::METHOD_POST, json_decode($res->getResponseBody(), true)["method"]);
83
		$this->assertEquals(200, $res->getResponseInfo()["http_code"]);
84
	}
85
86
	/**
87
	 * Tests the clearPostData() method.
88
	 *
89
	 * @return void
90
	 */
91
	public function testClearPostData() {
92
93
		$obj = new CURLPostRequest($this->configuration, self::RESOURCE_PATH);
94
95
		$obj->addPostData("name", "value");
96
		$this->assertCount(1, $obj->getPostData());
97
98
		$obj->clearPostData();
99
		$this->assertCount(0, $obj->getPostData());
100
	}
101
102
	/**
103
	 * Tests removePostData() method.
104
	 *
105
	 * @return void
106
	 */
107
	public function testRemovePostData() {
108
109
		$obj = new CURLPostRequest($this->configuration, self::RESOURCE_PATH);
110
111
		$obj->addPostData("name", "value");
112
		$this->assertCount(1, $obj->getPostData());
113
114
		$obj->removePostData("Name");
115
		$this->assertCount(1, $obj->getPostData());
116
117
		$obj->removePostData("name");
118
		$this->assertCount(0, $obj->getPostData());
119
	}
120
121
}
122