Skip to content

Commit

Permalink
Fixes up some issues with the last PR. Unit tests are now passing again.
Browse files Browse the repository at this point in the history
  • Loading branch information
allebb committed Apr 5, 2023
1 parent 03e1ccb commit 6acaa38
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 12 deletions.
21 changes: 17 additions & 4 deletions lib/Metar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Ballen\Metar;

use Ballen\Metar\Providers\MetarProviderInterface;

/**
* Metar
*
Expand Down Expand Up @@ -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();
}

Expand All @@ -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
*/
Expand All @@ -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);
}

}
5 changes: 5 additions & 0 deletions lib/Providers/Ivao.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public function __toString(): string
{
return $this->getMetarDataString();
}

public function raw(): string
{
return $this->__toString();
}
}
2 changes: 2 additions & 0 deletions lib/Providers/MetarProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ interface MetarProviderInterface
public function __construct(string $icao);

public function __toString();

public function raw() : string;
}
8 changes: 7 additions & 1 deletion lib/Providers/Noaa.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* @link http://www.bobbyallen.me
*
*/

use \Ballen\Metar\Helpers\MetarHTTPClient;

/**
Expand All @@ -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]);
}
Expand All @@ -48,4 +49,9 @@ public function __toString()
{
return $this->getMetarDataString();
}

public function raw(): string
{
return $this->__toString();
}
}
6 changes: 6 additions & 0 deletions lib/Providers/Vatsim.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* @link http://www.bobbyallen.me
*
*/

use \Ballen\Metar\Helpers\MetarHTTPClient;

/**
Expand Down Expand Up @@ -44,4 +45,9 @@ public function __toString()
{
return $this->getMetarDataString();
}

public function raw(): string
{
return $this->__toString();
}
}
2 changes: 1 addition & 1 deletion tests/MetarHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 8 additions & 6 deletions tests/MetarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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');
}

Expand All @@ -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');
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}
}

0 comments on commit 6acaa38

Please sign in to comment.