Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: registerEvent fixes #12

Open
wants to merge 1 commit into
base: unit-test-LogsHttpLogger
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions src/Logger/LogsHttpLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,9 @@ public function log($level, $message, array $context = array()) {
}

/**
* Register an event in the cache.
*
* To prevent multiple registration of the same error, we check that identical
* events are not captured twice, thus reducing the final HTTP requests needed.
*
* @param $level
* The severity level.
* @param message
* The message that contains the placeholders.
* @param array $context
* The context as passed from the main Logger.
* {@inheritdoc}
*/
public function registerEvent($level, $message, array $context = []) {
public function registerEvent($level, $message, array $context) {
if (!$this->isEnabled()) {
return;
}
Expand Down Expand Up @@ -183,4 +173,8 @@ public function isEnabled() {
public function getUrl() {
return $this->config->get('url');
}

public function getCache() {
return $this->cache;
}
}
10 changes: 8 additions & 2 deletions src/Logger/LogsHttpLoggerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@
public function reset();

/**
* Register an event.
* Register an event in the cache.
*
* To prevent multiple registration of the same error, we check that identical
* events are not captured twice, thus reducing the final HTTP requests needed.
*
* @param $level
* The severity level.
* @param message
* The message that contains the placeholders.
* @param array $context
* The context as passed from the main Logger.
*/
public function registerEvent($level, $message, array $context = []);
public function registerEvent($level, $message, array $context);

/**
* A getter for the current events.
Expand Down
40 changes: 40 additions & 0 deletions tests/src/Unit/LogsHttpLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ class LogsHttpLoggerTest extends UnitTestCase {
*/
protected $severityLevels;

/**
* The message to log.
*
* @var string
*/
protected $message;

/**
* The context array with the log data.
*
* @var array
*/
protected $context;


/**
* {@inheritdoc}
Expand All @@ -45,6 +59,10 @@ public function setUp() {
$this->config = $this->prophesize(ConfigFactoryInterface::class);
$this->logMessageParser = $this->prophesize(LogMessageParserInterface::class);
$this->severityLevels = RfcLogLevel::getLevels();
$this->message = $this->randomMachineName();
$this->context = [
'timestamp' => time(),
];

$this
->config
Expand Down Expand Up @@ -76,6 +94,28 @@ public function testIsEnabled($enabled, $url, $expected) {
$this->assertEquals($expected, $result);
}

/**
* Test register event when setting is disabled.
*
* @covers ::registerEvent
*/
public function testRegisterEventDisabled() {
$this
->config
->get('enabled')
->willReturn(FALSE);

$this
->config
->get('url')
->willReturn($this->randomMachineName());

$logger = new LogsHttpLogger($this->config->reveal(), $this->logMessageParser->reveal());
$logger->registerEvent(RfcLogLevel::CRITICAL, $this->message, $this->context);

$this->assertEmpty($logger->getCache());
}


/**
* Provides test data to test isEnabled.
Expand Down