Passed
Pull Request — master (#17)
by Mihail
15:10
created

PhpClientTest::test_php_factory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 20
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 26
rs 9.6
1
<?php
2
3
namespace Tests\Koded\Http\Client;
4
5
use Koded\Http\Client\ClientFactory;
6
use Koded\Http\Client\PhpClient;
7
use Koded\Http\Interfaces\ClientType;
8
use Koded\Http\Interfaces\HttpMethod;
9
use Koded\Http\Interfaces\HttpStatus;
10
use Koded\Http\ServerResponse;
11
use PHPUnit\Framework\TestCase;
12
use Tests\Koded\Http\AssertionTestSupportTrait;
13
14
class PhpClientTest extends TestCase
15
{
16
    use ClientTestCaseTrait, AssertionTestSupportTrait;
17
18
    public function test_php_factory()
19
    {
20
        $options = $this->getObjectProperty($this->SUT, 'options');
0 ignored issues
show
Bug introduced by
It seems like $this->SUT can also be of type null; however, parameter $objectOrString of Tests\Koded\Http\Client\...st::getObjectProperty() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

20
        $options = $this->getObjectProperty(/** @scrutinizer ignore-type */ $this->SUT, 'options');
Loading history...
21
        // keys
22
        $this->assertArrayNotHasKey('header', $options, 'Headers are not set up until read()');
23
        $this->assertArrayHasKey('protocol_version', $options);
24
        $this->assertArrayHasKey('user_agent', $options);
25
        $this->assertArrayHasKey('method', $options);
26
        $this->assertArrayHasKey('timeout', $options);
27
        $this->assertArrayHasKey('max_redirects', $options);
28
        $this->assertArrayHasKey('follow_location', $options);
29
        $this->assertArrayHasKey('ignore_errors', $options);
30
        // "read_buffer_size" is not set at all by default
31
        $this->assertArrayNotHasKey('read_buffer_size', $options);
32
33
        // values
34
        $this->assertSame(1.1, $options['protocol_version']);
35
        $this->assertStringContainsString(' stream/' . PHP_VERSION, $options['user_agent']);
36
        $this->assertSame('GET', $options['method']);
37
        $this->assertSame(20, $options['max_redirects']);
38
        $this->assertSame(1, $options['follow_location']);
39
        $this->assertTrue($options['ignore_errors']);
40
        $this->assertFalse($options['ssl']['allow_self_signed']);
41
        $this->assertTrue($options['ssl']['verify_peer']);
42
        $this->assertSame(5.0, $options['timeout']);
43
        $this->assertSame('', (string)$this->SUT->getBody(), 'The body is empty');
0 ignored issues
show
Bug introduced by
The method getBody() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        $this->assertSame('', (string)$this->SUT->/** @scrutinizer ignore-call */ getBody(), 'The body is empty');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
    }
45
46
    public function test_setting_the_client_with_methods()
47
    {
48
        $this->SUT
49
            ->ignoreErrors(true)
50
            ->timeout(5)
51
            ->followLocation(false)
52
            ->returnTransfer(false)
53
            ->maxRedirects(2)
54
            ->userAgent('foo')
55
            ->verifySslPeer(false)
56
            ->verifySslHost(true);
57
58
        $options = $this->getObjectProperty($this->SUT, 'options');
0 ignored issues
show
Bug introduced by
It seems like $this->SUT can also be of type null; however, parameter $objectOrString of Tests\Koded\Http\Client\...st::getObjectProperty() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        $options = $this->getObjectProperty(/** @scrutinizer ignore-type */ $this->SUT, 'options');
Loading history...
59
60
        $this->assertSame('foo', $options['user_agent']);
61
        $this->assertSame(5.0, $options['timeout']);
62
        $this->assertSame(2, $options['max_redirects']);
63
        $this->assertSame(0, $options['follow_location']);
64
        $this->assertSame(0, $options['read_buffer_size']);
65
        $this->assertSame(true, $options['ignore_errors']);
66
        $this->assertSame(true, $options['ssl']['allow_self_signed']);
67
        $this->assertSame(false, $options['ssl']['verify_peer']);
68
    }
69
70
    public function test_when_curl_returns_error()
71
    {
72
        $SUT = new class(HttpMethod::GET, 'http://example.com') extends PhpClient
73
        {
74
            protected function hasError($resource): bool
75
            {
76
                return true;
77
            }
78
        };
79
        $response = $SUT->read();
80
81
        $this->assertInstanceOf(ServerResponse::class, $response);
82
        $this->assertSame($response->getHeaderLine('Content-type'), 'application/problem+json');
83
        $this->assertSame(HttpStatus::FAILED_DEPENDENCY, $response->getStatusCode(),
84
            (string)$response->getBody());
85
    }
86
87
    public function test_when_creating_resource_fails()
88
    {
89
        $SUT = new class(HttpMethod::GET, 'http://example.com') extends PhpClient
90
        {
91
            protected function createResource($resource)
92
            {
93
                return false;
94
            }
95
        };
96
        $response = $SUT->read();
97
98
        $this->assertInstanceOf(ServerResponse::class, $response);
99
        $this->assertSame($response->getHeaderLine('Content-type'), 'application/problem+json');
100
        $this->assertSame(HttpStatus::FAILED_DEPENDENCY, $response->getStatusCode());
101
        $this->assertStringContainsString('The HTTP client is not created therefore cannot read anything',
102
            (string)$response->getBody());
103
    }
104
105
    public function test_on_exception()
106
    {
107
        $SUT = new class(HttpMethod::GET, 'http://example.com') extends PhpClient
108
        {
109
            protected function createResource($resource)
110
            {
111
                throw new \Exception('Exception message');
112
            }
113
        };
114
        $response = $SUT->read();
115
116
        $this->assertSame($response->getHeaderLine('Content-type'), 'application/problem+json');
117
        $this->assertSame(HttpStatus::INTERNAL_SERVER_ERROR, $response->getStatusCode());
118
        $this->assertStringContainsString('Exception message', (string)$response->getBody());
119
    }
120
121
    protected function setUp(): void
122
    {
123
        $this->SUT = (new ClientFactory(ClientType::PHP))
124
            ->get('http://example.com')
125
            ->timeout(5);
126
    }
127
}
128