From 6acaa38a99e80fb1a0009e49997ef573e3a434ee Mon Sep 17 00:00:00 2001 From: Bobby Allen Date: Wed, 5 Apr 2023 14:56:27 +0100 Subject: [PATCH] Fixes up some issues with the last PR. Unit tests are now passing again. --- lib/Metar.php | 21 +++++++++++++++++---- lib/Providers/Ivao.php | 5 +++++ lib/Providers/MetarProviderInterface.php | 2 ++ lib/Providers/Noaa.php | 8 +++++++- lib/Providers/Vatsim.php | 6 ++++++ tests/MetarHttpClientTest.php | 2 +- tests/MetarTest.php | 14 ++++++++------ 7 files changed, 46 insertions(+), 12 deletions(-) diff --git a/lib/Metar.php b/lib/Metar.php index e036e18..acca6c8 100644 --- a/lib/Metar.php +++ b/lib/Metar.php @@ -4,6 +4,8 @@ namespace Ballen\Metar; +use Ballen\Metar\Providers\MetarProviderInterface; + /** * Metar * @@ -55,7 +57,7 @@ public function __construct(string $icao) // Validate the ICAO code, just check some standard formatting stuff really! $this->validateIcao($this->icao); - // Set a default provider, can be overrideen with 'setProvider()' function. + // Set a default provider, can be overridden with 'setProvider()' function. $this->setProvider(); } @@ -68,11 +70,11 @@ public function __toString() // We'll set the object 'metar' property to the station metar data as well as return the string so // that in future we can extend further and use the METAR string in other parts of our class after it has // been retrieved! - return $this->metar = (string) new $this->metarProvider($this->icao); + return $this->metar = (string)new $this->metarProvider($this->icao); } /** - * Validates the ICAO code to ensure it's format is valid. + * Validates the ICAO code to ensure its format is valid. * @param string $icao ICAO code, eg. EGSS * @throws \InvalidArgumentException */ @@ -86,12 +88,23 @@ private function validateIcao(string $icao): void /** * Changes the default 'NOAA' METAR service provider to another one eg. 'VATSIM'. * @param string $provider METAR Provider Class/File name. + * @return void */ - public function setProvider(string $provider = self::DEFAULT_PROVIDER): self + public function setProvider(string $provider = self::DEFAULT_PROVIDER): void { if (!class_exists($provider)) { throw new \InvalidArgumentException('The service provider your specified does not exist in the namespace \'' . $provider . '\''); } $this->metarProvider = $provider; } + + /** + * Returns the METAR report (provider) object. + * @return MetarProviderInterface + */ + public function report(): MetarProviderInterface + { + return new $this->metarProvider($this->icao); + } + } diff --git a/lib/Providers/Ivao.php b/lib/Providers/Ivao.php index 0177bcc..856d0a5 100644 --- a/lib/Providers/Ivao.php +++ b/lib/Providers/Ivao.php @@ -42,4 +42,9 @@ public function __toString(): string { return $this->getMetarDataString(); } + + public function raw(): string + { + return $this->__toString(); + } } diff --git a/lib/Providers/MetarProviderInterface.php b/lib/Providers/MetarProviderInterface.php index 03971d9..7bae1e3 100644 --- a/lib/Providers/MetarProviderInterface.php +++ b/lib/Providers/MetarProviderInterface.php @@ -21,4 +21,6 @@ interface MetarProviderInterface public function __construct(string $icao); public function __toString(); + + public function raw() : string; } diff --git a/lib/Providers/Noaa.php b/lib/Providers/Noaa.php index ac69cff..39c9c2e 100644 --- a/lib/Providers/Noaa.php +++ b/lib/Providers/Noaa.php @@ -16,6 +16,7 @@ * @link http://www.bobbyallen.me * */ + use \Ballen\Metar\Helpers\MetarHTTPClient; /** @@ -39,7 +40,7 @@ private function getMetarDataString(): string $data = $this->getMetarAPIResponse(str_replace('{{_ICAO_}}', $this->icao, $this->serviceUrl)); - // The NOAA web service provides a human readable timestamp of when the report was last generated but we don't care about that so we'll jump to the next line (the actual METAR string) + // The NOAA web service provides a human-readable timestamp of when the report was last generated but we don't care about that so we'll jump to the next line (the actual METAR string) $lines = explode($this->icao, $data); return trim($this->icao . $lines[1]); } @@ -48,4 +49,9 @@ public function __toString() { return $this->getMetarDataString(); } + + public function raw(): string + { + return $this->__toString(); + } } diff --git a/lib/Providers/Vatsim.php b/lib/Providers/Vatsim.php index c229d84..b95b574 100644 --- a/lib/Providers/Vatsim.php +++ b/lib/Providers/Vatsim.php @@ -16,6 +16,7 @@ * @link http://www.bobbyallen.me * */ + use \Ballen\Metar\Helpers\MetarHTTPClient; /** @@ -44,4 +45,9 @@ public function __toString() { return $this->getMetarDataString(); } + + public function raw(): string + { + return $this->__toString(); + } } diff --git a/tests/MetarHttpClientTest.php b/tests/MetarHttpClientTest.php index 49cbe82..04c3c50 100644 --- a/tests/MetarHttpClientTest.php +++ b/tests/MetarHttpClientTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -use G0uzzleHttp\Handler\MockHandler; +use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Response; use PHPUnit\Framework\TestCase; diff --git a/tests/MetarTest.php b/tests/MetarTest.php index 26ba514..31696f0 100644 --- a/tests/MetarTest.php +++ b/tests/MetarTest.php @@ -21,7 +21,7 @@ class MetarTest extends TestCase { /** - * Test requesting of a valid ICAO code (does not thrown an invalid formation exception) + * Test requesting of a valid ICAO code (does not throw an invalid formation exception) */ public function testSetValidIcao() { @@ -34,7 +34,8 @@ public function testSetValidIcao() */ public function testSetInvalidIcao() { - $this->expectException('InvalidArgumentException', 'ICAO code does not appear to be a valid format'); + $this->expectException('InvalidArgumentException'); + $this->expectExceptionMessage('ICAO code does not appear to be a valid format'); $metar = new Metar('EGSSA'); } @@ -54,7 +55,8 @@ public function testSetValidProvider() public function testSetInvalidProvider() { $metar = new Metar('EGSS'); - $this->expectException('InvalidArgumentException', 'The service provider your specified does not exist in the namespace \'An_Invalid_Provider\''); + $this->expectException('InvalidArgumentException'); + $this->expectExceptionMessage('The service provider your specified does not exist in the namespace \'An_Invalid_Provider\''); $metar->setProvider('An_Invalid_Provider'); } @@ -64,7 +66,7 @@ public function testSetInvalidProvider() public function testValidNoaaMetarResponse() { $metar = new Metar('EGSS'); - $check_valid_metar = strpos($metar, 'EGSS'); + $check_valid_metar = strpos((string)$metar, 'EGSS'); $this->assertEquals($check_valid_metar, 0); } @@ -75,7 +77,7 @@ public function testValidVatsimMetarResponse() { $metar = new Metar('EGSS'); $metar->setProvider(Ballen\Metar\Providers\Vatsim::class); - $check_valid_metar = strpos($metar, 'EGSS'); + $check_valid_metar = strpos((string)$metar, 'EGSS'); $this->assertEquals($check_valid_metar, 0); } @@ -86,7 +88,7 @@ public function testValidIvaoMetarResponse() { $metar = new Metar('EGSS'); $metar->setProvider(Ballen\Metar\Providers\Ivao::class); - $check_valid_metar = strpos($metar, 'EGSS'); + $check_valid_metar = strpos($metar->report()->raw(), 'EGSS'); $this->assertEquals($check_valid_metar, 0); } }