Skip to content

Commit

Permalink
New methods (#33)
Browse files Browse the repository at this point in the history
* Update psalm version

* Add overnight method

* Add php 8.2
  • Loading branch information
liquetsoft authored Oct 5, 2023
1 parent 1ab8dc3 commit 32eb34e
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php-versions: ['8.1']
php-versions: ['8.1', '8.2']

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

$rules = [
'@Symfony' => true,
'new_with_braces' => true,
'new_with_parentheses' => true,
'concat_space' => ['spacing' => 'one'],
'array_syntax' => ['syntax' => 'short'],
'yoda_style' => false,
Expand Down
7 changes: 4 additions & 3 deletions docker/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ RUN set -xe && apk update && apk add --no-cache \
git \
autoconf \
g++ \
make
make \
linux-headers


RUN docker-php-ext-install zip soap \
RUN docker-php-ext-install soap zip opcache \
&& docker-php-source extract \
&& pecl install xdebug \
&& docker-php-ext-enable xdebug \
Expand All @@ -24,7 +25,7 @@ RUN docker-php-ext-install zip soap \
&& echo 'xdebug.mode=coverage' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini


RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --version=2.4.4 \
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --version=2.5.8 \
&& mkdir -p /.composer && chmod -Rf 777 /.composer


Expand Down
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
cacheDirectory="/tmp"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
findUnusedBaselineEntry="false"
findUnusedCode="false"
xsi:schemaLocation="https://getpsalm.org/schema/config file:///var/www/vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
Expand Down
21 changes: 21 additions & 0 deletions src/CbrfDaily.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Liquetsoft\CbrfService\Entity\Mkr;
use Liquetsoft\CbrfService\Entity\OstatDepoRate;
use Liquetsoft\CbrfService\Entity\OstatRate;
use Liquetsoft\CbrfService\Entity\OvernightRate;
use Liquetsoft\CbrfService\Entity\PreciousMetalRate;
use Liquetsoft\CbrfService\Entity\RepoDebt;
use Liquetsoft\CbrfService\Entity\ReutersCurrency;
Expand Down Expand Up @@ -564,4 +565,24 @@ public function getReutersCursOnDate(\DateTimeInterface $date): array

return $results;
}

/**
* Returns prices for coins.
*
* @return OvernightRate[]
*
* @throws CbrfException
*/
public function overnight(\DateTimeInterface $from, \DateTimeInterface $to): array
{
$res = $this->transport->query(
'Overnight',
[
'fromDate' => $from,
'ToDate' => $to,
]
);

return DataHelper::arrayOfItems('Overnight.OB', $res, OvernightRate::class);
}
}
2 changes: 1 addition & 1 deletion src/CbrfFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private function __construct()
/**
* Creates and returns new CbrfDaily object.
*/
public static function createDaily(?\SoapClient $soap = null): CbrfDaily
public static function createDaily(\SoapClient $soap = null): CbrfDaily
{
$transport = new CbrfSoapTransport($soap);

Expand Down
4 changes: 2 additions & 2 deletions src/CbrfSoapTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ final class CbrfSoapTransport implements CbrfTransport

private ?\SoapClient $client;

public function __construct(?\SoapClient $client = null)
public function __construct(\SoapClient $client = null)
{
$this->client = $client;
}

/**
* {@inheritDoc}
*/
public function query(string $method, ?array $params = null): array
public function query(string $method, array $params = null): array
{
$params = $params ?: [];

Expand Down
2 changes: 1 addition & 1 deletion src/CbrfTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ interface CbrfTransport
*
* @throws CbrfTransportException
*/
public function query(string $method, ?array $params = null): array;
public function query(string $method, array $params = null): array;
}
8 changes: 4 additions & 4 deletions src/DataHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static function enumInt(string $path, array $data, string $enumClass): ob
/**
* Returns string from the set path.
*/
public static function string(string $path, array $data, ?string $default = null): string
public static function string(string $path, array $data, string $default = null): string
{
$item = self::get($path, $data);

Expand All @@ -148,7 +148,7 @@ public static function string(string $path, array $data, ?string $default = null
/**
* Returns float from the set path.
*/
public static function float(string $path, array $data, ?float $default = null): float
public static function float(string $path, array $data, float $default = null): float
{
$item = self::get($path, $data);

Expand Down Expand Up @@ -184,7 +184,7 @@ public static function floatOrNull(string $path, array $data): ?float
/**
* Returns int from the set path.
*/
public static function int(string $path, array $data, ?int $default = null): int
public static function int(string $path, array $data, int $default = null): int
{
$item = self::get($path, $data);

Expand All @@ -204,7 +204,7 @@ public static function int(string $path, array $data, ?int $default = null): int
/**
* Returns char code from the set path.
*/
public static function charCode(string $path, array $data, ?string $default = null): string
public static function charCode(string $path, array $data, string $default = null): string
{
$item = self::get($path, $data);

Expand Down
36 changes: 36 additions & 0 deletions src/Entity/OvernightRate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Liquetsoft\CbrfService\Entity;

use Liquetsoft\CbrfService\CbrfEntityRate;
use Liquetsoft\CbrfService\DataHelper;

/**
* DTO that represents response item from Overnight method.
*
* @psalm-immutable
*/
final class OvernightRate implements CbrfEntityRate
{
private readonly \DateTimeInterface $date;

private readonly float $rate;

public function __construct(array $item)
{
$this->date = DataHelper::dateTime('date', $item);
$this->rate = DataHelper::float('stavka', $item, .0);
}

public function getRate(): float
{
return $this->rate;
}

public function getDate(): \DateTimeInterface
{
return $this->date;
}
}
2 changes: 1 addition & 1 deletion src/Exception/CbrfDataAccessException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
final class CbrfDataAccessException extends CbrfException
{
public function __construct(string $path = '', string $type = '', ?\Throwable $previous = null)
public function __construct(string $path = '', string $type = '', \Throwable $previous = null)
{
if (!empty($path) && !empty($type)) {
$message = sprintf("Can't find '%s' value at '%s'", $type, $path);
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/CbrfDataConvertException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
final class CbrfDataConvertException extends CbrfException
{
public function __construct(string $from, string $to, ?\Throwable $previous = null)
public function __construct(string $from, string $to, \Throwable $previous = null)
{
if ($previous) {
$message = sprintf(
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/CbrfTransportException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class CbrfTransportException extends CbrfException

private readonly array $params;

public function __construct(string $method, array $params = [], ?\Throwable $previous = null)
public function __construct(string $method, array $params = [], \Throwable $previous = null)
{
$this->method = $method;
$this->params = $params;
Expand Down
37 changes: 37 additions & 0 deletions tests/CbrfDailyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Liquetsoft\CbrfService\Entity\Mkr;
use Liquetsoft\CbrfService\Entity\OstatDepoRate;
use Liquetsoft\CbrfService\Entity\OstatRate;
use Liquetsoft\CbrfService\Entity\OvernightRate;
use Liquetsoft\CbrfService\Entity\PreciousMetalRate;
use Liquetsoft\CbrfService\Entity\RepoDebt;
use Liquetsoft\CbrfService\Entity\ReutersCurrency;
Expand Down Expand Up @@ -203,6 +204,13 @@ class CbrfDailyTest extends BaseTestCase
],
'path' => 'ReutersValutesData.Currency',
],
'Overnight' => [
'schema' => [
'stavka' => self::FIXTURE_TYPE_FLOAT,
'date' => self::FIXTURE_TYPE_DATE,
],
'path' => 'Overnight.OB',
],
];

/**
Expand Down Expand Up @@ -986,4 +994,33 @@ public function testGetReutersCursOnDate(): void
$this->assertSameDate($onDate, $list[$key]->getDate());
}
}

/**
* @test
*/
public function testOvernight(): void
{
[$rates, $response] = $this->createFixture(self::FIXTURES['Overnight']);
$from = new \DateTimeImmutable('-1 month');
$to = new \DateTimeImmutable();

$soapClient = $this->createTransportMock(
'Overnight',
[
'fromDate' => $from,
'ToDate' => $to,
],
$response
);

$service = new CbrfDaily($soapClient);
$list = $service->overnight($from, $to);

$this->assertCount(\count($rates), $list);
$this->assertContainsOnlyInstancesOf(OvernightRate::class, $list);
foreach ($rates as $key => $rate) {
$this->assertSame($rate['date'], $list[$key]->getDate()->format('Y-m-d'));
$this->assertSame($rate['stavka'], $list[$key]->getRate());
}
}
}
8 changes: 4 additions & 4 deletions tests/DataHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public function dateTimeProvider(): array
*
* @dataProvider stringProvider
*/
public function testString(string $path, array $input, string|\Exception $result, ?string $default = null): void
public function testString(string $path, array $input, string|\Exception $result, string $default = null): void
{
if ($result instanceof \Exception) {
$this->expectExceptionObject($result);
Expand Down Expand Up @@ -387,7 +387,7 @@ public function stringProvider(): array
*
* @dataProvider floatProvider
*/
public function testFloat(string $path, array $input, float|\Exception $result, ?float $default = null): void
public function testFloat(string $path, array $input, float|\Exception $result, float $default = null): void
{
if ($result instanceof \Exception) {
$this->expectExceptionObject($result);
Expand Down Expand Up @@ -505,7 +505,7 @@ public function floatOrNullProvider(): array
*
* @dataProvider intProvider
*/
public function testInt(string $path, array $input, int|\Exception $result, ?int $default = null): void
public function testInt(string $path, array $input, int|\Exception $result, int $default = null): void
{
if ($result instanceof \Exception) {
$this->expectExceptionObject($result);
Expand Down Expand Up @@ -570,7 +570,7 @@ public function intProvider(): array
*
* @dataProvider charCodeProvider
*/
public function testCharCode(string $path, array $input, string|\Exception $result, ?string $default = null): void
public function testCharCode(string $path, array $input, string|\Exception $result, string $default = null): void
{
if ($result instanceof \Exception) {
$this->expectExceptionObject($result);
Expand Down

0 comments on commit 32eb34e

Please sign in to comment.