From ac48332166ce28fcb1a2fc130c7f5adbc760e42d Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Fri, 31 Aug 2018 01:55:05 +0200 Subject: [PATCH] Models: Added LogEntry model --- ..._08_30_000000_create_log_entries_table.php | 47 ++++++++++++++ includes/includes.php | 1 - includes/model/LogEntries_model.php | 62 ------------------- includes/pages/admin_log.php | 20 +++--- src/Logger/EngelsystemLogger.php | 13 +++- src/Models/BaseModel.php | 23 +++++++ src/Models/LogEntry.php | 46 ++++++++++++++ .../Feature/Logger/EngelsystemLoggerTest.php | 21 ++++--- tests/Feature/Model/LogEntriesModelTest.php | 33 ---------- tests/Feature/Model/LogEntryTest.php | 56 +++++++++++++++++ .../Unit/Logger/LoggerServiceProviderTest.php | 1 + tests/Unit/Models/BaseModelTest.php | 22 +++++++ .../Models/Stub/BaseModelImplementation.php | 27 ++++++++ 13 files changed, 257 insertions(+), 115 deletions(-) create mode 100644 db/migrations/2018_08_30_000000_create_log_entries_table.php delete mode 100644 includes/model/LogEntries_model.php create mode 100644 src/Models/BaseModel.php create mode 100644 src/Models/LogEntry.php delete mode 100644 tests/Feature/Model/LogEntriesModelTest.php create mode 100644 tests/Feature/Model/LogEntryTest.php create mode 100644 tests/Unit/Models/BaseModelTest.php create mode 100644 tests/Unit/Models/Stub/BaseModelImplementation.php diff --git a/db/migrations/2018_08_30_000000_create_log_entries_table.php b/db/migrations/2018_08_30_000000_create_log_entries_table.php new file mode 100644 index 00000000..68815434 --- /dev/null +++ b/db/migrations/2018_08_30_000000_create_log_entries_table.php @@ -0,0 +1,47 @@ +schema->create('log_entries', function (Blueprint $table) { + $table->increments('id'); + $table->string('level', 20); + $table->text('message'); + $table->timestamp('created_at')->nullable(); + }); + + $this->schema->getConnection()->unprepared(' + INSERT INTO log_entries (`id`, `level`, `message`, `created_at`) + SELECT `id`, `level`, `message`, FROM_UNIXTIME(`timestamp`) FROM LogEntries + '); + + $this->schema->dropIfExists('LogEntries'); + } + + /** + * Reverse the migration + */ + public function down() + { + $this->schema->create('LogEntries', function (Blueprint $table) { + $table->increments('id'); + $table->string('level', 20); + $table->text('message'); + $table->integer('timestamp'); + }); + + $this->schema->getConnection()->unprepared(' + INSERT INTO LogEntries (`id`, `level`, `message`, `timestamp`) + SELECT `id`, `level`, `message`, UNIX_TIMESTAMP(`created_at`) FROM log_entries + '); + + $this->schema->dropIfExists('log_entries'); + } +} diff --git a/includes/includes.php b/includes/includes.php index 7a68c3c9..628cf88e 100644 --- a/includes/includes.php +++ b/includes/includes.php @@ -13,7 +13,6 @@ $includeFiles = [ __DIR__ . '/../includes/model/AngelType_model.php', __DIR__ . '/../includes/model/EventConfig_model.php', - __DIR__ . '/../includes/model/LogEntries_model.php', __DIR__ . '/../includes/model/Message_model.php', __DIR__ . '/../includes/model/NeededAngelTypes_model.php', __DIR__ . '/../includes/model/Room_model.php', diff --git a/includes/model/LogEntries_model.php b/includes/model/LogEntries_model.php deleted file mode 100644 index b16c598c..00000000 --- a/includes/model/LogEntries_model.php +++ /dev/null @@ -1,62 +0,0 @@ -statement('TRUNCATE `LogEntries`'); -} diff --git a/includes/pages/admin_log.php b/includes/pages/admin_log.php index f80f59d2..2a736aa5 100644 --- a/includes/pages/admin_log.php +++ b/includes/pages/admin_log.php @@ -1,5 +1,7 @@ has('keyword')) { $filter = strip_request_item('keyword'); } - $log_entries = LogEntries_filter($filter); - foreach ($log_entries as &$log_entry) { - $log_entry['date'] = date('d.m.Y H:i', $log_entry['timestamp']); + $log_entries = LogEntry::filter($filter); + + $entries = []; + foreach ($log_entries as $entry) { + $data = $entry->toArray(); + $data['created_at'] = date_format($entry->created_at, 'd.m.Y H:i'); + $entries[] = $data; } return page_with_title(admin_log_title(), [ @@ -30,9 +36,9 @@ function admin_log() form_submit(__('Search'), 'Go') ]), table([ - 'date' => 'Time', - 'level' => 'Type', - 'message' => 'Log Entry' - ], $log_entries) + 'created_at' => 'Time', + 'level' => 'Type', + 'message' => 'Log Entry' + ], $entries) ]); } diff --git a/src/Logger/EngelsystemLogger.php b/src/Logger/EngelsystemLogger.php index 1f255b69..aab8026a 100644 --- a/src/Logger/EngelsystemLogger.php +++ b/src/Logger/EngelsystemLogger.php @@ -2,6 +2,7 @@ namespace Engelsystem\Logger; +use Engelsystem\Models\LogEntry; use Psr\Log\AbstractLogger; use Psr\Log\InvalidArgumentException; use Psr\Log\LogLevel; @@ -19,6 +20,14 @@ class EngelsystemLogger extends AbstractLogger LogLevel::WARNING, ]; + /** @var LogEntry */ + protected $log; + + public function __construct(LogEntry $log) + { + $this->log = $log; + } + /** * Logs with an arbitrary level. * @@ -33,12 +42,12 @@ class EngelsystemLogger extends AbstractLogger public function log($level, $message, array $context = []) { if (!$this->checkLevel($level)) { - throw new InvalidArgumentException(); + throw new InvalidArgumentException('Unknown log level: ' . $level); } $message = $this->interpolate($message, $context); - LogEntry_create($level, $message); + $this->log->create(['level' => $level, 'message' => $message]); } /** diff --git a/src/Models/BaseModel.php b/src/Models/BaseModel.php new file mode 100644 index 00000000..cf718e4f --- /dev/null +++ b/src/Models/BaseModel.php @@ -0,0 +1,23 @@ +save(); + + return $instance; + } +} diff --git a/src/Models/LogEntry.php b/src/Models/LogEntry.php new file mode 100644 index 00000000..ca9702de --- /dev/null +++ b/src/Models/LogEntry.php @@ -0,0 +1,46 @@ +select() + ->orderByDesc('created_at') + ->orderByDesc('id') + ->limit(10000); + + if (!empty($keyword)) { + $query + ->where('level', '=', $keyword) + ->orWhere('message', 'LIKE', '%' . $keyword . '%'); + } + + return $query->get(); + } +} diff --git a/tests/Feature/Logger/EngelsystemLoggerTest.php b/tests/Feature/Logger/EngelsystemLoggerTest.php index ce7b8ac5..9ec078a3 100644 --- a/tests/Feature/Logger/EngelsystemLoggerTest.php +++ b/tests/Feature/Logger/EngelsystemLoggerTest.php @@ -3,6 +3,7 @@ namespace Engelsystem\Test\Feature\Logger; use Engelsystem\Logger\EngelsystemLogger; +use Engelsystem\Models\LogEntry; use Engelsystem\Test\Feature\ApplicationFeatureTest; use Psr\Log\InvalidArgumentException; use Psr\Log\LoggerInterface; @@ -15,12 +16,13 @@ class EngelsystemLoggerTest extends ApplicationFeatureTest */ public function getLogger() { - return new EngelsystemLogger(); + $logEntry = new LogEntry(); + return new EngelsystemLogger($logEntry); } public function testImplements() { - $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger()); + $this->assertInstanceOf(LoggerInterface::class, $this->getLogger()); } /** @@ -46,21 +48,20 @@ class EngelsystemLoggerTest extends ApplicationFeatureTest */ public function testAllLevels($level) { + LogEntry::query()->truncate(); $logger = $this->getLogger(); - LogEntries_clear_all(); - $logger->log($level, 'First log message'); $logger->{$level}('Second log message'); - $entries = LogEntries(); + $entries = LogEntry::all(); $this->assertCount(2, $entries); } public function testContextReplacement() { + LogEntry::query()->truncate(); $logger = $this->getLogger(); - LogEntries_clear_all(); $logger->log(LogLevel::INFO, 'My username is {username}', ['username' => 'Foo']); @@ -100,8 +101,8 @@ class EngelsystemLoggerTest extends ApplicationFeatureTest public function testContextToString() { + LogEntry::query()->truncate(); $logger = $this->getLogger(); - LogEntries_clear_all(); $mock = $this->getMockBuilder('someDataProvider') ->setMethods(['__toString']) @@ -132,14 +133,14 @@ class EngelsystemLoggerTest extends ApplicationFeatureTest */ public function getLastEntry() { - $entries = LogEntries(); - $entry = array_pop($entries); + $entries = LogEntry::all(); + $entry = $entries->last(); return $entry; } public function tearDown() { - LogEntries_clear_all(); + LogEntry::query()->truncate(); } } diff --git a/tests/Feature/Model/LogEntriesModelTest.php b/tests/Feature/Model/LogEntriesModelTest.php deleted file mode 100644 index c032a94c..00000000 --- a/tests/Feature/Model/LogEntriesModelTest.php +++ /dev/null @@ -1,33 +0,0 @@ -assertNotFalse(LogEntry_create(LogLevel::WARNING, 'test_LogEntry_create')); - - // There should be one more log entry now - $this->assertEquals(count(LogEntries()), $count + 1); - } - - public function testClearAllLogEntries() - { - LogEntry_create(LogLevel::WARNING, 'test'); - $this->assertTrue(count(LogEntries()) > 0); - - $this->assertNotFalse(LogEntries_clear_all()); - $this->assertCount(0, LogEntries()); - } - - public function tearDown() - { - LogEntries_clear_all(); - } -} diff --git a/tests/Feature/Model/LogEntryTest.php b/tests/Feature/Model/LogEntryTest.php new file mode 100644 index 00000000..25b35676 --- /dev/null +++ b/tests/Feature/Model/LogEntryTest.php @@ -0,0 +1,56 @@ + LogLevel::INFO, + 'Some test content' => LogLevel::ERROR, + 'Foo bar bartz!' => LogLevel::INFO, + 'Someone did something?' => LogLevel::NOTICE, + 'This is a Test!' => LogLevel::INFO, + 'I\'m verbose notice!' => LogLevel::DEBUG, + 'The newest stuff!!' => LogLevel::ERROR, + ] as $message => $level) { + $entry = new LogEntry(['level' => $level, 'message' => $message]); + $entry->save(); + } + + $model = new LogEntry(); + + $return = $model->filter(); + $this->assertCount(7, $return); + + /** @var LogEntry $first */ + $first = $return->first(); + + $this->assertEquals('The newest stuff!!', $first->message); + + $return = $model->filter(LogLevel::INFO); + $this->assertCount(3, $return); + + $return = $model->filter('notice'); + $this->assertCount(2, $return); + + $return = $model->filter('bartz'); + $this->assertCount(1, $return); + } + + /** + * This method is called before a test is executed. + */ + public function setUp() + { + LogEntry::query()->truncate(); + } +} diff --git a/tests/Unit/Logger/LoggerServiceProviderTest.php b/tests/Unit/Logger/LoggerServiceProviderTest.php index cef95d5b..aef0b7d1 100644 --- a/tests/Unit/Logger/LoggerServiceProviderTest.php +++ b/tests/Unit/Logger/LoggerServiceProviderTest.php @@ -17,6 +17,7 @@ class LoggerServiceProviderTest extends ServiceProviderTest { /** @var PHPUnit_Framework_MockObject_MockObject|EngelsystemLogger $logger */ $logger = $this->getMockBuilder(EngelsystemLogger::class) + ->disableOriginalConstructor() ->getMock(); $app = $this->getApp(['make', 'instance', 'bind']); diff --git a/tests/Unit/Models/BaseModelTest.php b/tests/Unit/Models/BaseModelTest.php new file mode 100644 index 00000000..52cb8c7b --- /dev/null +++ b/tests/Unit/Models/BaseModelTest.php @@ -0,0 +1,22 @@ +create(['foo' => 'bar']); + + $this->assertNotEquals($model, $newModel); + $this->assertEquals('bar', $newModel->foo); + $this->assertEquals(1, $newModel->saveCount); + } +} diff --git a/tests/Unit/Models/Stub/BaseModelImplementation.php b/tests/Unit/Models/Stub/BaseModelImplementation.php new file mode 100644 index 00000000..4aa1ef0b --- /dev/null +++ b/tests/Unit/Models/Stub/BaseModelImplementation.php @@ -0,0 +1,27 @@ +saveCount++; + return true; + } +}