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

ClientRequestTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 33
c 1
b 0
f 1
dl 0
loc 84
rs 10
wmc 12

12 Methods

Rating   Name   Duplication   Size   Complexity  
A test_defaults() 0 5 1
A test_request_target_with_query_string() 0 7 1
A test_construction_with_array_body() 0 4 1
A test_construction_with_iterable_body() 0 4 1
A test_should_change_the_method() 0 4 1
A test_with_uri_and_not_preserving_the_host() 0 5 1
A setUp() 0 3 1
A test_request_target() 0 5 1
A tearDown() 0 3 1
A test_uri() 0 3 1
A test_invalid_request_target() 0 7 1
A test_with_uri_preserving_the_host() 0 8 1
1
<?php
2
3
namespace Tests\Koded\Http;
4
5
use InvalidArgumentException;
6
use Koded\Http\ClientRequest;
7
use Koded\Http\Interfaces\HttpMethod;
8
use Koded\Http\Interfaces\HttpStatus;
9
use Koded\Http\Interfaces\Request;
10
use Koded\Http\Uri;
11
use PHPUnit\Framework\TestCase;
12
use Psr\Http\Message\UriInterface;
13
14
class ClientRequestTest extends TestCase
15
{
16
    private ClientRequest|Request $SUT;
17
18
    public function test_defaults()
19
    {
20
        $this->assertSame(HttpMethod::POST->value, $this->SUT->getMethod());
21
        $this->assertInstanceOf(UriInterface::class, $this->SUT->getUri());
22
        $this->assertSame('/', $this->SUT->getRequestTarget(), "No URI (path) and no request-target is provided");
23
    }
24
25
    public function test_uri()
26
    {
27
        $this->assertInstanceOf(Uri::class, $this->SUT->getUri());
28
    }
29
30
    public function test_should_change_the_method()
31
    {
32
        $request = $this->SUT->withMethod('get');
33
        $this->assertSame('GET', $request->getMethod());
34
    }
35
36
    public function test_request_target()
37
    {
38
        $request = $this->SUT->withRequestTarget('42');
39
        $this->assertSame('42', $request->getRequestTarget());
40
        $this->assertNotSame($request, $this->SUT);
41
    }
42
43
    public function test_request_target_with_query_string()
44
    {
45
        $uri     = new Uri('http://example.net/home?foo=bared');
46
        $request = $this->SUT->withUri($uri);
47
48
        $this->assertSame('/home?foo=bared', $request->getRequestTarget());
49
        $this->assertNotSame($request, $this->SUT);
50
    }
51
52
    public function test_invalid_request_target()
53
    {
54
        $this->expectException(InvalidArgumentException::class);
55
        $this->expectExceptionMessage(ClientRequest::E_INVALID_REQUEST_TARGET);
56
        $this->expectExceptionCode(HttpStatus::BAD_REQUEST);
57
58
        $this->SUT->withRequestTarget('foo bar');
59
    }
60
61
    public function test_with_uri_preserving_the_host()
62
    {
63
        $uri     = new Uri('http://example.net/42');
64
        $request = $this->SUT->withUri($uri, true);
65
66
        $this->assertSame(['example.org'], $request->getHeader('host'), 'The previous host is preserved in the header');
67
        $this->assertSame('example.net', $request->getUri()->getHost(), 'Request URI has its own hostname');
68
        $this->assertEquals('/42', $request->getPath());
0 ignored issues
show
Bug introduced by
The method getPath() does not exist on Psr\Http\Message\RequestInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Koded\Http\Interfaces\HttpRequestClient or Psr\Http\Message\ServerRequestInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

68
        $this->assertEquals('/42', $request->/** @scrutinizer ignore-call */ getPath());
Loading history...
69
    }
70
71
    public function test_with_uri_and_not_preserving_the_host()
72
    {
73
        $uri     = new Uri('http://example.net/42');
74
        $request = $this->SUT->withUri($uri, false);
75
        $this->assertSame(['example.net'], $request->getHeader('host'), 'Host is taken from Uri');
76
    }
77
78
    public function test_construction_with_array_body()
79
    {
80
        $this->SUT = new ClientRequest(HttpMethod::GET, 'http://example.org', ['foo' => 'bar']);
81
        $this->assertSame('{"foo":"bar"}', $this->SUT->getBody()->getContents());
82
    }
83
84
    public function test_construction_with_iterable_body()
85
    {
86
        $this->SUT = new ClientRequest(HttpMethod::GET, 'http://example.org', new \ArrayObject(['foo' => 'bar']));
87
        $this->assertSame('{"foo":"bar"}', $this->SUT->getBody()->getContents());
88
    }
89
90
    protected function setUp(): void
91
    {
92
        $this->SUT = new ClientRequest(HttpMethod::POST, 'http://example.org');
93
    }
94
95
    protected function tearDown(): void
96
    {
97
        $_SERVER['REQUEST_METHOD'] = 'POST';
98
    }
99
}
100