Passed
Push — master ( 2cd332...0af4da )
by Ross
02:35
created

CurlTest::itCanSetTheRequiredOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 0
1
<?php
2
/**
3
 *
4
 * Copyright (C) 2018  Ross Mitchell
5
 *
6
 * This file is part of RossMitchell/UpdateCloudFlare.
7
 *
8
 * RossMitchell/UpdateCloudFlare is free software: you can redistribute
9
 * it and/or modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
namespace RossMitchell\UpdateCloudFlare\Tests\Model;
23
24
use RossMitchell\UpdateCloudFlare\Model\Curl;
25
use RossMitchell\UpdateCloudFlare\Tests\AbstractTestClass;
26
use RossMitchell\UpdateCloudFlare\Tests\Fakes\CurlResource;
27
use RossMitchell\UpdateCloudFlare\Tests\Fakes\Request;
28
29
class CurlTest extends AbstractTestClass
30
{
31
    /**
32
     * @Inject
33
     * @var CurlResource
34
     */
35
    private $resource;
36
37
    /**
38
     * @test
39
     */
40
    public function itCanSetTheRequiredOptions()
41
    {
42
        $request = $this->getRequest();
43
        $curl    = $this->getClass();
44
        $curl->makeRequest($request);
45
        $requiredOptions = [
46
            \CURLOPT_URL            => 'http://www.example.com',
47
            \CURLOPT_USERAGENT      => 'curl',
48
            \CURLOPT_CUSTOMREQUEST  => 'GET',
49
            \CURLOPT_RETURNTRANSFER => true,
50
        ];
51
        foreach ($requiredOptions as $option => $value) {
52
            $this->assertEquals($value, $this->resource->getOption($option));
53
        }
54
    }
55
56
    /**
57
     * @test
58
     */
59
    public function itWillSetTheHeadersWhenPresent()
60
    {
61
        $request         = $this->getRequest();
62
        $expectedHeaders = ['testHeader: test'];
63
        $request->setHeaders($expectedHeaders);
64
        $curl = $this->getClass();
65
        $curl->makeRequest($request);
66
        $headers = $this->resource->getOption(\CURLOPT_HTTPHEADER);
67
        $this->assertInternalType('array', $headers);
68
        $this->assertCount(1, $headers);
69
        $this->assertEquals($expectedHeaders, $headers);
70
    }
71
72
    /**
73
     * @test
74
     */
75
    public function itWillNotSetTheHeadersWhenTheyAreNotPresent()
76
    {
77
        $request = $this->getRequest();
78
        $curl    = $this->getClass();
79
        $curl->makeRequest($request);
80
        $this->expectException(\LogicException::class);
81
        $this->resource->getOption(\CURLOPT_HTTPHEADER);
82
    }
83
84
    /**
85
     * @test
86
     */
87
    public function itWillSetTheFieldsWhenPresent()
88
    {
89
        $request        = $this->getRequest();
90
        $expectedFields = ['testHeader: test'];
91
        $request->setFields($expectedFields);
92
        $curl = $this->getClass();
93
        $curl->makeRequest($request);
94
        $headers = $this->resource->getOption(\CURLOPT_POSTFIELDS);
95
        $this->assertInternalType('string', $headers);
96
        $this->assertEquals(\json_encode($expectedFields), $headers);
97
    }
98
99
    /**
100
     * @test
101
     */
102
    public function itWillNotSetTheFieldsWhenTheyAreNotPresent()
103
    {
104
        $request = $this->getRequest();
105
        $curl    = $this->getClass();
106
        $curl->makeRequest($request);
107
        $this->expectException(\LogicException::class);
108
        $this->resource->getOption(\CURLOPT_POSTFIELDS);
109
    }
110
111
    /**
112
     * @test
113
     */
114
    public function itWillThrowAnExceptionWhenThereIsACurlError()
115
    {
116
        $request = $this->getRequest();
117
        $curl = $this->getClass();
118
        $this->resource->setError('An error has occurred');
119
        $this->expectException(\RuntimeException::class);
120
        $curl->makeRequest($request);
121
    }
122
123
124
    /**
125
     * @return Curl
126
     */
127
    private function getClass(): Curl
128
    {
129
        return new Curl($this->resource);
130
    }
131
132
    /**
133
     * @param string $url
134
     * @param string $requestType
135
     *
136
     * @return Request
137
     */
138
    private function getRequest(string $url = 'http://www.example.com', string $requestType = 'GET'): Request
139
    {
140
        $request = new Request();
141
        $request->setUrl($url);
142
        $request->setRequestType($requestType);
143
144
        return $request;
145
    }
146
}
147