Skip to content

Commit

Permalink
Modernize Publisher
Browse files Browse the repository at this point in the history
  • Loading branch information
bzikarsky committed Apr 5, 2022
1 parent 6477205 commit 5237435
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 66 deletions.
37 changes: 11 additions & 26 deletions src/Gelf/Publisher.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

/*
* This file is part of the php-gelf package.
Expand All @@ -24,25 +25,15 @@
*/
class Publisher implements PublisherInterface
{
/**
* @var Set
*/
protected $transports;

/**
* @var MessageValidatorInterface
*/
protected $messageValidator;
private Set $transports;
private MessageValidatorInterface $messageValidator;

/**
* Creates a Publisher for GELF-messages.
*
* @param TransportInterface|null $transport
* @param MessageValidatorInterface|null $messageValidator
*/
public function __construct(
TransportInterface $transport = null,
MessageValidatorInterface $messageValidator = null
?TransportInterface $transport = null,
?MessageValidatorInterface $messageValidator = null
) {
$this->transports = new Set();
$this->messageValidator = $messageValidator
Expand All @@ -54,11 +45,9 @@ public function __construct(
}

/**
* Publish a message over all defined transports
*
* @param MessageInterface $message
* @inheritDoc
*/
public function publish(MessageInterface $message)
public function publish(MessageInterface $message): void
{
if (count($this->transports) == 0) {
throw new RuntimeException(
Expand All @@ -78,11 +67,9 @@ public function publish(MessageInterface $message)
}

/**
* Adds a transport to the publisher.
*
* @param TransportInterface $transport
* Adds a transport object to the publisher.
*/
public function addTransport(TransportInterface $transport)
public function addTransport(TransportInterface $transport): void
{
$this->transports->attach($transport);
}
Expand All @@ -92,17 +79,15 @@ public function addTransport(TransportInterface $transport)
*
* @return TransportInterface[]
*/
public function getTransports()
public function getTransports(): array
{
return iterator_to_array($this->transports);
}

/**
* Returns the current message validator.
*
* @return MessageValidatorInterface
*/
public function getMessageValidator()
public function getMessageValidator(): MessageValidatorInterface
{
return $this->messageValidator;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Gelf/PublisherInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Gelf;

Expand All @@ -20,9 +21,6 @@ interface PublisherInterface

/**
* Publish a message
*
* @param MessageInterface $message
* @return void
*/
public function publish(MessageInterface $message);
public function publish(MessageInterface $message): void;
}
64 changes: 28 additions & 36 deletions tests/Gelf/Test/PublisherTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

/*
* This file is part of the php-gelf package.
Expand All @@ -11,33 +12,37 @@

namespace Gelf\Test;

use Gelf\MessageInterface;
use Gelf\MessageValidatorInterface;
use Gelf\Publisher;
use Gelf\Transport\TransportInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use RuntimeException;

class PublisherTest extends TestCase
{
private MockObject|TransportInterface $transportA;
private MockObject|TransportInterface $transportB;
private MockObject|MessageValidatorInterface $messageValidator;
private MockObject|MessageInterface $message;
private Publisher $publisher;

protected $transportA;
protected $transportB;
protected $messageValidator;
protected $message;
protected $publisher;

public function setUp()
public function setUp(): void
{
$this->transportA = $this->createMock('Gelf\Transport\TransportInterface');
$this->transportB = $this->createMock('Gelf\Transport\TransportInterface');
$this->transportA = $this->createMock(TransportInterface::class);
$this->transportB = $this->createMock(TransportInterface::class);
$this->messageValidator =
$this->createMock('Gelf\MessageValidatorInterface');
$this->message = $this->createMock('Gelf\MessageInterface');
$this->createMock(MessageValidatorInterface::class);
$this->message = $this->createMock(MessageInterface::class);

$this->publisher = new Publisher(
$this->transportA,
$this->messageValidator
);
}

public function testPublish()
public function testPublish(): void
{
$this->transportA->expects($this->once())
->method('send')
Expand All @@ -50,30 +55,26 @@ public function testPublish()
$this->publisher->publish($this->message);
}

/**
* @expectedException RuntimeException
*/
public function testPublishErrorOnInvalid()
public function testPublishErrorOnInvalid(): void
{
self::expectException(RuntimeException::class);
$this->messageValidator->expects($this->once())
->method('validate')
->will($this->returnValue(false));

$this->publisher->publish($this->message);
}

/**
* @expectedException RuntimeException
*/
public function testMissingTransport()
public function testMissingTransport(): void
{
self::expectException(RuntimeException::class);
$publisher = new Publisher(null, $this->messageValidator);
$this->assertCount(0, $publisher->getTransports());
self::assertCount(0, $publisher->getTransports());

$publisher->publish($this->message);
}

public function testMultipleTransports()
public function testMultipleTransports(): void
{
$pub = $this->publisher;
$pub->addTransport($this->transportB);
Expand All @@ -92,27 +93,18 @@ public function testMultipleTransports()
$pub->publish($this->message);
}

public function testGetTransports()
public function testGetTransports(): void
{
$pub = new Publisher(null, $this->messageValidator);
$this->assertCount(0, $pub->getTransports());
self::assertCount(0, $pub->getTransports());

$pub->addTransport($this->transportA);
$this->assertCount(1, $pub->getTransports());
self::assertCount(1, $pub->getTransports());

$pub->addTransport($this->transportB);
$this->assertCount(2, $pub->getTransports());
self::assertCount(2, $pub->getTransports());

$pub->addTransport($this->transportA);
$this->assertCount(2, $pub->getTransports());
}

public function testInitWithDefaultValidator()
{
$pub = new Publisher();
$this->assertInstanceOf(
'Gelf\MessageValidatorInterface',
$pub->getMessageValidator()
);
self::assertCount(2, $pub->getTransports());
}
}

0 comments on commit 5237435

Please sign in to comment.