Skip to content

Commit

Permalink
Finalizes Logging and prepares notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
moay committed Aug 25, 2019
1 parent 7932d1f commit f42bdb7
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 188 deletions.
28 changes: 28 additions & 0 deletions src/Communication/Notifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace LoxBerry\Communication;

/**
* Class Notifications.
*/
class Notifications
{
const RETRIEVE_ALL = 'all';
const RETRIEVE_ERRORS = 'error';
const RETRIEVE_INFO = 'info';

const SEVERITY_LEVEL_INFO = 6;
const SEVERITY_LEVEL_ERROR = 3;

public function get(): array
{
}

public function getHtml(string $type = 'all'): string
{
}

public function notify(): bool
{
}
}
29 changes: 0 additions & 29 deletions src/ConfigurationParser/PluginConfigurationParser.php

This file was deleted.

16 changes: 15 additions & 1 deletion src/Logging/LoggerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function __construct(
$this->pathProvider = $pathProvider;
$this->lowLevelExecutor = $lowLevelExecutor;
$this->systemConfiguration = $systemConfiguration;
$this->database = $this->databaseFactory->create();
}

/**
Expand All @@ -70,6 +71,7 @@ public function create(
throw new \InvalidArgumentException('Cannot enable file writing without logFile');
}
$eventLogger->setFileWriter(new LogFileWriter($fileName));
$this->database->logStart($packageName, $logName, $fileName);
}
if ($writeToStdOut || $writeToStdErr) {
$eventLogger->setSystemWriter(new LogSystemWriter($this->lowLevelExecutor));
Expand All @@ -82,6 +84,12 @@ public function create(
new AttributeLogger($this->databaseFactory->create())
);

$logger->setWriteToStdErr($writeToStdErr);
$logger->setWriteToStdOut($writeToStdOut);
$logger->setWriteToFile($writeToFile);

$logger->info('LoxBerry Version '.$this->systemConfiguration->getLoxBerryVersion());

return $logger;
}

Expand All @@ -92,6 +100,12 @@ public function create(
*/
public function createFromExistingLogSession(string $logKey): Logger
{
// Todo: Test & implement, should also set all params from existing session if exists.
$session = $this->database->getUnclosedLogSessionByKey($logKey);
if (null !== $session) {
$stderr = $this->database->getAttribute($logKey, 'stderr') ?? false;
$stdout = $this->database->getAttribute($logKey, 'stdout') ?? false;

return $this->create($session['NAME'], $session['PACKAGE'], $session['FILENAME'], true, $stderr, $stdout);
}
}
}
52 changes: 52 additions & 0 deletions src/Utility/HttpAccess/AbstractHttpAccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace LoxBerry\Utility;

use LoxBerry\ConfigurationParser\SystemConfigurationParser;

/**
* Class AbstractHttpAccess.
*/
abstract class AbstractHttpAccess
{
/** @var SystemConfigurationParser */
protected $systemConfiguration;

/**
* HttpAccess constructor.
*
* @param SystemConfigurationParser $systemConfiguration
*/
public function __construct(SystemConfigurationParser $systemConfiguration)
{
$this->systemConfiguration = $systemConfiguration;
}

abstract protected function getEndpointUrl(): string;

/**
* @param array $data
*
* @return false|string
*/
protected function post(array $data)
{
$context = stream_context_create([
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
],
]);

return file_get_contents($this->getEndpointUrl(), false, $context);
}

/**
* @return string
*/
protected function getBaseUrl(): string
{
return 'http://localhost:'.($this->systemConfiguration->getWebserverPort() ?? 80);
}
}
39 changes: 39 additions & 0 deletions src/Utility/HttpAccess/AjaxNotificationHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace LoxBerry\Utility\HttpAccess;

use LoxBerry\Utility\AbstractHttpAccess;

/**
* Class AjaxNotificationHandler.
*/
class AjaxNotificationHandler extends AbstractHttpAccess
{
const ENDPOINT_URL = '/admin/system/tools/ajax-notification-handler.cgi';

/**
* @param array $data
*
* @return string
*/
public function getAsString(array $data): string
{
}

/**
* @param array $data
*
* @return array
*/
public function getAsArray(array $data): array
{
}

/**
* @return string
*/
protected function getEndpointUrl(): string
{
return $this->getBaseUrl().self::ENDPOINT_URL;
}
}
31 changes: 31 additions & 0 deletions tests/Communication/NotificationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace LoxBerry\Tests\Communication;

