Completed
Push — master ( 939a81...dc41f6 )
by Andy
02:26
created

CurlTest::testGetUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Palmtree\Curl\Tests;
4
5
use Palmtree\Curl\Curl;
6
use Palmtree\Curl\Exception\BadMethodCallException;
7
use Palmtree\Curl\Exception\InvalidArgumentException;
8
use Palmtree\Curl\Tests\Fixtures\WebServer;
9
use PHPUnit\Framework\TestCase;
10
11
class CurlTest extends TestCase
12
{
13
    /** @var WebServer */
14
    private $server;
15
16
    public function __construct()
17
    {
18
        $this->server = new WebServer('localhost', __DIR__ . '/fixtures/server');
19
        $this->server->start();
20
21
        parent::__construct();
22
    }
23
24
    public function __destruct()
25
    {
26
        $this->server->stop();
27
    }
28
29
    public function testGetUrl()
30
    {
31
        $curl = new Curl('https://example.org');
32
33
        $this->assertSame('https://example.org', $curl->getUrl());
34
    }
35
36
    public function testCantSetCurlOptHeader()
37
    {
38
        $this->expectException(InvalidArgumentException::class);
39
40
        $curl = new Curl('https://example.org');
41
42
        $curl->setOpt(CURLOPT_HEADER, false);
43
    }
44
45
    public function testDefaultCurlOpts()
46
    {
47
        $curl = new Curl('https://example.org');
48
49
        $opts = $curl->getOpts();
50
51
        $this->assertArrayHasKey(CURLOPT_USERAGENT, $opts);
52
53
        $this->assertSame($opts[CURLOPT_USERAGENT], 'Palmtree\Curl');
54
    }
55
56
    public function testCurlOptOverride()
57
    {
58
        $curl = new Curl('https://example.org', [
59
            CURLOPT_USERAGENT => 'Palmtree\Curl\Tests',
60
        ]);
61
62
        $opts = $curl->getOpts();
63
64
        $this->assertArrayHasKey(CURLOPT_USERAGENT, $opts);
65
66
        $this->assertSame($opts[CURLOPT_USERAGENT], 'Palmtree\Curl\Tests');
67
    }
68
69
    public function testPost()
70
    {
71
        $curl = new Curl($this->server->getUrl('post.php'));
72
73
        $response = $curl->post(['foo' => 'bar']);
74
75
        $this->assertSame('true', $response->getBody());
76
    }
77
78
    public function testPostJson()
79
    {
80
        $curl = new Curl($this->server->getUrl('json.php'));
81
82
        $response = $curl->postJson(\json_encode(['foo' => true]));
83
84
        $this->assertSame('true', $response->getBody());
85
    }
86
87
    public function testGet()
88
    {
89
        $curl = new Curl($this->server->getUrl('get.php'));
90
91
        $response = $curl->getResponse();
92
93
        $this->assertSame('foo', $response->getBody());
94
    }
95
96
    public function testCannotExecuteMoreThanOnce()
97
    {
98
        $this->expectException(BadMethodCallException::class);
99
100
        $curl = new Curl($this->server->getUrl());
101
102
        $curl->execute();
103
        $curl->execute();
104
    }
105
}
106