From 9a2f2465116387880282df505e3d3230516604dd Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 18 Dec 2018 04:43:13 +0100 Subject: metrics: Added more stats --- tests/Unit/Controllers/Metrics/ControllerTest.php | 40 +++++++++++++++ tests/Unit/Controllers/Metrics/StatsTest.php | 60 +++++++++++++++++++++++ 2 files changed, 100 insertions(+) (limited to 'tests/Unit') diff --git a/tests/Unit/Controllers/Metrics/ControllerTest.php b/tests/Unit/Controllers/Metrics/ControllerTest.php index 013a3352..37310ddf 100644 --- a/tests/Unit/Controllers/Metrics/ControllerTest.php +++ b/tests/Unit/Controllers/Metrics/ControllerTest.php @@ -11,6 +11,7 @@ use Engelsystem\Http\Request; use Engelsystem\Http\Response; use Engelsystem\Test\Unit\TestCase; use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LogLevel; use Symfony\Component\HttpFoundation\ServerBag; class ControllerTest extends TestCase @@ -36,9 +37,18 @@ class ControllerTest extends TestCase ->willReturnCallback(function ($path, $data) use ($response) { $this->assertEquals('/metrics', $path); $this->assertArrayHasKey('users', $data); + $this->assertArrayHasKey('licenses', $data); $this->assertArrayHasKey('users_working', $data); $this->assertArrayHasKey('work_seconds', $data); + $this->assertArrayHasKey('worklog_seconds', $data); + $this->assertArrayHasKey('shifts', $data); + $this->assertArrayHasKey('announcements', $data); + $this->assertArrayHasKey('questions', $data); + $this->assertArrayHasKey('messages', $data); + $this->assertArrayHasKey('password_resets', $data); $this->assertArrayHasKey('registration_enabled', $data); + $this->assertArrayHasKey('sessions', $data); + $this->assertArrayHasKey('log_entries', $data); $this->assertArrayHasKey('scrape_duration_seconds', $data); return 'metrics return'; @@ -53,6 +63,10 @@ class ControllerTest extends TestCase ->with('metrics return') ->willReturn($response); + $stats->expects($this->exactly(5)) + ->method('licenses') + ->withConsecutive(['forklift'], ['car'], ['3.5t'], ['7.5t'], ['12.5t']) + ->willReturnOnConsecutiveCalls(3, 15, 9, 7, 1); $stats->expects($this->exactly(2)) ->method('arrivedUsers') ->withConsecutive([false], [true]) @@ -65,7 +79,33 @@ class ControllerTest extends TestCase ->method('workSeconds') ->withConsecutive([true, false], [false, false], [null, true]) ->willReturnOnConsecutiveCalls(60 * 37, 60 * 251, 60 * 3); + $stats->expects($this->exactly(2)) + ->method('announcements') + ->withConsecutive([false], [true]) + ->willReturnOnConsecutiveCalls(18, 7); + $stats->expects($this->exactly(2)) + ->method('questions') + ->withConsecutive([true], [false]) + ->willReturnOnConsecutiveCalls(5, 0); + $stats->expects($this->exactly(8)) + ->method('logEntries') + ->withConsecutive( + [LogLevel::EMERGENCY], + [LogLevel::ALERT], + [LogLevel::CRITICAL], + [LogLevel::ERROR], + [LogLevel::WARNING], + [LogLevel::NOTICE], + [LogLevel::INFO], + [LogLevel::DEBUG] + ) + ->willReturnOnConsecutiveCalls(0, 1, 0, 5, 999, 4, 55, 3); $this->setExpects($stats, 'newUsers', null, 9); + $this->setExpects($stats, 'worklogSeconds', null, 39 * 60 * 60); + $this->setExpects($stats, 'shifts', null, 142); + $this->setExpects($stats, 'messages', null, 3); + $this->setExpects($stats, 'passwordResets', null, 1); + $this->setExpects($stats, 'sessions', null, 1234); $config->set('registration_enabled', 1); diff --git a/tests/Unit/Controllers/Metrics/StatsTest.php b/tests/Unit/Controllers/Metrics/StatsTest.php index 1618b99b..2ebe52ae 100644 --- a/tests/Unit/Controllers/Metrics/StatsTest.php +++ b/tests/Unit/Controllers/Metrics/StatsTest.php @@ -2,12 +2,16 @@ namespace Engelsystem\Test\Unit\Controllers\Metrics; +use Carbon\Carbon; use Engelsystem\Controllers\Metrics\Stats; +use Engelsystem\Models\LogEntry; +use Engelsystem\Models\User\PasswordReset; use Engelsystem\Models\User\State; use Engelsystem\Models\User\User; use Engelsystem\Test\Unit\HasDatabase; use Engelsystem\Test\Unit\TestCase; use Illuminate\Support\Str; +use Psr\Log\LogLevel; class StatsTest extends TestCase { @@ -39,6 +43,62 @@ class StatsTest extends TestCase $this->assertEquals(3, $stats->arrivedUsers()); } + /** + * @covers \Engelsystem\Controllers\Metrics\Stats::sessions + */ + public function testSessions() + { + $this->initDatabase(); + + $this->database + ->getConnection() + ->table('sessions') + ->insert([ + ['id' => 'asd', 'payload' => 'data', 'last_activity' => new Carbon('1 month ago')], + ['id' => 'efg', 'payload' => 'lorem', 'last_activity' => new Carbon('55 minutes ago')], + ['id' => 'hij', 'payload' => 'ipsum', 'last_activity' => new Carbon('3 seconds ago')], + ['id' => 'klm', 'payload' => 'dolor', 'last_activity' => new Carbon()], + ]); + + $stats = new Stats($this->database); + $this->assertEquals(4, $stats->sessions()); + } + + /** + * @covers \Engelsystem\Controllers\Metrics\Stats::logEntries + */ + public function testLogEntries() + { + $this->initDatabase(); + + (new LogEntry(['level' => LogLevel::INFO, 'message' => 'Some info']))->save(); + (new LogEntry(['level' => LogLevel::INFO, 'message' => 'Another info']))->save(); + (new LogEntry(['level' => LogLevel::CRITICAL, 'message' => 'A critical error!']))->save(); + (new LogEntry(['level' => LogLevel::DEBUG, 'message' => 'Verbose output!']))->save(); + (new LogEntry(['level' => LogLevel::INFO, 'message' => 'Shutdown initiated']))->save(); + (new LogEntry(['level' => LogLevel::WARNING, 'message' => 'Please be cautious']))->save(); + + $stats = new Stats($this->database); + $this->assertEquals(6, $stats->logEntries()); + $this->assertEquals(3, $stats->logEntries(LogLevel::INFO)); + $this->assertEquals(1, $stats->logEntries(LogLevel::DEBUG)); + } + + /** + * @covers \Engelsystem\Controllers\Metrics\Stats::passwordResets + */ + public function testPasswordResets() + { + $this->initDatabase(); + $this->addUsers(); + + (new PasswordReset(['use_id' => 1, 'token' => 'loremIpsum123']))->save(); + (new PasswordReset(['use_id' => 3, 'token' => '5omeR4nd0mTok3N']))->save(); + + $stats = new Stats($this->database); + $this->assertEquals(2, $stats->passwordResets()); + } + /** * Add some example users */ -- cgit v1.2.3-54-g00ecf