Skip to content

Commit

Permalink
RatingList: keep upper code yourself
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Jan 14, 2023
1 parent daec714 commit 8bf59a3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 30 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ parameters:
path: src/RatingList/RatingListLongProcess.php

-
message: "#^Expression \"\\$ratingList\\['qwe'\\]\" on a separate line does not do anything\\.$#"
message: "#^Expression \"\\$ratingList\\['QWE'\\]\" on a separate line does not do anything\\.$#"
count: 1
path: tests/src/RatingList/RatingListTest.php

Expand Down
17 changes: 5 additions & 12 deletions src/RatingList/RatingList.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function setTTL(int $ttl): void

public function addProperty(Property $property): void
{
$this->currencies[self::normalizeKey($property->code)] = $property;
$this->currencies[$property->code] = $property;
}


Expand All @@ -67,20 +67,19 @@ public function offsetExists($offset): bool
{
assert($this->currencies !== []);

return isset($this->currencies[self::normalizeKey($offset)]);
return isset($this->currencies[$offset]);
}


public function offsetGet($offset): Property
{
assert($this->currencies !== []);
$key = self::normalizeKey($offset);

if (!isset($this->currencies[$key])) {
throw new UnknownCurrencyException($key);
if (!isset($this->currencies[$offset])) {
throw new UnknownCurrencyException($offset);
}

return $this->currencies[$key];
return $this->currencies[$offset];
}


Expand Down Expand Up @@ -130,10 +129,4 @@ public function rewind(): void
reset($this->currencies);
}


private static function normalizeKey(string $key): string
{
return strtolower($key);
}

}
8 changes: 4 additions & 4 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ function createExchangeFactory(string $driver = 'cnb'): Exchange\ExchangeFactory
{
$httpFactory = new Exchange\Fixtures\HttpFactory($driver);

$exchangeFactory = new Exchange\ExchangeFactory('eur', null, __DIR__ . '/temp/exchange', [
'czk',
'usd',
'eur',
$exchangeFactory = new Exchange\ExchangeFactory('EUR', null, __DIR__ . '/temp/exchange', [
'CZK',
'USD',
'EUR',
]);
$exchangeFactory->setClient($httpFactory);
$exchangeFactory->setRequestFactory($httpFactory);
Expand Down
10 changes: 5 additions & 5 deletions tests/src/ExchangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
Assert::same($exchange->getOutput(), $exchange->getDefault());

Assert::same(100.0, $exchange->change(100));
Assert::same(25.0, $exchange->change(1, 'eur', 'czk'));
Assert::same(80.0, $exchange->change(100, 'usd', 'eur'));
Assert::same(25.0, $exchange->change(1, 'EUR', 'CZK'));
Assert::same(80.0, $exchange->change(100, 'USD', 'EUR'));

Assert::same(125.0, $exchange->change(100, null, 'usd'));
Assert::same(80.0, $exchange->change(100, 'usd'));
Assert::same(125.0, $exchange->change(100, null, 'USD'));
Assert::same(80.0, $exchange->change(100, 'USD'));

$result = $exchange->transfer(100, 'usd');
$result = $exchange->transfer(100, 'USD');
Assert::same(80.0, $result[0]);
Assert::type(Exchange\Driver\Cnb\Property::class, $result[1]);
Assert::same('EUR', $result[1]->code);
Expand Down
16 changes: 8 additions & 8 deletions tests/src/RatingList/RatingListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,35 @@
$exchange = createExchangeFactory()->create();
$ratingList = $exchange->getIterator();

Assert::same(20.0, $ratingList['usd']->rate);
Assert::same(25.0, $ratingList['eur']->rate);
Assert::same(20.0, $ratingList['USD']->rate);
Assert::same(25.0, $ratingList['EUR']->rate);

Assert::exception(function () use ($ratingList) {
$ratingList->offsetSet('xxx', new Exchange\Currency\Property(
$ratingList->offsetSet('XXX', new Exchange\Currency\Property(
1,
1,
'XXX',
));
}, Exceptions\FrozenMethodException::class);

Assert::exception(function () use ($ratingList) {
$ratingList->offsetUnset('xxx');
$ratingList->offsetUnset('XXX');
}, Exceptions\FrozenMethodException::class);

Assert::exception(function () use ($ratingList) {
$ratingList['qwe'];
$ratingList['QWE'];
}, Exchange\Exceptions\UnknownCurrencyException::class);

Assert::type(Exchange\Currency\Property::class, $ratingList->offsetGet('czk'));
Assert::type(Exchange\Currency\Property::class, $ratingList->offsetGet('CZK'));

Assert::exception(function () use ($ratingList) {
$ratingList['czk'] = new Exchange\Currency\Property(
$ratingList['CZK'] = new Exchange\Currency\Property(
1,
1,
'XXX',
);
}, Exceptions\FrozenMethodException::class);

Assert::exception(function () use ($ratingList) {
unset($ratingList['czk']);
unset($ratingList['CZK']);
}, Exceptions\FrozenMethodException::class);

0 comments on commit 8bf59a3

Please sign in to comment.