Skip to content

Commit

Permalink
throw exception when rates file contains content that could not be un…
Browse files Browse the repository at this point in the history
…serialized
  • Loading branch information
dannyvankooten committed Sep 6, 2023
1 parent e218a03 commit 866b651
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jobs:

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v2"

- name: "Run PHPUnit"
run: "vendor/bin/phpunit"

6 changes: 3 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
beStrictAboutOutputDuringTests="true"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
convertErrorsToExceptions="false"
convertNoticesToExceptions="false"
convertWarningsToExceptions="false"
failOnRisky="true"
failOnWarning="true"
processIsolation="false"
Expand Down
6 changes: 4 additions & 2 deletions src/Rates.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ private function loadFromFile()
]
]);

if (is_array($data)) {
$this->rates = $data;
if (false === is_array($data)) {
throw new Exception("Unserializable file content");
}

$this->rates = $data;
}

private function loadFromRemote()
Expand Down
2 changes: 1 addition & 1 deletion tests/RatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function testRatesAreLoadedFromFile()
// test by invalidating file and testing for exception
file_put_contents('vendor/rates', 'foobar');
$rates = new Rates('vendor/rates', 30, $client);
$this->expectError(Error::class);
$this->expectException(Exception::class);
$this->assertEquals(21.0, $rates->getRateForCountry('NL'));
}

Expand Down

0 comments on commit 866b651

Please sign in to comment.