use PHPUnit\Framework\TestCase;

/**
* Class NotificationsTest.
*/
class NotificationsTest extends TestCase
{
public function testRetrievesNotificationsProperly()
{
$this->markTestIncomplete();
}

public function testRetrievesNotificationsHtmlProperly()
{
$this->markTestIncomplete();
}

public function testPushesSimpleNotification()
{
$this->markTestIncomplete();
}

public function teestPushesExtendedNotification()
{
$this->markTestIncomplete();
}
}
16 changes: 0 additions & 16 deletions tests/ConfigurationParser/PluginConfigurationParserTest.php

This file was deleted.

43 changes: 28 additions & 15 deletions tests/Logging/LoggerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace LoxBerry\Tests\Logging;

use LoxBerry\ConfigurationParser\SystemConfigurationParser;
use LoxBerry\Logging\Database\LogFileDatabase;
use LoxBerry\Logging\Database\LogFileDatabaseFactory;
use LoxBerry\Logging\Logger;
use LoxBerry\Logging\LoggerFactory;
Expand Down Expand Up @@ -42,6 +43,10 @@ public function testLogStartHeadersAreProperlyLogged()
$pathProviderMock = $this->createMock(PathProvider::class);
$systemConfigurationMock = $this->createMock(SystemConfigurationParser::class);

$systemConfigurationMock->expects($this->once())
->method('getLoxBerryVersion')
->willReturn('1.5.0');

$loggerFactory = new LoggerFactory(
$databaseFactoryMock,
$lowLevelMock,
Expand All @@ -50,26 +55,34 @@ public function testLogStartHeadersAreProperlyLogged()
);

$logger = $loggerFactory->create('eventLog', 'testPlugin', __DIR__.DIRECTORY_SEPARATOR.self::TEST_FILE);
$this->markTestIncomplete();
// Logs LB version
// Logs Package/Plugin title and version
// Logs LogLevel
$loggedStuff = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.self::TEST_FILE);
$this->assertStringContainsString('1.5.0', $loggedStuff);
}

public function testLogStartDatabaseQueriesAreProperlyExecutedIfFileWritingIsEnabled()
{
$this->markTestIncomplete();
// Test log to db if writing to file
// Test logs sessino info to db including filename and package info
// Test logs params [append, loglevel, loglevel_is_static, logdir, LOGSTARTBYTE, _ISPLUGIN, PLUGINTITLE, stdout, stderr]
}
$databaseFactoryMock = $this->createMock(LogFileDatabaseFactory::class);
$lowLevelMock = $this->createMock(LowLevelExecutor::class);
$pathProviderMock = $this->createMock(PathProvider::class);
$systemConfigurationMock = $this->createMock(SystemConfigurationParser::class);
$databaseMock = $this->createMock(LogFileDatabase::class);

public function testLogEndDatabaseQueriesAreProperlyExecutedIfFileWritingIsEnabled()
{
$this->markTestIncomplete();
// Test log to db if writing to file
// Test logs session info to db including filename and package info
// Test logs params [append, loglevel, loglevel_is_static, logdir, LOGSTARTBYTE, _ISPLUGIN, PLUGINTITLE, stdout, stderr, ATTENTIONMESSAGES, STATUS]
$databaseMock->expects($this->once())
->method('logStart')
->with('testPlugin', 'eventLog', __DIR__.DIRECTORY_SEPARATOR.self::TEST_FILE);

$databaseFactoryMock
->method('create')
->willReturn($databaseMock);

$loggerFactory = new LoggerFactory(
$databaseFactoryMock,
$lowLevelMock,
$pathProviderMock,
$systemConfigurationMock
);

$logger = $loggerFactory->create('eventLog', 'testPlugin', __DIR__.DIRECTORY_SEPARATOR.self::TEST_FILE);
}

protected function setUp(): void
Expand Down
21 changes: 21 additions & 0 deletions tests/System/Utility/AjaxNotificationHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace LoxBerry\Tests\System\Utility;

use PHPUnit\Framework\TestCase;

/**
* Class AjaxNotificationHandlerTest.
*/
class AjaxNotificationHandlerTest extends TestCase
{
public function testGetsDataAsString()
{
$this->markTestIncomplete();
}

public function testGetsDataAsArrayExpectingJson()
{
$this->markTestIncomplete();
}
}
Loading

0 comments on commit f42bdb7

Please sign in to comment.