mirror of
https://github.com/henrywhitaker3/Speedtest-Tracker.git
synced 2025-12-24 06:28:27 +01:00
@@ -3,7 +3,7 @@
|
||||
{
|
||||
"description": "Changed integration config loading.",
|
||||
"link": ""
|
||||
}.
|
||||
},
|
||||
{
|
||||
"description": "Added more tests.",
|
||||
"link": ""
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Controllers\IntegrationsController;
|
||||
|
||||
use App;
|
||||
use App\Helpers\SettingsHelper;
|
||||
use App\Http\Controllers\IntegrationsController;
|
||||
use Henrywhitaker3\Healthchecks\Healthchecks;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class HealthcheckTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* Controller
|
||||
*
|
||||
* @var IntegrationsController
|
||||
*/
|
||||
private $controller;
|
||||
|
||||
private $uuid;
|
||||
|
||||
public function setUp() : void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->controller = new IntegrationsController();
|
||||
|
||||
$this->uuid = env('HEALTHCHECKS_UUID');
|
||||
$this->bindFacade($this->uuid);
|
||||
}
|
||||
|
||||
public function testStartPing()
|
||||
{
|
||||
$resp = $this->controller->testHealthchecks('start')->original;
|
||||
|
||||
$this->assertEquals([
|
||||
'method' => 'test healthchecks \'start\' endpoint',
|
||||
'success' => true
|
||||
], $resp);
|
||||
}
|
||||
|
||||
public function testSuccessPing()
|
||||
{
|
||||
$resp = $this->controller->testHealthchecks('success')->original;
|
||||
|
||||
$this->assertEquals([
|
||||
'method' => 'test healthchecks \'success\' endpoint',
|
||||
'success' => true
|
||||
], $resp);
|
||||
}
|
||||
|
||||
public function testFailPing()
|
||||
{
|
||||
$resp = $this->controller->testHealthchecks('fail')->original;
|
||||
|
||||
$this->assertEquals([
|
||||
'method' => 'test healthchecks \'fail\' endpoint',
|
||||
'success' => true
|
||||
], $resp);
|
||||
}
|
||||
|
||||
public function testInvalidUUID()
|
||||
{
|
||||
$this->bindFacade('test');
|
||||
|
||||
$resp = $this->controller->testHealthchecks('start')->original;
|
||||
|
||||
$this->assertEquals([
|
||||
'method' => 'test healthchecks \'start\' endpoint',
|
||||
'success' => false,
|
||||
'error' => 'Invalid UUID'
|
||||
], $resp);
|
||||
|
||||
$this->bindFacade($this->uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* As clean install before setting up, there is no healthchecks
|
||||
* uuid in the db, so the facade doesn't get created during boot,
|
||||
* now just bind it in the container on test setup
|
||||
*
|
||||
* @param String $uuid
|
||||
* @return void
|
||||
*/
|
||||
private function bindFacade(String $uuid)
|
||||
{
|
||||
App::bind('healthcheck', function() use ($uuid) {
|
||||
return new Healthchecks($uuid);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Controllers\IntegrationsController;
|
||||
|
||||
use App\Http\Controllers\IntegrationsController;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class NotificationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* Controller
|
||||
*
|
||||
* @var IntegrationsController
|
||||
*/
|
||||
private $controller;
|
||||
|
||||
public function setUp() : void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->controller = new IntegrationsController();
|
||||
}
|
||||
|
||||
public function testNotificationsTest()
|
||||
{
|
||||
$resp = $this->controller->testNotification()->original;
|
||||
|
||||
$this->assertEquals([
|
||||
'method' => 'test notification agents'
|
||||
], $resp);
|
||||
}
|
||||
}
|
||||
66
tests/Unit/Controllers/SpeedtestController/DeleteTest.php
Normal file
66
tests/Unit/Controllers/SpeedtestController/DeleteTest.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Controllers\SpeedtestController;
|
||||
|
||||
use App\Http\Controllers\SpeedtestController;
|
||||
use App\Speedtest;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class DeleteTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* SpeedtestController
|
||||
*
|
||||
* @var SpeedtestController
|
||||
*/
|
||||
private $controller;
|
||||
|
||||
public function setUp() : void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->controller = new SpeedtestController();
|
||||
}
|
||||
|
||||
public function testDeleteAll()
|
||||
{
|
||||
for($i = 0; $i < 5; $i++) {
|
||||
Speedtest::create([
|
||||
'download' => 5,
|
||||
'upload' => 5,
|
||||
'ping' => 5
|
||||
]);
|
||||
}
|
||||
|
||||
$this->assertEquals(5, Speedtest::count());
|
||||
|
||||
$resp = $this->controller->deleteAll()->original;
|
||||
|
||||
$this->assertArrayHasKey('method', $resp);
|
||||
$this->assertArrayHasKey('success', $resp);
|
||||
|
||||
$this->assertEquals(0, Speedtest::count());
|
||||
}
|
||||
|
||||
public function testDeleteSpecific()
|
||||
{
|
||||
$test = Speedtest::create([
|
||||
'download' => 5,
|
||||
'upload' => 5,
|
||||
'ping' => 5
|
||||
]);
|
||||
$id = $test->id;
|
||||
|
||||
$this->assertNotNull(Speedtest::find($id));
|
||||
|
||||
$resp = $this->controller->delete($test)->original;
|
||||
|
||||
$this->assertArrayHasKey('method', $resp);
|
||||
$this->assertArrayHasKey('success', $resp);
|
||||
|
||||
$this->assertNull(Speedtest::find($id));
|
||||
}
|
||||
}
|
||||
43
tests/Unit/Controllers/SpeedtestController/FailTest.php
Normal file
43
tests/Unit/Controllers/SpeedtestController/FailTest.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Controllers\SpeedtestController;
|
||||
|
||||
use App\Http\Controllers\SpeedtestController;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class FailTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* SpeedtestController
|
||||
*
|
||||
* @var SpeedtestController
|
||||
*/
|
||||
private $controller;
|
||||
|
||||
public function setUp() : void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->controller = new SpeedtestController();
|
||||
}
|
||||
|
||||
public function testFail()
|
||||
{
|
||||
$resp = $this->controller->fail(5)->original;
|
||||
|
||||
$this->assertArrayHasKey('method', $resp);
|
||||
$this->assertArrayHasKey('data', $resp);
|
||||
$this->assertArrayHasKey('days', $resp);
|
||||
}
|
||||
|
||||
public function testFailInvalidInput()
|
||||
{
|
||||
$resp = $this->controller->fail('test')->original;
|
||||
|
||||
$this->assertArrayHasKey('method', $resp);
|
||||
$this->assertArrayHasKey('error', $resp);
|
||||
}
|
||||
}
|
||||
34
tests/Unit/Controllers/SpeedtestController/IndexTest.php
Normal file
34
tests/Unit/Controllers/SpeedtestController/IndexTest.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Controllers\SpeedtestController;
|
||||
|
||||
use App\Http\Controllers\SpeedtestController;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class IndexTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* SpeedtestController
|
||||
*
|
||||
* @var SpeedtestController
|
||||
*/
|
||||
private $controller;
|
||||
|
||||
public function setUp() : void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->controller = new SpeedtestController();
|
||||
}
|
||||
|
||||
public function testIndex()
|
||||
{
|
||||
$resp = $this->controller->index()->original;
|
||||
|
||||
$this->assertArrayHasKey('method', $resp);
|
||||
$this->assertArrayHasKey('data', $resp);
|
||||
}
|
||||
}
|
||||
58
tests/Unit/Controllers/SpeedtestController/LatestTest.php
Normal file
58
tests/Unit/Controllers/SpeedtestController/LatestTest.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Controllers\SpeedtestController;
|
||||
|
||||
use App\Http\Controllers\SpeedtestController;
|
||||
use App\Speedtest;
|
||||
use DB;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LatestTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* SpeedtestController
|
||||
*
|
||||
* @var SpeedtestController
|
||||
*/
|
||||
private $controller;
|
||||
|
||||
public function setUp() : void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->controller = new SpeedtestController();
|
||||
}
|
||||
|
||||
public function testLatestNoEntries()
|
||||
{
|
||||
DB::table('speedtests')->delete();
|
||||
|
||||
$resp = $this->controller->latest();
|
||||
$resp = $resp->original;
|
||||
|
||||
$this->assertEquals([
|
||||
'method' => 'get latest speedtest',
|
||||
'error' => 'no speedtests have been run'
|
||||
], $resp);
|
||||
}
|
||||
|
||||
public function testLatest()
|
||||
{
|
||||
$test = Speedtest::create([
|
||||
'download' => 5,
|
||||
'upload' => 5,
|
||||
'ping' => 5
|
||||
]);
|
||||
$test = $test->attributesToArray();
|
||||
|
||||
$resp = $this->controller->latest();
|
||||
$resp = $resp->original;
|
||||
|
||||
$this->assertArrayHasKey('data', $resp);
|
||||
$this->assertArrayHasKey('average', $resp);
|
||||
$this->assertArrayHasKey('max', $resp);
|
||||
}
|
||||
}
|
||||
42
tests/Unit/Controllers/SpeedtestController/RunTest.php
Normal file
42
tests/Unit/Controllers/SpeedtestController/RunTest.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Controllers\SpeedtestController;
|
||||
|
||||
use App\Http\Controllers\SpeedtestController;
|
||||
use App\Jobs\SpeedtestJob;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Queue;
|
||||
use Tests\TestCase;
|
||||
|
||||
class RunTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* SpeedtestController
|
||||
*
|
||||
* @var SpeedtestController
|
||||
*/
|
||||
private $controller;
|
||||
|
||||
public function setUp() : void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->controller = new SpeedtestController();
|
||||
}
|
||||
|
||||
public function testRun()
|
||||
{
|
||||
Queue::fake();
|
||||
|
||||
$resp = $this->controller->run()->original;
|
||||
|
||||
$this->assertArrayHasKey('method', $resp);
|
||||
$this->assertArrayHasKey('data', $resp);
|
||||
|
||||
Queue::assertPushed(SpeedtestJob::class, function($job) {
|
||||
return true;
|
||||
});
|
||||
}
|
||||
}
|
||||
43
tests/Unit/Controllers/SpeedtestController/TimeTest.php
Normal file
43
tests/Unit/Controllers/SpeedtestController/TimeTest.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Controllers\SpeedtestController;
|
||||
|
||||
use App\Http\Controllers\SpeedtestController;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TimeTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* SpeedtestController
|
||||
*
|
||||
* @var SpeedtestController
|
||||
*/
|
||||
private $controller;
|
||||
|
||||
public function setUp() : void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->controller = new SpeedtestController();
|
||||
}
|
||||
|
||||
public function testTime()
|
||||
{
|
||||
$resp = $this->controller->time(5)->original;
|
||||
|
||||
$this->assertArrayHasKey('method', $resp);
|
||||
$this->assertArrayHasKey('data', $resp);
|
||||
$this->assertArrayHasKey('days', $resp);
|
||||
}
|
||||
|
||||
public function testTimeInvalidInput()
|
||||
{
|
||||
$resp = $this->controller->time('test')->original;
|
||||
|
||||
$this->assertArrayHasKey('method', $resp);
|
||||
$this->assertArrayHasKey('error', $resp);
|
||||
}
|
||||
}
|
||||
@@ -2,17 +2,41 @@
|
||||
|
||||
namespace Tests\Unit\Listeners\SpeedtestFailedListener;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use App\Helpers\SettingsHelper;
|
||||
use App\Listeners\SpeedtestFailedListener;
|
||||
use App\Speedtest;
|
||||
use Exception;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use stdClass;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SlackTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* A basic unit test example.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testExample()
|
||||
public function testSlackFailedNotification()
|
||||
{
|
||||
SettingsHelper::set('speedtest_notifications', true);
|
||||
SettingsHelper::set('slack_webhook', env('SLACK_WEBHOOK'));
|
||||
|
||||
$l = new SpeedtestFailedListener();
|
||||
$test = Speedtest::create([ 'download' => 0, 'upload' => 0, 'ping' => 0, 'failed' => true ]);
|
||||
|
||||
$event = new stdClass();
|
||||
$event->speedtest = $test;
|
||||
|
||||
try {
|
||||
$l->handle($event);
|
||||
} catch(Exception $e) {
|
||||
$this->assertTrue(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,17 +2,43 @@
|
||||
|
||||
namespace Tests\Unit\Listeners\SpeedtestFailedListener;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use App\Helpers\SettingsHelper;
|
||||
use App\Listeners\SpeedtestFailedListener;
|
||||
use App\Speedtest;
|
||||
use Exception;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use stdClass;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TelegramTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* A basic unit test example.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testExample()
|
||||
public function testTelegramFailedNotification()
|
||||
{
|
||||
SettingsHelper::set('speedtest_notifications', true);
|
||||
SettingsHelper::set('telegram_bot_token', env('TELEGRAM_BOT_TOKEN'));
|
||||
SettingsHelper::set('telegram_chat_id', env('TELEGRAM_CHAT_ID'));
|
||||
SettingsHelper::set('slack_webhook', false);
|
||||
|
||||
$l = new SpeedtestFailedListener();
|
||||
$test = Speedtest::create([ 'download' => 5, 'upload' => 5, 'ping' => 5, 'failed' => true ]);
|
||||
|
||||
$event = new stdClass();
|
||||
$event->speedtest = $test;
|
||||
|
||||
try {
|
||||
$l->handle($event);
|
||||
} catch(Exception $e) {
|
||||
$this->assertTrue(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,17 +2,39 @@
|
||||
|
||||
namespace Tests\Unit\Listeners\SpeedtestOverviewListener;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use App\Helpers\SettingsHelper;
|
||||
use App\Listeners\SpeedtestOverviewListener;
|
||||
use App\Speedtest;
|
||||
use Exception;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use stdClass;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SlackTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* A basic unit test example.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testExample()
|
||||
public function testSlackOverviewNotification()
|
||||
{
|
||||
SettingsHelper::set('speedtest_overview_notification', true);
|
||||
SettingsHelper::set('slack_webhook', env('SLACK_WEBHOOK'));
|
||||
|
||||
$l = new SpeedtestOverviewListener();
|
||||
|
||||
$event = new stdClass();
|
||||
|
||||
try {
|
||||
$l->handle($event);
|
||||
} catch(Exception $e) {
|
||||
$this->assertTrue(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,17 +2,43 @@
|
||||
|
||||
namespace Tests\Unit\Listeners\SpeedtestOverviewListener;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use App\Helpers\SettingsHelper;
|
||||
use App\Listeners\SpeedtestOverviewListener;
|
||||
use App\Speedtest;
|
||||
use Exception;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use stdClass;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TelegramTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* A basic unit test example.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testExample()
|
||||
public function testTelegramOverviewNotification()
|
||||
{
|
||||
SettingsHelper::set('speedtest_overview_notification', true);
|
||||
SettingsHelper::set('telegram_bot_token', env('TELEGRAM_BOT_TOKEN'));
|
||||
SettingsHelper::set('telegram_chat_id', env('TELEGRAM_CHAT_ID'));
|
||||
SettingsHelper::set('slack_webhook', false);
|
||||
|
||||
$l = new SpeedtestOverviewListener();
|
||||
$test = Speedtest::create([ 'download' => 5, 'upload' => 5, 'ping' => 5, 'failed' => true ]);
|
||||
|
||||
$event = new stdClass();
|
||||
$event->speedtest = $test;
|
||||
|
||||
try {
|
||||
$l->handle($event);
|
||||
} catch(Exception $e) {
|
||||
$this->assertTrue(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
39
tests/Unit/Listeners/TestNotificationListener/SlackTest.php
Normal file
39
tests/Unit/Listeners/TestNotificationListener/SlackTest.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Listeners\TestNotificationListener;
|
||||
|
||||
use App\Events\TestNotificationEvent;
|
||||
use App\Helpers\SettingsHelper;
|
||||
use App\Listeners\TestNotificationListener;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use stdClass;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SlackTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* A basic unit test example.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSlackTest()
|
||||
{
|
||||
SettingsHelper::set('slack_webhook', env('SLACK_WEBHOOK'));
|
||||
|
||||
$l = new TestNotificationListener();
|
||||
|
||||
$event = new stdClass();
|
||||
$event->agents = [ 'slack' ];
|
||||
|
||||
try {
|
||||
$l->handle($event);
|
||||
} catch(Exception $e) {
|
||||
$this->assertTrue(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Listeners\TestNotificationListener;
|
||||
|
||||
use App\Helpers\SettingsHelper;
|
||||
use App\Listeners\TestNotificationListener;
|
||||
use Exception;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use stdClass;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TelegramTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* A basic unit test example.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testTelegramTest()
|
||||
{
|
||||
SettingsHelper::set('telegram_bot_token', env('TELEGRAM_BOT_TOKEN'));
|
||||
SettingsHelper::set('telegram_chat_id', env('TELEGRAM_CHAT_ID'));
|
||||
SettingsHelper::set('slack_webhook', false);
|
||||
|
||||
$l = new TestNotificationListener();
|
||||
|
||||
$event = new stdClass();
|
||||
$event->agents = [ 'telegram' ];
|
||||
|
||||
try {
|
||||
$l->handle($event);
|
||||
} catch(Exception $e) {
|
||||
$this->assertTrue(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user