mirror of
https://github.com/henrywhitaker3/Speedtest-Tracker.git
synced 2025-12-21 21:33:08 +01:00
Added unit tests for helpers and tests for commands
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -16,3 +16,4 @@ yarn-error.log
|
|||||||
_ide_helper.php
|
_ide_helper.php
|
||||||
.idea
|
.idea
|
||||||
.config
|
.config
|
||||||
|
reports/
|
||||||
|
|||||||
21
tests/Feature/Commands/AcceptEULACommandTest.php
Normal file
21
tests/Feature/Commands/AcceptEULACommandTest.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class AcceptEULACommandTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A basic feature test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testAcceptEULA()
|
||||||
|
{
|
||||||
|
$response = $this->artisan('speedtest:eula')
|
||||||
|
->assertExitCode(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Tests\Feature;
|
namespace Tests\Feature\Commands;
|
||||||
|
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
use Illuminate\Foundation\Testing\WithFaker;
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
73
tests/Feature/Commands/AuthenticationCommandTest.php
Normal file
73
tests/Feature/Commands/AuthenticationCommandTest.php
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Commands;
|
||||||
|
|
||||||
|
use App\Console\Commands\AuthenticationCommand;
|
||||||
|
use App\Helpers\SettingsHelper;
|
||||||
|
use Artisan;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
|
use Symfony\Component\Console\Tester\CommandTester;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class AuthenticationCommandTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enable app auth
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testEnableAuth()
|
||||||
|
{
|
||||||
|
SettingsHelper::set('auth', false);
|
||||||
|
|
||||||
|
$this->assertEquals(Artisan::call('speedtest:auth', [ '--enable' => true ]), 0);
|
||||||
|
|
||||||
|
$this->assertTrue((bool)SettingsHelper::get('auth')->value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disable app auth
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testDisableAuth()
|
||||||
|
{
|
||||||
|
SettingsHelper::set('auth', true);
|
||||||
|
|
||||||
|
$this->assertEquals(Artisan::call('speedtest:auth', [ '--disable' => true ]), 0);
|
||||||
|
|
||||||
|
$this->assertFalse((bool)SettingsHelper::get('auth')->value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test invalid params for command
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
// public function testAuthBothOptions()
|
||||||
|
// {
|
||||||
|
// $command = new AuthenticationCommand();
|
||||||
|
// $command->setLaravel($this->app);
|
||||||
|
// $tester = new CommandTester($command);
|
||||||
|
// $tester->setInputs([]);
|
||||||
|
// $tester->execute([ '--enable' => true, '--disable' => true ]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test invalid params for command
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
// public function testAuthNoOptions()
|
||||||
|
// {
|
||||||
|
// $command = new AuthenticationCommand();
|
||||||
|
// $command->setLaravel($this->app);
|
||||||
|
// $tester = new CommandTester($command);
|
||||||
|
// $tester->setInputs([]);
|
||||||
|
// $tester->execute([]);
|
||||||
|
// }
|
||||||
|
}
|
||||||
24
tests/Feature/Commands/ClearOldSessionCommandTest.php
Normal file
24
tests/Feature/Commands/ClearOldSessionCommandTest.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class ClearOldSessionCommandTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic feature test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testClearSessionsCommand()
|
||||||
|
{
|
||||||
|
$this->artisan('speedtest:clear-sessions')
|
||||||
|
->expectsOutput('Invalidated expired sessions')
|
||||||
|
->assertExitCode(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
23
tests/Feature/Commands/ClearQueueCommandTest.php
Normal file
23
tests/Feature/Commands/ClearQueueCommandTest.php
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class ClearQueueCommandTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic feature test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testClearQueue()
|
||||||
|
{
|
||||||
|
$this->artisan('queue:clear')
|
||||||
|
->assertExitCode(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
27
tests/Feature/Commands/GetConfigCommandTest.php
Normal file
27
tests/Feature/Commands/GetConfigCommandTest.php
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Commands;
|
||||||
|
|
||||||
|
use App\Helpers\SettingsHelper;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class GetConfigCommandTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic feature test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testGetConfig()
|
||||||
|
{
|
||||||
|
$configJson = json_encode(SettingsHelper::getConfig(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
||||||
|
|
||||||
|
$this->artisan('speedtest:config')
|
||||||
|
->expectsOutput($configJson)
|
||||||
|
->assertExitCode(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
29
tests/Feature/Commands/SetSlackWebhookCommandTest.php
Normal file
29
tests/Feature/Commands/SetSlackWebhookCommandTest.php
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Commands;
|
||||||
|
|
||||||
|
use App\Helpers\SettingsHelper;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class SetSlackWebhookCommandTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic feature test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testSetSlackWebhook()
|
||||||
|
{
|
||||||
|
SettingsHelper::set('slack_webhook', 'pre-test');
|
||||||
|
|
||||||
|
$this->artisan('speedtest:slack', [ 'webhook' => 'test' ])
|
||||||
|
->expectsOutput('Slack webhook updated')
|
||||||
|
->assertExitCode(0);
|
||||||
|
|
||||||
|
$this->assertEquals(SettingsHelper::get('slack_webhook')->value, 'test');
|
||||||
|
}
|
||||||
|
}
|
||||||
33
tests/Feature/Commands/SetTelegramOptionsCommandTest.php
Normal file
33
tests/Feature/Commands/SetTelegramOptionsCommandTest.php
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Commands;
|
||||||
|
|
||||||
|
use App\Helpers\SettingsHelper;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class SetTelegramOptionsCommandTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic feature test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testSetTelegramOptions()
|
||||||
|
{
|
||||||
|
SettingsHelper::set('telegram_bot_token', 'pre-test-bot');
|
||||||
|
SettingsHelper::set('telegram_chat_id', 'pre-test-bot');
|
||||||
|
|
||||||
|
$this->artisan('speedtest:telegram', [
|
||||||
|
'--bot' => 'test-bot',
|
||||||
|
'--chat' => 'test-chat'
|
||||||
|
])->expectsOutput('Telegram options updated')
|
||||||
|
->assertExitCode(0);
|
||||||
|
|
||||||
|
$this->assertEquals(SettingsHelper::get('telegram_bot_token')->value, 'test-bot');
|
||||||
|
$this->assertEquals(SettingsHelper::get('telegram_chat_id')->value, 'test-chat');
|
||||||
|
}
|
||||||
|
}
|
||||||
23
tests/Feature/Commands/SpeedtestOverviewCommandTest.php
Normal file
23
tests/Feature/Commands/SpeedtestOverviewCommandTest.php
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class SpeedtestOverviewCommandTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic feature test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testOverviewCommand()
|
||||||
|
{
|
||||||
|
$this->artisan('speedtest:overview')
|
||||||
|
->assertExitCode(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
23
tests/Feature/Commands/TestNotificationCommandTest.php
Normal file
23
tests/Feature/Commands/TestNotificationCommandTest.php
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class TestNotificationCommandTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic feature test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testTestNotificationCommand()
|
||||||
|
{
|
||||||
|
$this->artisan('speedtest:notification')
|
||||||
|
->assertExitCode(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit\Helpers\NotificationsHelper;
|
||||||
|
|
||||||
|
use App\Helpers\NotificationsHelper;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class ThresholdMessageTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Test absolute message
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testAbsoluteMessageMultiField()
|
||||||
|
{
|
||||||
|
$msg = NotificationsHelper::formatAbsoluteThresholdMessage([ 'ping', 'upload' ]);
|
||||||
|
|
||||||
|
$this->assertEquals('For the latest speedtest, the ping, upload values exceeded the absolute threshold', $msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test absolute message
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testAbsoluteMessageSingleField()
|
||||||
|
{
|
||||||
|
$msg = NotificationsHelper::formatAbsoluteThresholdMessage([ 'ping' ]);
|
||||||
|
|
||||||
|
$this->assertEquals('For the latest speedtest, the ping value exceeded the absolute threshold', $msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test absolute message
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testPercentageMessageMultiField()
|
||||||
|
{
|
||||||
|
$msg = NotificationsHelper::formatPercentageThresholdMessage([ 'ping', 'upload' ]);
|
||||||
|
|
||||||
|
$this->assertEquals('For the latest speedtest, the ping, upload values exceeded the percentage threshold', $msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test absolute message
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testPercentageMessageSingleField()
|
||||||
|
{
|
||||||
|
$msg = NotificationsHelper::formatPercentageThresholdMessage([ 'ping' ]);
|
||||||
|
|
||||||
|
$this->assertEquals('For the latest speedtest, the ping value exceeded the percentage threshold', $msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
38
tests/Unit/Helpers/SettingsHelper/SettingsGetTest.php
Normal file
38
tests/Unit/Helpers/SettingsHelper/SettingsGetTest.php
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit\Helpers\SettingsHelper;
|
||||||
|
|
||||||
|
use App\Helpers\SettingsHelper;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class SettingsGetTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic feature test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testValidSetting()
|
||||||
|
{
|
||||||
|
$response = SettingsHelper::get('auth');
|
||||||
|
|
||||||
|
$this->assertNotFalse($response);
|
||||||
|
$this->assertFalse((bool)$response->value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic feature test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testInvalidSetting()
|
||||||
|
{
|
||||||
|
$response = SettingsHelper::get('test');
|
||||||
|
|
||||||
|
$this->assertFalse($response);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit\Helpers\SettingsHelper;
|
||||||
|
|
||||||
|
use App\Helpers\SettingsHelper;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class SettingsLoadIntegrationsConfigTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic feature test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testLoadIntegrationsConfig()
|
||||||
|
{
|
||||||
|
$preLoad = [
|
||||||
|
"healthchecks_enabled" => false,
|
||||||
|
"healthchecks_uuid" => null,
|
||||||
|
"slack_webhook" => null,
|
||||||
|
"telegram_bot_token" => null,
|
||||||
|
"telegram_chat_id" => null,
|
||||||
|
];
|
||||||
|
|
||||||
|
SettingsHelper::set('slack_webhook', 'test');
|
||||||
|
|
||||||
|
SettingsHelper::loadIntegrationConfig();
|
||||||
|
|
||||||
|
$this->assertEquals(config('integrations.slack_webhook'), 'test');
|
||||||
|
}
|
||||||
|
}
|
||||||
25
tests/Unit/Helpers/SettingsHelper/SettingsSetTest.php
Normal file
25
tests/Unit/Helpers/SettingsHelper/SettingsSetTest.php
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit\Helpers\SettingsHelper;
|
||||||
|
|
||||||
|
use App\Helpers\SettingsHelper;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class SettingsSetTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic feature test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testUpdatingSetting()
|
||||||
|
{
|
||||||
|
$response = SettingsHelper::set('auth', 'hello');
|
||||||
|
|
||||||
|
$this->assertEquals('hello', $response->value);
|
||||||
|
}
|
||||||
|
}
|
||||||
170
tests/Unit/Helpers/SpeedtestHelper/AbsoluteThresholdTest.php
Normal file
170
tests/Unit/Helpers/SpeedtestHelper/AbsoluteThresholdTest.php
Normal file
@@ -0,0 +1,170 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit\Helpers\SpeedtestHelper;
|
||||||
|
|
||||||
|
use App\Helpers\SettingsHelper;
|
||||||
|
use App\Helpers\SpeedtestHelper;
|
||||||
|
use App\Speedtest;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class AbsoluteThresholdTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testAbsoluteDownloadThresholdExceeded()
|
||||||
|
{
|
||||||
|
$threshold = 10;
|
||||||
|
$dl = 5;
|
||||||
|
|
||||||
|
SettingsHelper::set('threshold_alert_absolute_download', $threshold);
|
||||||
|
|
||||||
|
$test = Speedtest::create([
|
||||||
|
'download' => $dl,
|
||||||
|
'upload' => 11,
|
||||||
|
'ping' => 5
|
||||||
|
]);
|
||||||
|
|
||||||
|
$result = SpeedtestHelper::testIsLowerThanThreshold('absolute', $test);
|
||||||
|
|
||||||
|
$this->assertEquals([ 'download' ], $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testAbsoluteDownloadThresholdNotExceeded()
|
||||||
|
{
|
||||||
|
$threshold = 1;
|
||||||
|
$dl = 5;
|
||||||
|
|
||||||
|
SettingsHelper::set('threshold_alert_absolute_download', $threshold);
|
||||||
|
|
||||||
|
$test = Speedtest::create([
|
||||||
|
'download' => $dl,
|
||||||
|
'upload' => 11,
|
||||||
|
'ping' => 5
|
||||||
|
]);
|
||||||
|
|
||||||
|
$result = SpeedtestHelper::testIsLowerThanThreshold('absolute', $test);
|
||||||
|
|
||||||
|
$this->assertEquals([], $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testAbsoluteUploadThresholdExceeded()
|
||||||
|
{
|
||||||
|
$threshold = 10;
|
||||||
|
$ul = 5;
|
||||||
|
|
||||||
|
SettingsHelper::set('threshold_alert_absolute_upload', $threshold);
|
||||||
|
|
||||||
|
$test = Speedtest::create([
|
||||||
|
'download' => 11,
|
||||||
|
'upload' => $ul,
|
||||||
|
'ping' => 5
|
||||||
|
]);
|
||||||
|
|
||||||
|
$result = SpeedtestHelper::testIsLowerThanThreshold('absolute', $test);
|
||||||
|
|
||||||
|
$this->assertEquals([ 'upload' ], $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testAbsoluteUploadThresholdNotExceeded()
|
||||||
|
{
|
||||||
|
$threshold = 1;
|
||||||
|
$ul = 5;
|
||||||
|
|
||||||
|
SettingsHelper::set('threshold_alert_absolute_upload', $threshold);
|
||||||
|
|
||||||
|
$test = Speedtest::create([
|
||||||
|
'download' => 11,
|
||||||
|
'upload' => $ul,
|
||||||
|
'ping' => 5
|
||||||
|
]);
|
||||||
|
|
||||||
|
$result = SpeedtestHelper::testIsLowerThanThreshold('absolute', $test);
|
||||||
|
|
||||||
|
$this->assertEquals([], $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testAbsolutePingThresholdExceeded()
|
||||||
|
{
|
||||||
|
$threshold = 10;
|
||||||
|
$ping = 11;
|
||||||
|
|
||||||
|
SettingsHelper::set('threshold_alert_absolute_ping', $threshold);
|
||||||
|
|
||||||
|
$test = Speedtest::create([
|
||||||
|
'download' => 10,
|
||||||
|
'upload' => 10,
|
||||||
|
'ping' => $ping
|
||||||
|
]);
|
||||||
|
|
||||||
|
$result = SpeedtestHelper::testIsLowerThanThreshold('absolute', $test);
|
||||||
|
|
||||||
|
$this->assertEquals([ 'ping' ], $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testAbsolutePingThresholdNotExceeded()
|
||||||
|
{
|
||||||
|
$threshold = 10;
|
||||||
|
$ping = 9;
|
||||||
|
|
||||||
|
SettingsHelper::set('threshold_alert_absolute_ping', $threshold);
|
||||||
|
|
||||||
|
$test = Speedtest::create([
|
||||||
|
'download' => 10,
|
||||||
|
'upload' => 10,
|
||||||
|
'ping' => $ping
|
||||||
|
]);
|
||||||
|
|
||||||
|
$result = SpeedtestHelper::testIsLowerThanThreshold('absolute', $test);
|
||||||
|
|
||||||
|
$this->assertEquals([], $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testInvalidArgument()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
SpeedtestHelper::testIsLowerThanThreshold('test', new Speedtest());
|
||||||
|
} catch(InvalidArgumentException $e) {
|
||||||
|
$this->assertTrue(true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertTrue(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
59
tests/Unit/Helpers/SpeedtestHelper/CheckOutputTest.php
Normal file
59
tests/Unit/Helpers/SpeedtestHelper/CheckOutputTest.php
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit\Helpers\SpeedtestHelper;
|
||||||
|
|
||||||
|
use App\Helpers\SpeedtestHelper;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class CheckOutputTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testGoodOutput()
|
||||||
|
{
|
||||||
|
$expected = [
|
||||||
|
'type' => 'result',
|
||||||
|
'download' => [ 'bandwidth' => '*' ],
|
||||||
|
'upload' => [ 'bandwidth' => '*' ],
|
||||||
|
'ping' => [ 'latency' => '*' ],
|
||||||
|
'server' => [
|
||||||
|
'id' => '*',
|
||||||
|
'name' => '*',
|
||||||
|
'host' => '*',
|
||||||
|
'port' => '*',
|
||||||
|
],
|
||||||
|
'result' => [
|
||||||
|
'url' => '*',
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->assertTrue(SpeedtestHelper::checkOutputIsComplete($expected));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testBadOutput()
|
||||||
|
{
|
||||||
|
$expected = [
|
||||||
|
'type' => 'result',
|
||||||
|
'download' => [ 'bandwidth' => '*' ],
|
||||||
|
'server' => [
|
||||||
|
'id' => '*',
|
||||||
|
'name' => '*',
|
||||||
|
'host' => '*',
|
||||||
|
'port' => '*',
|
||||||
|
],
|
||||||
|
'result' => [
|
||||||
|
'url' => '*',
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->assertFalse(SpeedtestHelper::checkOutputIsComplete($expected));
|
||||||
|
}
|
||||||
|
}
|
||||||
46
tests/Unit/Helpers/SpeedtestHelper/FailureRateTest.php
Normal file
46
tests/Unit/Helpers/SpeedtestHelper/FailureRateTest.php
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit\Helpers\SpeedtestHelper;
|
||||||
|
|
||||||
|
use App\Helpers\SpeedtestHelper;
|
||||||
|
use App\Speedtest;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class FailureRateTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testFailureRate()
|
||||||
|
{
|
||||||
|
$success = rand(1, 15);
|
||||||
|
$failed = rand(1, 15);
|
||||||
|
|
||||||
|
for($i = 0; $i < $success; $i++) {
|
||||||
|
Speedtest::create([
|
||||||
|
'ping' => 5,
|
||||||
|
'download' => 5,
|
||||||
|
'upload' => 5
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for($i = 0; $i < $failed; $i++) {
|
||||||
|
Speedtest::create([
|
||||||
|
'ping' => 5,
|
||||||
|
'download' => 5,
|
||||||
|
'upload' => 5,
|
||||||
|
'failed' => true
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$output = SpeedtestHelper::failureRate(1);
|
||||||
|
|
||||||
|
$this->assertEquals($output[0]['success'], $success);
|
||||||
|
$this->assertEquals($output[0]['failure'], $failed);
|
||||||
|
}
|
||||||
|
}
|
||||||
69
tests/Unit/Helpers/SpeedtestHelper/ParseUnitsTest.php
Normal file
69
tests/Unit/Helpers/SpeedtestHelper/ParseUnitsTest.php
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit\Helpers\SpeedtestHelper;
|
||||||
|
|
||||||
|
use App\Helpers\SpeedtestHelper;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class ParseUnitsTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testMbyteToMb()
|
||||||
|
{
|
||||||
|
$initial = '80 Mbyte';
|
||||||
|
$expected = 640;
|
||||||
|
|
||||||
|
$result = SpeedtestHelper::parseUnits($initial);
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $result['val']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testKbitToMb()
|
||||||
|
{
|
||||||
|
$initial = '80 Kbit';
|
||||||
|
$expected = 0.08;
|
||||||
|
|
||||||
|
$result = SpeedtestHelper::parseUnits($initial);
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $result['val']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testKbyteToMb()
|
||||||
|
{
|
||||||
|
$initial = '80 Kbyte';
|
||||||
|
$expected = 0.64;
|
||||||
|
|
||||||
|
$result = SpeedtestHelper::parseUnits($initial);
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $result['val']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testMbToMb()
|
||||||
|
{
|
||||||
|
$initial = '80 Mbit';
|
||||||
|
$expected = 80;
|
||||||
|
|
||||||
|
$result = SpeedtestHelper::parseUnits($initial);
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $result['val']);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit\Helpers\SpeedtestHelper;
|
||||||
|
|
||||||
|
use App\Helpers\SettingsHelper;
|
||||||
|
use App\Helpers\SpeedtestHelper;
|
||||||
|
use App\Speedtest;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class PercentageThresholdTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
public function setUp() : void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->createAverageOf5();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testPercentageThresholdDownloadExceeded()
|
||||||
|
{
|
||||||
|
SettingsHelper::set('threshold_alert_percentage', 15);
|
||||||
|
|
||||||
|
$test = Speedtest::create([
|
||||||
|
'download' => 5 * 0.8,
|
||||||
|
'upload' => 5,
|
||||||
|
'ping' => 5
|
||||||
|
]);
|
||||||
|
|
||||||
|
$result = SpeedtestHelper::testIsLowerThanThreshold('percentage', $test);
|
||||||
|
|
||||||
|
$this->assertEquals([ 'download' ], $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testThresholdNotSet()
|
||||||
|
{
|
||||||
|
SettingsHelper::set('threshold_alert_percentage', '');
|
||||||
|
|
||||||
|
$test = Speedtest::create([
|
||||||
|
'download' => 5 * 0.9,
|
||||||
|
'upload' => 5,
|
||||||
|
'ping' => 5
|
||||||
|
]);
|
||||||
|
|
||||||
|
$result = SpeedtestHelper::testIsLowerThanThreshold('percentage', $test);
|
||||||
|
|
||||||
|
$this->assertEquals([], $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createAverageOf5()
|
||||||
|
{
|
||||||
|
for($i = 0; $i < 5; $i++) {
|
||||||
|
Speedtest::create([
|
||||||
|
'download' => 5,
|
||||||
|
'upload' => 5,
|
||||||
|
'ping' => 5
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
76
tests/Unit/Helpers/SpeedtestHelper/SpeedtestTest.php
Normal file
76
tests/Unit/Helpers/SpeedtestHelper/SpeedtestTest.php
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit\Helpers\SpeedtestHelper;
|
||||||
|
|
||||||
|
use App\Helpers\SpeedtestHelper;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use JsonException;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class SpeedtestTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
private $output;
|
||||||
|
|
||||||
|
public function setUp() : void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->output = SpeedtestHelper::output();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testOutputFunction()
|
||||||
|
{
|
||||||
|
$this->assertJson($this->output);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testRunSpeedtestWithExistingOutput()
|
||||||
|
{
|
||||||
|
$output = json_decode($this->output, true);
|
||||||
|
|
||||||
|
$test = SpeedtestHelper::runSpeedtest($this->output);
|
||||||
|
|
||||||
|
$this->assertEquals($output['ping']['latency'], $test->ping);
|
||||||
|
$this->assertEquals(SpeedtestHelper::convert($output['download']['bandwidth']), $test->download);
|
||||||
|
$this->assertEquals(SpeedtestHelper::convert($output['upload']['bandwidth']), $test->upload);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testInvaidJson()
|
||||||
|
{
|
||||||
|
$json = '{hi: hi}';
|
||||||
|
|
||||||
|
$o = SpeedtestHelper::runSpeedtest($json);
|
||||||
|
|
||||||
|
$this->assertFalse($o);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic unit test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testIncompleteJson()
|
||||||
|
{
|
||||||
|
$json = '{"hi": "hi"}';
|
||||||
|
|
||||||
|
$o = SpeedtestHelper::runSpeedtest($json);
|
||||||
|
|
||||||
|
$this->assertFalse($o);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user