Added some tests

This commit is contained in:
Henry Whitaker
2020-04-14 00:05:29 +01:00
parent 50ed4ab894
commit 62eeeee9fe
4 changed files with 112 additions and 21 deletions

View File

@@ -0,0 +1,80 @@
<?php
namespace Tests\Feature;
use App\Speedtest;
use Faker\Factory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class APISpeedtestTest extends TestCase
{
use RefreshDatabase;
/**
* Gets latest speedtest values
*
* @test
* @return void
*/
public function getLatestSpeedtest()
{
$faker = Factory::create();
$ping = [];
$dl = [];
$ul = [];
for($i = 0; $i < 3; $i++) {
$pingVal = $faker->randomFloat();
array_push($ping,$pingVal);
$dlVal = $faker->randomFloat();
array_push($dl,$dlVal);
$ulVal = $faker->randomFloat();
array_push($ul,$ulVal);
Speedtest::create([
'ping' => $pingVal,
'download' => $dlVal,
'upload' => $ulVal,
]);
}
$avgVals = Speedtest::select(DB::raw('AVG(ping) as ping, AVG(download) as download, AVG(upload) as upload'))->get()[0];
$maxVals = Speedtest::select(DB::raw('MAX(ping) as ping, MAX(download) as download, MAX(upload) as upload'))->get()[0];
$pingAvg = $avgVals['ping'];
$dlAvg = $avgVals['download'];
$ulAvg = $avgVals['upload'];
$pingMax = $maxVals['ping'];
$dlMax = $maxVals['download'];
$ulMax = $maxVals['upload'];
$response = $this->get('/api/speedtest/latest');
$response->assertStatus(200);
$response->assertJsonStructure([
'method',
'data' => [
'id',
'ping',
'download',
'upload',
'created_at',
'updated_at',
],
'average' => [
'ping',
'download',
'upload',
],
'max' => [
'ping',
'download',
'upload',
],
]);
}
}

View File

@@ -3,16 +3,17 @@
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class ExampleTest extends TestCase
class BackupTest extends TestCase
{
/**
* A basic test example.
* A basic feature test example.
*
* @return void
*/
public function testBasicTest()
public function testExample()
{
$response = $this->get('/');

View File

@@ -0,0 +1,28 @@
<?php
namespace Tests\Feature;
use App\Helpers\SpeedtestHelper;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class SpeedtestTest extends TestCase
{
use RefreshDatabase;
/**
* Runs a speedtest
*
* @test
* @return void
*/
public function runSpeedtest()
{
$data = SpeedtestHelper::runSpeedtest();
$this->assertArrayHasKey('ping', $data);
$this->assertArrayHasKey('download', $data);
$this->assertArrayHasKey('upload', $data);
}
}

View File

@@ -1,18 +0,0 @@
<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$this->assertTrue(true);
}
}