PublisherTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 50
c 2
b 0
f 0
dl 0
loc 86
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testPublishErrorOnInvalid() 0 8 1
A testMissingTransport() 0 7 1
A testMultipleTransports() 0 17 1
A testGetTransports() 0 13 1
A setUp() 0 11 1
A testPublish() 0 11 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;
14
15
use Gelf\MessageInterface;
16
use Gelf\MessageValidatorInterface;
17
use Gelf\Publisher;
18
use Gelf\Transport\TransportInterface;
19
use PHPUnit\Framework\MockObject\MockObject;
20
use PHPUnit\Framework\TestCase;
21
use RuntimeException;
22
23
class PublisherTest extends TestCase
24
{
25
    private MockObject|TransportInterface $transportA;
26
    private MockObject|TransportInterface $transportB;
27
    private MockObject|MessageValidatorInterface $messageValidator;
28
    private MockObject|MessageInterface $message;
29
    private Publisher $publisher;
30
31
    public function setUp(): void
32
    {
33
        $this->transportA = $this->createMock(TransportInterface::class);
34
        $this->transportB = $this->createMock(TransportInterface::class);
35
        $this->messageValidator =
36
            $this->createMock(MessageValidatorInterface::class);
37
        $this->message = $this->createMock(MessageInterface::class);
38
39
        $this->publisher = new Publisher(
40
            $this->transportA,
41
            $this->messageValidator
42
        );
43
    }
44
45
    public function testPublish(): void
46
    {
47
        $this->transportA->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Gelf\Transport\TransportInterface. ( Ignorable by Annotation )

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

47
        $this->transportA->/** @scrutinizer ignore-call */ 
48
                           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...
48
            ->method('send')
49
            ->with($this->equalTo($this->message));
50
51
        $this->messageValidator->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Gelf\MessageValidatorInterface. ( Ignorable by Annotation )

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

51
        $this->messageValidator->/** @scrutinizer ignore-call */ 
52
                                 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...
52
            ->method('validate')
53
            ->will($this->returnValue(true));
54
55
        $this->publisher->publish($this->message);
56
    }
57
58
    public function testPublishErrorOnInvalid(): 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
        $this->messageValidator->expects($this->once())
62
            ->method('validate')
63
            ->will($this->returnValue(false));
64
65
        $this->publisher->publish($this->message);
66
    }
67
68
    public function testMissingTransport(): void
69
    {
70
        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

70
        self::/** @scrutinizer ignore-call */ 
71
              expectException(RuntimeException::class);
Loading history...
71
        $publisher = new Publisher(null, $this->messageValidator);
72
        self::assertCount(0, $publisher->getTransports());
73
74
        $publisher->publish($this->message);
75
    }
76
77
    public function testMultipleTransports(): void
78
    {
79
        $pub = $this->publisher;
80
        $pub->addTransport($this->transportB);
81
        $this->transportA->expects($this->once())
82
            ->method('send')
83
            ->with($this->equalTo($this->message));
84
85
        $this->transportB->expects($this->once())
86
            ->method('send')
87
            ->with($this->equalTo($this->message));
88
89
        $this->messageValidator->expects($this->once())
90
            ->method('validate')
91
            ->will($this->returnValue(true));
92
93
        $pub->publish($this->message);
94
    }
95
96
    public function testGetTransports(): void
97
    {
98
        $pub = new Publisher(null, $this->messageValidator);
99
        self::assertCount(0, $pub->getTransports());
100
101
        $pub->addTransport($this->transportA);
102
        self::assertCount(1, $pub->getTransports());
103
104
        $pub->addTransport($this->transportB);
105
        self::assertCount(2, $pub->getTransports());
106
107
        $pub->addTransport($this->transportA);
108
        self::assertCount(2, $pub->getTransports());
109
    }
110
}
111