diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2018-08-31 01:55:05 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2018-08-31 03:24:54 +0200 |
commit | ac48332166ce28fcb1a2fc130c7f5adbc760e42d (patch) | |
tree | 1ff076a6a1c5a35b4ad73477d60cbcb77ebfd922 /tests | |
parent | 9d9aa71eb736885f23c069a75c7a047b06434178 (diff) |
Models: Added LogEntry model
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Feature/Logger/EngelsystemLoggerTest.php | 21 | ||||
-rw-r--r-- | tests/Feature/Model/LogEntriesModelTest.php | 33 | ||||
-rw-r--r-- | tests/Feature/Model/LogEntryTest.php | 56 | ||||
-rw-r--r-- | tests/Unit/Logger/LoggerServiceProviderTest.php | 1 | ||||
-rw-r--r-- | tests/Unit/Models/BaseModelTest.php | 22 | ||||
-rw-r--r-- | tests/Unit/Models/Stub/BaseModelImplementation.php | 27 |
6 files changed, 117 insertions, 43 deletions
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 @@ -<?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(); - } -} 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 @@ +<?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(); + } +} 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 @@ +<?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); + } +} 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 @@ +<?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; + } +} |