Added unit tests for helpers and tests for commands

This commit is contained in:
Henry Whitaker
2020-08-25 18:26:21 +01:00
parent b30ec477f4
commit 94e4919b44
21 changed files with 927 additions and 1 deletions

1
.gitignore vendored
View File

@@ -16,3 +16,4 @@ yarn-error.log
_ide_helper.php
.idea
.config
reports/

View 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);
}
}

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\Feature;
namespace Tests\Feature\Commands;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;

View 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([]);
// }
}

View 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);
}
}

View 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);
}
}

View 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);
}
}

View 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');
}
}

View 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');
}
}

View 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);
}
}

View 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);
}
}

View File

@@ -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);
}
}

View 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);
}
}

View File

@@ -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');
}
}

View 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);
}
}

View 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);
}
}

View 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));
}
}

View 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);
}
}

View 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']);
}
}

View File

@@ -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
]);
}
}
}

View 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);
}
}