KeepAliveRetryTransportWrapperTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 43
c 5
b 0
f 0
dl 0
loc 77
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testSendSuccess() 0 10 1
A testSendFailTwiceWithoutResponse() 0 17 1
A testSendSuccessAfterRetry() 0 15 1
A testSendFailWithUnmanagedException() 0 13 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Gelf\Test\Transport;
5
6
use Gelf\Message;
7
use Gelf\Transport\HttpTransport;
8
use Gelf\Transport\KeepAliveRetryTransportWrapper;
9
use PHPUnit\Framework\TestCase;
10
use PHPUnit\Framework\MockObject\MockObject;
11
use RuntimeException;
12
13
class KeepAliveRetryTransportWrapperTest extends TestCase
14
{
15
    private const FAILURE_MESSAGE
16
        = "Graylog-Server didn't answer properly, expected 'HTTP/1.x 202 Accepted', response is ''";
17
18
    private Message $message;
19
    private HttpTransport|MockObject $transport;
20
    private KeepAliveRetryTransportWrapper $wrapper;
21
22
    public function setUp(): void
23
    {
24
        $this->message = new Message();
25
        $this->transport = $this->createMock(HttpTransport::class);
26
        $this->wrapper   = new KeepAliveRetryTransportWrapper($this->transport);
27
    }
28
29
    public function testSendSuccess(): void
30
    {
31
        $this->transport->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Gelf\Transport\HttpTransport. ( Ignorable by Annotation )

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

31
        $this->transport->/** @scrutinizer ignore-call */ 
32
                          expects($this->once())

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...
32
            ->method('send')
33
            ->with($this->message)
34
            ->will($this->returnValue(42));
35
36
        $bytes = $this->wrapper->send($this->message);
37
38
        self::assertEquals(42, $bytes);
39
    }
40
41
    public function testSendSuccessAfterRetry(): void
42
    {
43
        $expectedException = new RuntimeException(self::FAILURE_MESSAGE);
44
45
        $this->transport->expects($this->exactly(2))
46
            ->method('send')
47
            ->with($this->message)
48
            ->will($this->onConsecutiveCalls(
49
                $this->throwException($expectedException),
50
                42
51
            ));
52
53
        $bytes = $this->wrapper->send($this->message);
54
55
        self::assertEquals(42, $bytes);
56
    }
57
58
    public function testSendFailTwiceWithoutResponse(): void
59
    {
60
        self::expectException(RuntimeException::class);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

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

60
        self::/** @scrutinizer ignore-call */ 
61
              expectException(RuntimeException::class);
Loading history...
61
        self::expectExceptionMessage("response is ''");
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCa...xpectExceptionMessage() is not static, but was called statically. ( Ignorable by Annotation )

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

61
        self::/** @scrutinizer ignore-call */ 
62
              expectExceptionMessage("response is ''");
Loading history...
62
63
        $expectedException1 = new RuntimeException(self::FAILURE_MESSAGE);
64
        $expectedException2 = new RuntimeException(self::FAILURE_MESSAGE);
65
66
        $this->transport->expects($this->exactly(2))
67
            ->method('send')
68
            ->with($this->message)
69
            ->will($this->onConsecutiveCalls(
70
                $this->throwException($expectedException1),
71
                $this->throwException($expectedException2)
72
            ));
73
74
        $this->wrapper->send($this->message);
75
    }
76
77
    public function testSendFailWithUnmanagedException(): void
78
    {
79
        $this->expectException(RuntimeException::class);
80
        $this->expectExceptionMessage("foo");
81
82
        $expectedException = new RuntimeException('foo');
83
84
        $this->transport->expects($this->once())
85
            ->method('send')
86
            ->with($this->message)
87
            ->willThrowException($expectedException);
88
89
        $this->wrapper->send($this->message);
90
    }
91
}
92