2018-12-18 02:23:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Controllers\Metrics;
|
|
|
|
|
2018-12-18 04:43:13 +01:00
|
|
|
use Carbon\Carbon;
|
2018-12-18 02:23:44 +01:00
|
|
|
use Engelsystem\Controllers\Metrics\Stats;
|
2018-12-18 04:43:13 +01:00
|
|
|
use Engelsystem\Models\LogEntry;
|
2019-11-10 21:30:26 +01:00
|
|
|
use Engelsystem\Models\News;
|
2019-12-07 19:31:13 +01:00
|
|
|
use Engelsystem\Models\Question;
|
2018-12-18 04:43:13 +01:00
|
|
|
use Engelsystem\Models\User\PasswordReset;
|
2018-12-21 20:09:15 +01:00
|
|
|
use Engelsystem\Models\User\PersonalData;
|
2018-12-18 02:23:44 +01:00
|
|
|
use Engelsystem\Models\User\State;
|
|
|
|
use Engelsystem\Models\User\User;
|
|
|
|
use Engelsystem\Test\Unit\HasDatabase;
|
|
|
|
use Engelsystem\Test\Unit\TestCase;
|
2018-12-21 20:09:15 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2018-12-18 02:23:44 +01:00
|
|
|
use Illuminate\Support\Str;
|
2018-12-18 04:43:13 +01:00
|
|
|
use Psr\Log\LogLevel;
|
2018-12-18 02:23:44 +01:00
|
|
|
|
|
|
|
class StatsTest extends TestCase
|
|
|
|
{
|
|
|
|
use HasDatabase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::__construct
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::getQuery
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::newUsers
|
2018-12-18 02:23:44 +01:00
|
|
|
*/
|
|
|
|
public function testNewUsers()
|
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(2, $stats->newUsers());
|
|
|
|
}
|
|
|
|
|
2018-12-21 20:09:15 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::vouchers
|
|
|
|
*/
|
|
|
|
public function testVouchers()
|
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(14, $stats->vouchers());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::tshirts
|
|
|
|
*/
|
|
|
|
public function testTshirts()
|
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(2, $stats->tshirts());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::raw
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::tshirtSizes
|
2018-12-21 20:09:15 +01:00
|
|
|
*/
|
|
|
|
public function testTshirtSizes()
|
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$sizes = $stats->tshirtSizes();
|
|
|
|
$this->assertCount(2, $sizes);
|
|
|
|
$this->assertEquals(new Collection([
|
|
|
|
(object)['shirt_size' => 'L', 'count' => 2],
|
|
|
|
(object)['shirt_size' => 'XXL', 'count' => 1],
|
|
|
|
]), $sizes);
|
|
|
|
}
|
|
|
|
|
2019-11-10 21:30:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::announcements
|
|
|
|
*/
|
|
|
|
public function testAnnouncements()
|
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
$newsData = ['title' => 'Test', 'text' => 'Foo Bar', 'user_id' => 1];
|
|
|
|
|
|
|
|
(new News($newsData))->save();
|
|
|
|
(new News($newsData))->save();
|
|
|
|
(new News($newsData + ['is_meeting' => true]))->save();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(3, $stats->announcements());
|
|
|
|
$this->assertEquals(2, $stats->announcements(false));
|
|
|
|
$this->assertEquals(1, $stats->announcements(true));
|
|
|
|
}
|
|
|
|
|
2019-12-07 19:31:13 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::questions
|
|
|
|
*/
|
|
|
|
public function testQuestions()
|
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
$questionsData = ['text' => 'Lorem Ipsum', 'user_id' => 1];
|
|
|
|
|
|
|
|
(new Question($questionsData))->save();
|
|
|
|
(new Question($questionsData))->save();
|
|
|
|
(new Question($questionsData + ['answerer_id' => 2, 'answer' => 'Dolor sit!']))->save();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(3, $stats->questions());
|
|
|
|
$this->assertEquals(2, $stats->questions(false));
|
|
|
|
$this->assertEquals(1, $stats->questions(true));
|
|
|
|
}
|
|
|
|
|
2018-12-18 02:23:44 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::arrivedUsers
|
|
|
|
*/
|
|
|
|
public function testArrivedUsers()
|
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
2018-12-21 20:09:15 +01:00
|
|
|
$this->assertEquals(6, $stats->arrivedUsers());
|
2018-12-18 02:23:44 +01:00
|
|
|
}
|
|
|
|
|
2019-12-24 14:33:11 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::forceActiveUsers
|
|
|
|
*/
|
|
|
|
public function testForceActiveUsers()
|
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(2, $stats->forceActiveUsers());
|
|
|
|
}
|
|
|
|
|
2018-12-18 04:43:13 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::sessions
|
|
|
|
*/
|
|
|
|
public function testSessions()
|
|
|
|
{
|
|
|
|
$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());
|
|
|
|
}
|
|
|
|
|
2019-08-21 14:22:31 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::databaseRead
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::databaseWrite
|
|
|
|
*/
|
|
|
|
public function testDatabase()
|
|
|
|
{
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
|
|
|
|
$read = $stats->databaseRead();
|
|
|
|
$write = $stats->databaseWrite();
|
|
|
|
|
|
|
|
$this->assertIsFloat($read);
|
|
|
|
$this->assertNotEmpty($read);
|
|
|
|
$this->assertIsFloat($write);
|
|
|
|
$this->assertNotEmpty($write);
|
|
|
|
}
|
|
|
|
|
2018-12-18 04:43:13 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::logEntries
|
|
|
|
*/
|
|
|
|
public function testLogEntries()
|
|
|
|
{
|
|
|
|
(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->addUsers();
|
|
|
|
|
2019-10-08 14:18:51 +02:00
|
|
|
(new PasswordReset(['user_id' => 1, 'token' => 'loremIpsum123']))->save();
|
|
|
|
(new PasswordReset(['user_id' => 3, 'token' => '5omeR4nd0mTok3N']))->save();
|
2018-12-18 04:43:13 +01:00
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(2, $stats->passwordResets());
|
|
|
|
}
|
|
|
|
|
2018-12-18 02:23:44 +01:00
|
|
|
/**
|
|
|
|
* Add some example users
|
|
|
|
*/
|
|
|
|
protected function addUsers()
|
|
|
|
{
|
|
|
|
$this->addUser();
|
2018-12-21 20:09:15 +01:00
|
|
|
$this->addUser([], ['shirt_size' => 'L']);
|
2018-12-18 02:23:44 +01:00
|
|
|
$this->addUser(['arrived' => 1]);
|
2018-12-21 20:09:15 +01:00
|
|
|
$this->addUser(['arrived' => 1, 'got_voucher' => 2], ['shirt_size' => 'XXL']);
|
|
|
|
$this->addUser(['arrived' => 1, 'got_voucher' => 9]);
|
2019-12-24 14:33:11 +01:00
|
|
|
$this->addUser(['arrived' => 1, 'got_voucher' => 3, 'force_active' => true]);
|
|
|
|
$this->addUser(['arrived' => 1, 'active' => 1, 'got_shirt' => true, 'force_active' => true]);
|
2018-12-21 20:09:15 +01:00
|
|
|
$this->addUser(['arrived' => 1, 'active' => 1, 'got_shirt' => true], ['shirt_size' => 'L']);
|
2018-12-18 02:23:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $state
|
2018-12-21 20:09:15 +01:00
|
|
|
* @param array $personalData
|
2018-12-18 02:23:44 +01:00
|
|
|
*/
|
2018-12-21 20:09:15 +01:00
|
|
|
protected function addUser(array $state = [], $personalData = [])
|
2018-12-18 02:23:44 +01:00
|
|
|
{
|
|
|
|
$name = 'user_' . Str::random(5);
|
|
|
|
|
|
|
|
$user = new User([
|
|
|
|
'name' => $name,
|
|
|
|
'password' => '',
|
|
|
|
'email' => $name . '@engel.example.com',
|
|
|
|
'api_key' => '',
|
|
|
|
]);
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
$state = new State($state);
|
|
|
|
$state->user()
|
|
|
|
->associate($user)
|
|
|
|
->save();
|
2018-12-21 20:09:15 +01:00
|
|
|
|
|
|
|
$personalData = new PersonalData($personalData);
|
|
|
|
$personalData->user()
|
|
|
|
->associate($user)
|
|
|
|
->save();
|
2018-12-18 02:23:44 +01:00
|
|
|
}
|
2019-11-10 21:30:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up the environment
|
|
|
|
*/
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->initDatabase();
|
|
|
|
}
|
2018-12-18 02:23:44 +01:00
|
|
|
}
|