Refactor all of the unit conversion code
This commit is contained in:
@@ -47,22 +47,21 @@ class MetarTest extends TestCase
|
||||
$this->assertEquals(280, $parsed['wind_direction']);
|
||||
$this->assertEquals('W', $parsed['wind_direction_label']);
|
||||
$this->assertEquals(false, $parsed['wind_direction_varies']);
|
||||
$this->assertEquals(16093, $parsed['visibility']);
|
||||
$this->assertEquals('16093 meters', $parsed['visibility_report']);
|
||||
$this->assertEquals(16093.44, $parsed['visibility']['m']);
|
||||
$this->assertEquals('Dry', $parsed['present_weather_report']);
|
||||
|
||||
$this->assertCount(4, $parsed['clouds']);
|
||||
$this->assertEquals(
|
||||
'A few at 1676 meters; scattered at 2896 meters; broken sky at 3353 meters; broken sky at 7010 meters',
|
||||
$parsed['clouds_report']);
|
||||
$this->assertEquals(1676, $parsed['cloud_height']);
|
||||
$this->assertEquals(1676.4, $parsed['cloud_height']['m']);
|
||||
$this->assertEquals(false, $parsed['cavok']);
|
||||
|
||||
$this->assertEquals(12, $parsed['temperature']);
|
||||
$this->assertEquals(54, $parsed['temperature_f']);
|
||||
$this->assertEquals(12, $parsed['temperature']['c']);
|
||||
$this->assertEquals(53.6, $parsed['temperature']['f']);
|
||||
|
||||
$this->assertEquals(-4, $parsed['dew_point']);
|
||||
$this->assertEquals(25, $parsed['dew_point_f']);
|
||||
$this->assertEquals(-4, $parsed['dew_point']['c']);
|
||||
$this->assertEquals(24.8, $parsed['dew_point']['f']);
|
||||
|
||||
$this->assertEquals(33, $parsed['humidity']);
|
||||
$this->assertEquals(29.58, $parsed['barometer']);
|
||||
|
||||
@@ -132,15 +132,15 @@ class PIREPTest extends TestCase
|
||||
|
||||
// Check that it has the fuel units
|
||||
$this->assertHasKeys($body['fuel_used'], ['lbs', 'kg']);
|
||||
$this->assertEquals($pirep->fuel_used->toNumber(), $body['fuel_used']['lbs']);
|
||||
$this->assertEquals($pirep->fuel_used['lbs'], $body['fuel_used']['lbs']);
|
||||
|
||||
// Check that it has the distance units
|
||||
$this->assertHasKeys($body['distance'], ['km', 'nmi', 'mi']);
|
||||
$this->assertEquals($pirep->distance->toNumber(), $body['distance']['nmi']);
|
||||
$this->assertEquals($pirep->distance['nmi'], $body['distance']['nmi']);
|
||||
|
||||
// Check the planned_distance field
|
||||
$this->assertHasKeys($body['planned_distance'], ['km', 'nmi', 'mi']);
|
||||
$this->assertEquals($pirep->planned_distance->toNumber(), $body['planned_distance']['nmi']);
|
||||
$this->assertEquals($pirep->planned_distance['nmi'], $body['planned_distance']['nmi']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -110,6 +110,23 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
|
||||
return $method->invokeArgs($object, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform any data that's passed in. E.g, make sure that any mutator
|
||||
* classes (e.g, units) are not passed in as the mutator class
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
protected function transformData(array $data): array
|
||||
{
|
||||
foreach($data as $key => $value) {
|
||||
if (is_subclass_of($value, App\Interfaces\Unit::class)) {
|
||||
$data[$key] = $value->__toString();
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the GET call to inject the user API key
|
||||
* @param string $uri
|
||||
@@ -137,6 +154,7 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
|
||||
*/
|
||||
public function post($uri, array $data = [], array $headers = [], $user=null)
|
||||
{
|
||||
$data = $this->transformData($data);
|
||||
$req = parent::post($uri, $data, $this->headers($user, $headers));
|
||||
if ($req->isClientError() || $req->isServerError()) {
|
||||
Log::error('POST Error: ' . $uri, $req->json());
|
||||
@@ -155,7 +173,7 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
|
||||
*/
|
||||
public function put($uri, array $data = [], array $headers = [], $user = null)
|
||||
{
|
||||
$req = parent::put($uri, $data, $this->headers($user, $headers));
|
||||
$req = parent::put($uri, $this->transformData($data), $this->headers($user, $headers));
|
||||
if ($req->isClientError() || $req->isServerError()) {
|
||||
Log::error('PUT Error: ' . $uri, $req->json());
|
||||
}
|
||||
@@ -173,7 +191,7 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
|
||||
*/
|
||||
public function delete($uri, array $data = [], array $headers = [], $user=null)
|
||||
{
|
||||
$req = parent::delete($uri, $data, $this->headers($user, $headers));
|
||||
$req = parent::delete($uri, $this->transformData($data), $this->headers($user, $headers));
|
||||
if ($req->isClientError() || $req->isServerError()) {
|
||||
Log::error('DELETE Error: ' . $uri, $req->json());
|
||||
}
|
||||
|
||||
@@ -21,9 +21,11 @@ trait TestData
|
||||
]);
|
||||
|
||||
// Return a Pirep model
|
||||
return factory(\App\Models\Pirep::class)->make([
|
||||
$pirep = factory(\App\Models\Pirep::class)->make([
|
||||
'aircraft_id' => $subfleet['aircraft']->random()->id
|
||||
]);
|
||||
|
||||
return $pirep;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user