Models: Added LogEntry model
parent
9d9aa71eb7
commit
ac48332166
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
use Engelsystem\Database\Migration\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
||||
class CreateLogEntriesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migration
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
$this->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');
|
||||
}
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Engelsystem\Database\DB;
|
||||
|
||||
/**
|
||||
* Creates a log entry.
|
||||
*
|
||||
* @param string $logLevel Log level
|
||||
* @param string $message Log Message
|
||||
* @return bool
|
||||
*/
|
||||
function LogEntry_create($logLevel, $message)
|
||||
{
|
||||
return DB::insert('
|
||||
INSERT INTO `LogEntries` (`timestamp`, `level`, `message`)
|
||||
VALUES(?, ?, ?)
|
||||
', [time(), $logLevel, $message]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns log entries with maximum count of 10000.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function LogEntries()
|
||||
{
|
||||
return DB::select('SELECT * FROM `LogEntries` ORDER BY `timestamp` DESC LIMIT 10000');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns log entries filtered by a keyword
|
||||
*
|
||||
* @param string $keyword
|
||||
* @return array
|
||||
*/
|
||||
function LogEntries_filter($keyword)
|
||||
{
|
||||
if ($keyword == '') {
|
||||
return LogEntries();
|
||||
}
|
||||
|
||||
$keyword = '%' . $keyword . '%';
|
||||
return DB::select('
|
||||
SELECT *
|
||||
FROM `LogEntries`
|
||||
WHERE `level` LIKE ?
|
||||
OR `message` LIKE ?
|
||||
ORDER BY `timestamp` DESC
|
||||
',
|
||||
[$keyword, $keyword]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all log entries.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function LogEntries_clear_all()
|
||||
{
|
||||
return DB::connection()->statement('TRUNCATE `LogEntries`');
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
abstract class BaseModel extends Model
|
||||
{
|
||||
/** @var bool Disable timestamps by default because of "Datensparsamkeit" */
|
||||
public $timestamps = false;
|
||||
|
||||
/**
|
||||
* @param array $attributes
|
||||
* @return BaseModel
|
||||
*/
|
||||
public function create(array $attributes = [])
|
||||
{
|
||||
$instance = new static($attributes);
|
||||
$instance->save();
|
||||
|
||||
return $instance;
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class LogEntry extends BaseModel
|
||||
{
|
||||
/** @var bool enable timestamps for created_at */
|
||||
public $timestamps = true;
|
||||
|
||||
/** @var null Disable updated_at */
|
||||
const UPDATED_AT = null;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'level',
|
||||
'message',
|
||||
];
|
||||
|
||||
/**
|
||||
* @param $keyword
|
||||
* @return Builder[]|Collection|LogEntry[]
|
||||
*/
|
||||
public static function filter($keyword = null)
|
||||
{
|
||||
$query = LogEntry::query()
|
||||
->select()
|
||||
->orderByDesc('created_at')
|
||||
->orderByDesc('id')
|
||||
->limit(10000);
|
||||
|
||||
if (!empty($keyword)) {
|
||||
$query
|
||||
->where('level', '=', $keyword)
|
||||
->orWhere('message', 'LIKE', '%' . $keyword . '%');
|
||||
}
|
||||
|
||||
return $query->get();
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Feature\Model;
|
||||
|
||||
use Engelsystem\Test\Feature\ApplicationFeatureTest;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
class LogEntriesModelTest extends ApplicationFeatureTest
|
||||
{
|
||||
public function testCreateLogEntry()
|
||||
{
|
||||
LogEntries_clear_all();
|
||||
$count = count(LogEntries());
|
||||
$this->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();
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Feature\Model;
|
||||
|
||||
use Engelsystem\Models\LogEntry;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
class LogEntryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Models\LogEntry::filter
|
||||
*/
|
||||
public function testFilter()
|
||||
{
|
||||
foreach ([
|
||||
'Lorem Ipsum' => 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();
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Models;
|
||||
|
||||
use Engelsystem\Test\Unit\Models\Stub\BaseModelImplementation;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class BaseModelTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Models\BaseModel::create
|
||||
*/
|
||||
public function testCreate()
|
||||
{
|
||||
$model = new BaseModelImplementation();
|
||||
$newModel = $model->create(['foo' => 'bar']);
|
||||
|
||||
$this->assertNotEquals($model, $newModel);
|
||||
$this->assertEquals('bar', $newModel->foo);
|
||||
$this->assertEquals(1, $newModel->saveCount);
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Models\Stub;
|
||||
|
||||
use Engelsystem\Models\BaseModel;
|
||||
|
||||
/**
|
||||
* @property string foo
|
||||
*/
|
||||
class BaseModelImplementation extends BaseModel
|
||||
{
|
||||
/** @var array */
|
||||
protected $fillable = ['foo'];
|
||||
|
||||
/** @var int */
|
||||
public $saveCount = 0;
|
||||
|
||||
/**
|
||||
* @param array $options
|
||||
* @return bool
|
||||
*/
|
||||
public function save(array $options = [])
|
||||
{
|
||||
$this->saveCount++;
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue