CurlTest::getRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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