mirror of
https://github.com/henrywhitaker3/Speedtest-Tracker.git
synced 2025-12-21 13:23:04 +01:00
Used a mock ooklatester in tests
Speed up test suite
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# Speedtest Tracker
|
||||
|
||||
[](https://hub.docker.com/r/henrywhitaker3/speedtest-tracker) [](https://github.com/henrywhitaker3/Speedtest-Tracker/actions) [](https://github.com/henrywhitaker3/Speedtest-Tracker/actions) [](https://github.com/henrywhitaker3/Speedtest-Tracker/commits) [](https://github.com/henrywhitaker3/Speedtest-Tracker/issues) [](https://github.com/henrywhitaker3/Speedtest-Tracker/commits)  [](https://github.com/henrywhitaker3/Speedtest-Tracker/blob/master/LICENSE)
|
||||
[](https://hub.docker.com/r/henrywhitaker3/speedtest-tracker) [](https://github.com/henrywhitaker3/Speedtest-Tracker/actions) [](https://github.com/henrywhitaker3/Speedtest-Tracker/actions) [](https://github.com/henrywhitaker3/Speedtest-Tracker/commits) [](https://github.com/henrywhitaker3/Speedtest-Tracker/issues) [](https://github.com/henrywhitaker3/Speedtest-Tracker/commits)  [](https://github.com/henrywhitaker3/Speedtest-Tracker/blob/master/LICENSE)
|
||||
|
||||
This program runs a speedtest check every hour and graphs the results. The back-end is written in [Laravel](https://laravel.com/) and the front-end uses [React](https://reactjs.org/). It uses the [Ookla's speedtest cli](https://www.speedtest.net/apps/cli) package to get the data and uses [Chart.js](https://www.chartjs.org/) to plot the results.
|
||||
|
||||
@@ -18,6 +18,7 @@ Disclaimer: You will need to accept Ookla's EULA and privacy agreements in order
|
||||
- Slack/Discord/Telegram notifications
|
||||
- [healthchecks.io](https://healthchecks.io) integration
|
||||
- Organizr integration
|
||||
- InfluxDB integration (currently v1 only, v2 is a WIP)
|
||||
|
||||
## Installation & Setup
|
||||
|
||||
@@ -84,6 +85,8 @@ Container images are configured using parameters passed at runtime (such as thos
|
||||
| `-e PUID` | Optional. Supply a local user ID for volume permissions |
|
||||
| `-e PGID` | Optional. Supply a local group ID for volume permissions |
|
||||
| `-e AUTH` | Optional. Set to 'true' to enable authentication for the app |
|
||||
| `-e INFLUXDB_RETENTION`| Optional. Sets the InfluxDB retention period, defaults to `30d` |
|
||||
| `-e INFLUXDB_HOST_TAG | Optional. Sets the InfluxDB host tag value, defaults to `speedtest` |
|
||||
|
||||
### Authentication
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
use App\Interfaces\SpeedtestProvider;
|
||||
use App\Models\Speedtest;
|
||||
use App\Utils\OoklaTester;
|
||||
use Carbon\Carbon;
|
||||
@@ -25,7 +26,7 @@ class SpeedtestHelper
|
||||
*/
|
||||
public static function runSpeedtest()
|
||||
{
|
||||
$tester = new OoklaTester();
|
||||
$tester = app()->make(SpeedtestProvider::class);
|
||||
return $tester->run();
|
||||
}
|
||||
|
||||
|
||||
37
app/Utils/InfluxDB/InfluxDBVersion2Wrapper.php
Normal file
37
app/Utils/InfluxDB/InfluxDBVersion2Wrapper.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utils\InfluxDB;
|
||||
|
||||
use App\Interfaces\InfluxDBWrapperInterface;
|
||||
use App\Models\Speedtest;
|
||||
use InfluxDB2\Client;
|
||||
|
||||
class InfluxDBVersion2Wrapper implements InfluxDBWrapperInterface
|
||||
{
|
||||
private Client $client;
|
||||
|
||||
public function __construct(Client $client)
|
||||
{
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
public function testConnection(): bool
|
||||
{
|
||||
return $this->client->health()->getStatus() !== 'pass';
|
||||
}
|
||||
|
||||
public function doesDatabaseExist(string $database): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function createDatabase(string $database): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function store(Speedtest $speedtest): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,10 @@
|
||||
{
|
||||
"1.12.0": [
|
||||
{
|
||||
"description": "Added InfluxDB intergation.",
|
||||
"link": ""
|
||||
}
|
||||
],
|
||||
"1.11.1": [
|
||||
{
|
||||
"description": "Add option to show/hide columns in the all tests table.",
|
||||
|
||||
@@ -7,7 +7,7 @@ return [
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
'version' => '1.11.1',
|
||||
'version' => '1.12.0',
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
||||
@@ -26,6 +26,7 @@ class SpeedtestFactory extends Factory
|
||||
'upload' => rand(15, 900),
|
||||
'ping' => rand(1, 25),
|
||||
'scheduled' => (bool) rand(0, 1),
|
||||
'failed' => false,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -3,14 +3,28 @@
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Helpers\SpeedtestHelper;
|
||||
use App\Interfaces\SpeedtestProvider;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Tests\Mocks\OoklaTesterMocker;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SpeedtestTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->app->singleton(
|
||||
SpeedtestProvider::class,
|
||||
function () {
|
||||
return new OoklaTesterMocker();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a speedtest
|
||||
*
|
||||
|
||||
26
tests/Feature/Utils/InfluxDB/InfluxDBWrapperWrapperTest.php
Normal file
26
tests/Feature/Utils/InfluxDB/InfluxDBWrapperWrapperTest.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Utils\InfluxDB;
|
||||
|
||||
use App\Exceptions\InfluxDBNotEnabledException;
|
||||
use App\Utils\InfluxDB\InfluxDB;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Tests\TestCase;
|
||||
|
||||
class InfluxDBWrapperWrapperTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* Test it throws the right exception.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_it_throws_excetpion_when_it_is_disabled()
|
||||
{
|
||||
$this->expectException(InfluxDBNotEnabledException::class);
|
||||
|
||||
InfluxDB::connect();
|
||||
}
|
||||
}
|
||||
59
tests/Mocks/OoklaTesterMocker.php
Normal file
59
tests/Mocks/OoklaTesterMocker.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Mocks;
|
||||
|
||||
use App\Exceptions\SpeedtestFailureException;
|
||||
use App\Interfaces\SpeedtestProvider;
|
||||
use App\Models\Speedtest;
|
||||
use Exception;
|
||||
|
||||
class OoklaTesterMocker implements SpeedtestProvider
|
||||
{
|
||||
private bool $passes;
|
||||
|
||||
public function __construct(bool $passes = true)
|
||||
{
|
||||
$this->passes = $passes;
|
||||
}
|
||||
|
||||
public function run($output = null): Speedtest
|
||||
{
|
||||
$output = $output ?? $this->output();
|
||||
|
||||
try {
|
||||
$output = json_decode($output, true, 512, JSON_THROW_ON_ERROR);
|
||||
} catch (Exception $e) {
|
||||
throw new SpeedtestFailureException();
|
||||
}
|
||||
|
||||
return $this->passes
|
||||
? Speedtest::factory()->create()
|
||||
: Speedtest::factory()->create([
|
||||
'download' => 0,
|
||||
'upload' => 0,
|
||||
'ping' => 0,
|
||||
'failed' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function output()
|
||||
{
|
||||
return !$this->passes
|
||||
? null
|
||||
: json_encode([
|
||||
'type' => 'result',
|
||||
'download' => ['bandwidth' => '50'],
|
||||
'upload' => ['bandwidth' => '50'],
|
||||
'ping' => ['latency' => '50'],
|
||||
'server' => [
|
||||
'id' => '1',
|
||||
'name' => 'PHPUnit',
|
||||
'host' => 'phpunit',
|
||||
'port' => '443',
|
||||
],
|
||||
'result' => [
|
||||
'url' => 'some-url',
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace Tests\Unit\Helpers\SpeedtestHelper;
|
||||
|
||||
use App\Utils\OoklaTester;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Tests\Mocks\OoklaTesterMocker;
|
||||
|
||||
class CheckOutputTest extends TestCase
|
||||
{
|
||||
@@ -12,6 +13,7 @@ class CheckOutputTest extends TestCase
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->speedtestProvider = new OoklaTester();
|
||||
$this->mocker = new OoklaTesterMocker();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Exceptions\SpeedtestFailureException;
|
||||
use App\Helpers\SpeedtestHelper;
|
||||
use App\Utils\OoklaTester;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\Mocks\OoklaTesterMocker;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SpeedtestTest extends TestCase
|
||||
@@ -22,17 +23,7 @@ class SpeedtestTest extends TestCase
|
||||
|
||||
$this->speedtestProvider = new OoklaTester();
|
||||
|
||||
$this->output = $this->speedtestProvider->output();
|
||||
}
|
||||
|
||||
/**
|
||||
* A basic unit test example.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testOutputFunction()
|
||||
{
|
||||
$this->assertJson($this->output);
|
||||
$this->output = (new OoklaTesterMocker())->output();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user