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

ClientTestCaseTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 18
c 2
b 0
f 1
dl 0
loc 39
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A test_read_on_success() 0 8 1
A test_should_exit_on_bad_url() 0 7 1
A test_should_exit_on_bad_request() 0 11 1
A tearDown() 0 3 1
1
<?php
2
3
namespace Tests\Koded\Http\Client;
4
5
use Koded\Http\Interfaces\{HttpRequestClient, HttpStatus};
6
use Koded\Http\Uri;
7
use function Koded\Http\create_stream;
8
9
/**
10
 * Trait ClientTestCaseTrait ensures some consistent behaviour
11
 * across the HTTP client implementations.
12
 *
13
 */
14
15
/**
16
 * @group internet
17
 */
18
trait ClientTestCaseTrait
19
{
20
    private ?HttpRequestClient $SUT;
21
22
    public function test_read_on_success()
23
    {
24
        $this->markTestSkipped();
0 ignored issues
show
Bug introduced by
It seems like markTestSkipped() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

24
        $this->/** @scrutinizer ignore-call */ 
25
               markTestSkipped();
Loading history...
25
        $response = $this->SUT->read();
0 ignored issues
show
Bug introduced by
The method read() 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

25
        /** @scrutinizer ignore-call */ 
26
        $response = $this->SUT->read();

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...
26
27
        $this->assertSame(HttpStatus::OK, $response->getStatusCode(), (string)$response->getBody());
0 ignored issues
show
Bug introduced by
It seems like assertSame() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

27
        $this->/** @scrutinizer ignore-call */ 
28
               assertSame(HttpStatus::OK, $response->getStatusCode(), (string)$response->getBody());
Loading history...
28
        $this->assertStringContainsString('text/html', $response->getHeaderLine('Content-Type'));
0 ignored issues
show
Bug introduced by
It seems like assertStringContainsString() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

28
        $this->/** @scrutinizer ignore-call */ 
29
               assertStringContainsString('text/html', $response->getHeaderLine('Content-Type'));
Loading history...
29
        $this->assertGreaterThan(0, (string)$response->getBody()->getSize());
0 ignored issues
show
Bug introduced by
It seems like assertGreaterThan() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

29
        $this->/** @scrutinizer ignore-call */ 
30
               assertGreaterThan(0, (string)$response->getBody()->getSize());
Loading history...
30
    }
31
32
    public function test_should_exit_on_bad_url()
33
    {
34
        $this->expectException(\InvalidArgumentException::class);
0 ignored issues
show
Bug introduced by
It seems like expectException() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

34
        $this->/** @scrutinizer ignore-call */ 
35
               expectException(\InvalidArgumentException::class);
Loading history...
35
        $this->expectExceptionMessage('Please provide a valid URI');
0 ignored issues
show
Bug introduced by
It seems like expectExceptionMessage() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

35
        $this->/** @scrutinizer ignore-call */ 
36
               expectExceptionMessage('Please provide a valid URI');
Loading history...
36
        $this->expectExceptionCode(HttpStatus::BAD_REQUEST);
0 ignored issues
show
Bug introduced by
It seems like expectExceptionCode() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

36
        $this->/** @scrutinizer ignore-call */ 
37
               expectExceptionCode(HttpStatus::BAD_REQUEST);
Loading history...
37
38
        $this->SUT->withUri(new Uri('scheme://host:port'));
39
    }
40
41
    public function test_should_exit_on_bad_request()
42
    {
43
        /** @var HttpRequestClient $SUT */
44
        $SUT = $this->SUT->withBody(create_stream(json_encode(['foo' => 'bar'])));
45
46
        $badResponse = $SUT->read();
47
48
        $this->assertSame(HttpStatus::BAD_REQUEST, $badResponse->getStatusCode(), get_class($SUT));
49
        $this->assertSame($badResponse->getHeaderLine('Content-type'), 'application/problem+json');
50
        $this->assertStringContainsString('failed to open stream: you should not set the message body with safe HTTP methods',
51
            (string)$badResponse->getBody());
52
    }
53
54
    protected function tearDown(): void
55
    {
56
        $this->SUT = null;
57
    }
58
}
59