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

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