Passed
Push — master ( 8dceab...2d1996 )
by Benjamin
15:07 queued 12:31
created

UdpTransportTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 43
c 3
b 1
f 0
dl 0
loc 84
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testSendChunked() 0 16 1
A testInvalidChunkNumber() 0 6 1
A testSendUnchunked() 0 8 1
A setUp() 0 14 1
A getTransport() 0 14 1
A testSetEncoder() 0 6 1
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * This file is part of the php-gelf package.
6
 *
7
 * (c) Benjamin Zikarsky <http://benjamin-zikarsky.de>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Gelf\Test\Transport;
14
15
use Gelf\Encoder\EncoderInterface;
16
use Gelf\MessageInterface;
17
use Gelf\Transport\StreamSocketClient;
18
use Gelf\Transport\UdpTransport;
19
use PHPUnit\Framework\MockObject\MockObject;
20
use PHPUnit\Framework\TestCase;
21
use ReflectionObject;
22
use RuntimeException;
23
24
class UdpTransportTest extends TestCase
25
{
26
    private const CHUNK_HEADER_LENGTH = 12;
27
28
    private MockObject|StreamSocketClient $socketClient;
29
    private MockObject|MessageInterface $message;
30
    private MockObject|EncoderInterface $encoder;
31
    private UdpTransport $transport;
32
    private string $testMessage;
33
34
    public function setUp(): void
35
    {
36
        $this->testMessage = str_repeat("0123456789", 30); // 300 char string
37
38
        $this->socketClient = $this->createMock(StreamSocketClient::class);
39
        $this->message = $this->createMock(MessageInterface::class);
40
41
        // create an encoder always return $testMessage
42
        $this->encoder = $this->createMock(EncoderInterface::class);
43
        $this->encoder->expects($this->any())->method('encode')->will(
44
            $this->returnValue($this->testMessage)
45
        );
46
47
        $this->transport = $this->getTransport(0);
48
    }
49
50
    private function getTransport(int $chunkSize): UdpTransport
51
    {
52
        // initialize transport with an unlimited packet-size
53
        // and the mocked message encoder
54
        $transport = new UdpTransport("", 0, $chunkSize);
55
        $transport->setMessageEncoder($this->encoder);
56
57
        // replace internal stream socket client with our mock
58
        $reflectedTransport = new ReflectionObject($transport);
59
        $reflectedClient = $reflectedTransport->getProperty('socketClient');
60
        $reflectedClient->setAccessible(true);
61
        $reflectedClient->setValue($transport, $this->socketClient);
62
63
        return $transport;
64
    }
65
66
    public function testSetEncoder(): void
67
    {
68
        $encoder = $this->createMock(EncoderInterface::class);
69
        $this->transport->setMessageEncoder($encoder);
70
71
        self::assertEquals($encoder, $this->transport->getMessageEncoder());
72
    }
73
74
    public function testSendUnchunked(): void
75
    {
76
        $this->socketClient
77
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Gelf\Transport\StreamSocketClient. ( Ignorable by Annotation )

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

77
            ->/** @scrutinizer ignore-call */ 
78
              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...
78
            ->method('write')
79
            ->with($this->testMessage);
80
81
        $this->transport->send($this->message);
82
    }
83
84
    public function testSendChunked(): void
85
    {
86
        $chunkSize = 20 + self::CHUNK_HEADER_LENGTH;
87
        $transport = $this->getTransport($chunkSize);
88
        $expectedMessageCount =  strlen($this->testMessage) / ($chunkSize - self::CHUNK_HEADER_LENGTH);
89
90
        $test = $this;
91
        $this->socketClient
92
            ->expects($this->exactly($expectedMessageCount))
93
            ->method('write')
94
            ->willReturnCallback(function ($data) use ($chunkSize, $test) {
95
                $test->assertLessThanOrEqual($chunkSize, strlen($data));
96
                return 1;
97
            });
98
99
        $transport->send($this->message);
100
    }
101
102
    public function testInvalidChunkNumber()
103
    {
104
        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

104
        self::/** @scrutinizer ignore-call */ 
105
              expectException(RuntimeException::class);
Loading history...
105
106
        $transport = $this->getTransport(self::CHUNK_HEADER_LENGTH + 1);
107
        $transport->send($this->message);
108
    }
109
}
110