From 62eeeee9fee032dea7afa206e2c94943682a0fd3 Mon Sep 17 00:00:00 2001 From: Henry Whitaker Date: Tue, 14 Apr 2020 00:05:29 +0100 Subject: [PATCH] Added some tests --- tests/Feature/APISpeedtestTest.php | 80 +++++++++++++++++++ .../{ExampleTest.php => BackupTest.php} | 7 +- tests/Feature/SpeedtestTest.php | 28 +++++++ tests/Unit/ExampleTest.php | 18 ----- 4 files changed, 112 insertions(+), 21 deletions(-) create mode 100644 tests/Feature/APISpeedtestTest.php rename tests/Feature/{ExampleTest.php => BackupTest.php} (61%) create mode 100644 tests/Feature/SpeedtestTest.php delete mode 100644 tests/Unit/ExampleTest.php diff --git a/tests/Feature/APISpeedtestTest.php b/tests/Feature/APISpeedtestTest.php new file mode 100644 index 00000000..a1e34432 --- /dev/null +++ b/tests/Feature/APISpeedtestTest.php @@ -0,0 +1,80 @@ +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', + ], + ]); + } +} diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/BackupTest.php similarity index 61% rename from tests/Feature/ExampleTest.php rename to tests/Feature/BackupTest.php index cdb51119..fa1ae6c6 100644 --- a/tests/Feature/ExampleTest.php +++ b/tests/Feature/BackupTest.php @@ -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('/'); diff --git a/tests/Feature/SpeedtestTest.php b/tests/Feature/SpeedtestTest.php new file mode 100644 index 00000000..4efc6e17 --- /dev/null +++ b/tests/Feature/SpeedtestTest.php @@ -0,0 +1,28 @@ +assertArrayHasKey('ping', $data); + $this->assertArrayHasKey('download', $data); + $this->assertArrayHasKey('upload', $data); + } +} diff --git a/tests/Unit/ExampleTest.php b/tests/Unit/ExampleTest.php deleted file mode 100644 index 358cfc88..00000000 --- a/tests/Unit/ExampleTest.php +++ /dev/null @@ -1,18 +0,0 @@ -assertTrue(true); - } -}