Skip to content

Commit

Permalink
Issue #60: Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ademarco committed Oct 20, 2017
1 parent b04bfc6 commit c93d4d8
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 14 deletions.
30 changes: 30 additions & 0 deletions tests/src/Logger/TestFileLoggerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace EC\Poetry\Tests\Logger;

use Monolog\Formatter\JsonFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

/**
* Class TestFileLoggerFactory
*
* @package EC\Poetry\Tests\Logger
*/
class TestFileLoggerFactory
{
/**
* @param string $file
*
* @return \Monolog\Logger
*/
public static function getInstance($file)
{
@unlink($file);
$formatter = new JsonFormatter();
$stream = new StreamHandler($file);
$stream->setFormatter($formatter);

return new Logger('Test File Logger', [$stream]);
}
}
74 changes: 60 additions & 14 deletions tests/src/NotificationHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
namespace EC\Poetry\Tests;

use EC\Poetry\Events\Notifications\StatusUpdatedEvent;
use EC\Poetry\Messages\Components\Identifier;
use EC\Poetry\Messages\Notifications\StatusUpdated;
use EC\Poetry\Messages\Responses\Status;
use EC\Poetry\Poetry;
use EC\Poetry\Events\NotificationEventInterface;
use EC\Poetry\Messages\Notifications\TranslationReceived;
use EC\Poetry\Server;
use Monolog\Formatter\JsonFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use EC\Poetry\Tests\Logger\TestFileLoggerFactory;
use Psr\Log\LogLevel;
use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -41,7 +37,6 @@ public function testFunctionCallback()
$poetry = new Poetry([
'notification.username' => 'username',
'notification.password' => 'password',
'log_level' => LogLevel::INFO,
]);
$poetry->getEventDispatcher()->addListener(StatusUpdatedEvent::NAME, function (StatusUpdatedEvent $event) {
expect($event->hasMessage())->be->true();
Expand Down Expand Up @@ -82,25 +77,50 @@ public function testFunctionCallback()
expect($status->getStatuses()[0]->getCode())->to->be->equal('-1');
}

/**
* Test response event.
*
* @param string $level
* @param array $messages
*
* @dataProvider loggingCycleDataProvider
*/
public function testLoggingCycle($level, array $messages)
{
$file = $this->logFile;
$callback = function (Response $response) use ($file, $level) {
$poetry = new Poetry([
'notification.username' => 'username',
'notification.password' => 'password',
'logger' => TestFileLoggerFactory::getInstance($file),
'log_level' => $level,
]);
$poetry->getServer()->handle();
};

$this->setupServer('/notification', $callback);

$message = $this->getFixture('messages/notifications/status-updated.xml');
$this->notifyServer('/notification', 'username', 'password', $message);

$logs = $this->getLogs();
foreach ($messages as $key => $expected) {
expect($logs[$key]->message)->to->equal($expected);
}
}

/**
* Test bad request.
*/
public function testBadRequest()
{
$file = $this->logFile;
$callback = function (Response $response) use ($file) {
@unlink($file);
$formatter = new JsonFormatter();
$stream = new StreamHandler($file, Logger::INFO);
$stream->setFormatter($formatter);
$logger = new Logger('Test Logger');
$logger->pushHandler($stream);

$poetry = new Poetry([
'notification.username' => 'username',
'notification.password' => 'password',
'exceptions' => false,
'logger' => $logger,
'logger' => TestFileLoggerFactory::getInstance($file),
'log_level' => LogLevel::INFO,
]);
$poetry->getServer()->handle();
Expand All @@ -125,4 +145,30 @@ public static function onTranslationReceived(NotificationEventInterface $event)
expect($event->hasMessage())->be->true();
expect($event->getMessage())->to->be->instanceof(TranslationReceived::class);
}

/**
* @return array
*/
public function loggingCycleDataProvider()
{
return [
[
LogLevel::DEBUG,
[
'poetry.notification_handler.received_notification',
'poetry.notification.parse',
'poetry.notification.status_updated',
'poetry.notification_handler.sent_notification_response',
],
],
[
LogLevel::INFO,
[
'poetry.notification_handler.received_notification',
'poetry.notification.status_updated',
'poetry.notification_handler.sent_notification_response',
],
],
];
}
}

0 comments on commit c93d4d8

Please sign in to comment.