Completed
Push — master ( 4bbbcd...b12113 )
by Andy
01:43
created

CurlTest::testPostArrayAsJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Palmtree\Curl\Tests;
4
5
use Palmtree\Curl\Curl;
6
use Palmtree\Curl\CurlError;
7
use Palmtree\Curl\Exception\BadMethodCallException;
8
use Palmtree\Curl\Exception\CurlErrorException;
9
use Palmtree\Curl\Exception\InvalidArgumentException;
10
use Palmtree\Curl\Tests\Fixtures\WebServer;
11
use PHPUnit\Framework\TestCase;
12
13
class CurlTest extends TestCase
14
{
15
    /** @var WebServer */
16
    private $server;
17
18
    public function __construct()
19
    {
20
        $this->server = new WebServer('localhost', __DIR__ . '/fixtures/server');
21
        $this->server->start();
22
23
        parent::__construct();
24
    }
25
26
    public function __destruct()
27
    {
28
        $this->server->stop();
29
    }
30
31
    public function testGetUrl()
32
    {
33
        $curl = new Curl('https://example.org');
34
35
        $this->assertSame('https://example.org', $curl->getUrl());
36
    }
37
38
    public function testCantSetCurlOptHeader()
39
    {
40
        $this->expectException(InvalidArgumentException::class);
41
42
        $curl = new Curl('https://example.org');
43
44
        $curl->getCurlOpts()->set(CURLOPT_HEADER, false);
45
    }
46
47
    public function testDefaultCurlOpts()
48
    {
49
        $curl = new Curl('https://example.org');
50
51
        $opts = $curl->getCurlOpts()->toArray();
52
53
        $this->assertArrayHasKey(CURLOPT_USERAGENT, $opts);
54
55
        $this->assertSame($opts[CURLOPT_USERAGENT], 'Palmtree\Curl');
56
    }
57
58
    public function testCurlOptOverride()
59
    {
60
        $curl = new Curl('https://example.org', [
61
            CURLOPT_USERAGENT => 'Palmtree\Curl\Tests',
62
        ]);
63
64
        $opts = $curl->getCurlOpts()->toArray();
65
66
        $this->assertArrayHasKey(CURLOPT_USERAGENT, $opts);
67
68
        $this->assertSame($opts[CURLOPT_USERAGENT], 'Palmtree\Curl\Tests');
69
    }
70
71
    public function testCurlSetOpt()
72
    {
73
        $curl = new Curl('https://example.org');
74
75
        $curl->getCurlOpts()->set(CURLOPT_USERAGENT, 'Palmtree\Curl\Tests');
76
77
        $this->assertArrayHasKey(CURLOPT_USERAGENT, $curl->getCurlOpts()->toArray());
78
        $this->assertSame($curl->getCurlOpts()[CURLOPT_USERAGENT], 'Palmtree\Curl\Tests');
79
    }
80
81
    public function testPost()
82
    {
83
        $curl = new Curl($this->server->getUrl('post.php'));
84
85
        $response = $curl->post(['foo' => 'bar']);
86
87
        $this->assertSame('true', $response->getBody());
88
    }
89
90
    public function testPostJson()
91
    {
92
        $curl = new Curl($this->server->getUrl('json.php'));
93
94
        $response = $curl->postJson(\json_encode(['foo' => true]));
95
96
        $this->assertSame('true', $response->getBody());
97
    }
98
99
    public function testPostArrayAsJson()
100
    {
101
        $curl = new Curl($this->server->getUrl('json.php'));
102
103
        $response = $curl->postJson(['foo' => true]);
104
105
        $this->assertSame('true', $response->getBody());
106
    }
107
108
    public function testGet()
109
    {
110
        $curl = new Curl($this->server->getUrl('get.php'));
111
112
        $response = $curl->getResponse();
113
114
        $this->assertSame('foo', $response->getBody());
115
    }
116
117
    public function testCannotExecuteMoreThanOnce()
118
    {
119
        $this->expectException(BadMethodCallException::class);
120
121
        $curl = new Curl($this->server->getUrl());
122
123
        $curl->execute();
124
        $curl->execute();
125
    }
126
127
    public function testCurlGetContents()
128
    {
129
        $contents = Curl::getContents($this->server->getUrl('get.php'));
130
131
        $this->assertSame('foo', $contents);
132
    }
133
134
    public function testToString()
135
    {
136
        $curl = new Curl($this->server->getUrl('get.php'));
137
138
        $this->assertSame('foo', (string)$curl);
139
    }
140
141
    public function testCurlExceptionError()
142
    {
143
        $curl = new Curl('foo://bar');
144
145
        try {
146
            $curl->execute();
147
            $this->fail('Expected exception ' . CurlError::UNSUPPORTED_PROTOCOL . ' not thrown');
148
        } catch (CurlErrorException $exception) {
149
            $this->assertSame(CurlError::UNSUPPORTED_PROTOCOL, $exception->getCode());
150
        }
151
    }
152
153
    public function testCurlExceptionInToString()
154
    {
155
        $curl = new Curl('foo://bar');
156
157
        $this->assertSame('', (string)$curl);
158
    }
159
}
160