diff --git a/composer.json b/composer.json index 871d732..5fd13dd 100644 --- a/composer.json +++ b/composer.json @@ -27,13 +27,20 @@ "require-dev": { "phpunit/phpunit": "9.5.10", "squizlabs/php_codesniffer": "3.6.1", - "phpstan/phpstan": "^1.11" + "phpstan/phpstan": "1.11.5", + "phpstan/phpstan-phpunit": "1.4.0", + "phpstan/phpstan-strict-rules": "1.6.0" }, "autoload": { "psr-4": { "FioApi\\": "src/FioApi" } }, + "autoload-dev": { + "psr-4": { + "FioApi\\": "tests/FioApi" + } + }, "scripts": { "ci": [ "@phpstan", @@ -42,6 +49,6 @@ ], "test": "phpunit", "phpcs": "phpcs --standard=PSR2 src && phpcs --standard=PSR2 tests", - "phpstan": "phpstan analyse -l 8 src tests" + "phpstan": "phpstan analyse -c phpstan.neon" } } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..d5bb70e --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,10 @@ +includes: + - vendor/phpstan/phpstan-strict-rules/rules.neon + - vendor/phpstan/phpstan-phpunit/extension.neon + - vendor/phpstan/phpstan-phpunit/rules.neon + +parameters: + level: 8 + paths: + - src + - tests diff --git a/src/FioApi/Downloader.php b/src/FioApi/Downloader.php index 9964ec9..89682e3 100644 --- a/src/FioApi/Downloader.php +++ b/src/FioApi/Downloader.php @@ -66,21 +66,15 @@ public function setLastId(string $id): void } } - /** - * @param string $url - * @return TransactionList - */ private function downloadTransactionsList(string $url): TransactionList { $client = $this->getClient(); $transactions = null; try { - /** @var ?ResponseInterface $response */ $response = $client->request('get', $url); - if ($response) { - $transactions = json_decode($response->getBody()->getContents())->accountStatement; - } + $jsonData = json_decode($response->getBody()->getContents(), null, 512, JSON_THROW_ON_ERROR); + $transactions = $jsonData->accountStatement; } catch (BadResponseException $e) { $this->handleException($e); } diff --git a/src/FioApi/Transaction.php b/src/FioApi/Transaction.php index c60f2f5..18241b7 100644 --- a/src/FioApi/Transaction.php +++ b/src/FioApi/Transaction.php @@ -110,20 +110,20 @@ public static function create(\stdClass $data): Transaction new \DateTimeImmutable($data->column0->value), //Datum $data->column1->value, //Objem $data->column14->value, //Měna - !empty($data->column2) ? $data->column2->value : null, //Protiúčet - !empty($data->column3) ? $data->column3->value : null, //Kód banky - !empty($data->column12) ? $data->column12->value : null, //Název banky - !empty($data->column10) ? $data->column10->value : null, //Název protiúčtu - !empty($data->column4) ? $data->column4->value : null, //KS - !empty($data->column5) ? $data->column5->value : null, //VS - !empty($data->column6) ? $data->column6->value : null, //SS - !empty($data->column7) ? $data->column7->value : null, //Uživatelská identifikace - !empty($data->column16) ? $data->column16->value : null, //Zpráva pro příjemce - !empty($data->column8) ? $data->column8->value : '', //Typ - !empty($data->column9) ? $data->column9->value : null, //Provedl - !empty($data->column25) ? $data->column25->value : null, //Komentář - !empty($data->column17) ? $data->column17->value : null, //ID pokynu - !empty($data->column18) ? $data->column18->value : null //Upřesnění + $data->column2 !== null ? $data->column2->value : null, //Protiúčet + $data->column3 !== null ? $data->column3->value : null, //Kód banky + $data->column12 !== null ? $data->column12->value : null, //Název banky + $data->column10 !== null ? $data->column10->value : null, //Název protiúčtu + $data->column4 !== null ? $data->column4->value : null, //KS + $data->column5 !== null ? $data->column5->value : null, //VS + $data->column6 !== null ? $data->column6->value : null, //SS + $data->column7 !== null ? $data->column7->value : null, //Uživatelská identifikace + $data->column16 !== null ? $data->column16->value : null, //Zpráva pro příjemce + $data->column8 !== null ? $data->column8->value : '', //Typ + $data->column9 !== null ? $data->column9->value : null, //Provedl + $data->column25 !== null ? $data->column25->value : null, //Komentář + $data->column17 !== null ? $data->column17->value : null, //ID pokynu + $data->column18 !== null ? $data->column18->value : null //Upřesnění ); } diff --git a/src/FioApi/UrlBuilder.php b/src/FioApi/UrlBuilder.php index 666a7eb..d450d58 100644 --- a/src/FioApi/UrlBuilder.php +++ b/src/FioApi/UrlBuilder.php @@ -24,7 +24,7 @@ public function getToken(): string public function setToken(string $token): void { - if (!$token) { + if ($token === '') { throw new MissingTokenException( 'Token is required for ebanking API calls. You can get one at https://www.fio.cz/.' ); diff --git a/tests/FioApi/AccountTest.php b/tests/FioApi/AccountTest.php index 2dc810c..ba0f983 100644 --- a/tests/FioApi/AccountTest.php +++ b/tests/FioApi/AccountTest.php @@ -3,6 +3,8 @@ namespace FioApi; +use PHPUnit\Framework\Assert; + class AccountTest extends \PHPUnit\Framework\TestCase { public function testAccountValuesAreProperlySet(): void @@ -14,10 +16,10 @@ public function testAccountValuesAreProperlySet(): void 'CZ9820100000000214498596', 'FIOBCZPPXXX' ); - $this->assertSame('214498596', $account->getAccountNumber()); - $this->assertSame('2010', $account->getBankCode()); - $this->assertSame('CZK', $account->getCurrency()); - $this->assertSame('CZ9820100000000214498596', $account->getIban()); - $this->assertSame('FIOBCZPPXXX', $account->getBic()); + Assert::assertSame('214498596', $account->getAccountNumber()); + Assert::assertSame('2010', $account->getBankCode()); + Assert::assertSame('CZK', $account->getCurrency()); + Assert::assertSame('CZ9820100000000214498596', $account->getIban()); + Assert::assertSame('FIOBCZPPXXX', $account->getBic()); } } diff --git a/tests/FioApi/DownloaderTest.php b/tests/FioApi/DownloaderTest.php index 6879019..d07eb76 100644 --- a/tests/FioApi/DownloaderTest.php +++ b/tests/FioApi/DownloaderTest.php @@ -8,6 +8,7 @@ use GuzzleHttp\HandlerStack; use GuzzleHttp\Middleware; use GuzzleHttp\Psr7\Response; +use PHPUnit\Framework\Assert; class DownloaderTest extends \PHPUnit\Framework\TestCase { @@ -57,7 +58,7 @@ public function testDownloaderDownloadsData(): void $transactionList = $downloader->downloadSince(new \DateTimeImmutable('-1 week')); - $this->assertInstanceOf(TransactionList::class, $transactionList); + Assert::assertCount(3, $transactionList->getTransactions()); } public function testDownloaderDownloadsLast(): void @@ -69,7 +70,7 @@ public function testDownloaderDownloadsLast(): void $transactionList = $downloader->downloadLast(); - $this->assertInstanceOf(TransactionList::class, $transactionList); + Assert::assertCount(3, $transactionList->getTransactions()); } public function testDownloaderSetsLastId(): void @@ -85,11 +86,11 @@ public function testDownloaderSetsLastId(): void $downloader->setLastId('123456'); - $this->assertCount(1, $container); + Assert::assertCount(1, $container); /** @var \GuzzleHttp\Psr7\Request $request */ $request = $container[0]['request']; - $this->assertSame('https://fioapi.fio.cz/v1/rest/set-last-id/validToken/123456/', (string) $request->getUri()); + Assert::assertSame('https://fioapi.fio.cz/v1/rest/set-last-id/validToken/123456/', (string) $request->getUri()); } } diff --git a/tests/FioApi/TransactionListTest.php b/tests/FioApi/TransactionListTest.php index 8a3786a..0f42f9b 100644 --- a/tests/FioApi/TransactionListTest.php +++ b/tests/FioApi/TransactionListTest.php @@ -3,6 +3,8 @@ namespace FioApi; +use PHPUnit\Framework\Assert; + class TransactionListTest extends \PHPUnit\Framework\TestCase { public function testTransactionListValuesAreProperlySet(): void @@ -11,15 +13,13 @@ public function testTransactionListValuesAreProperlySet(): void $transactionList = TransactionList::create($transactionList->accountStatement); - $this->assertSame(500.0, $transactionList->getOpeningBalance()); - $this->assertSame(1000.0, $transactionList->getClosingBalance()); - $this->assertEquals(new \DateTimeImmutable('2015-03-30+0200'), $transactionList->getDateStart()); - $this->assertEquals(new \DateTimeImmutable('2015-03-31+0200'), $transactionList->getDateEnd()); - $this->assertSame((float) 1111111111, $transactionList->getIdFrom()); - $this->assertSame((float) 1111111999, $transactionList->getIdTo()); - $this->assertSame(null, $transactionList->getIdLastDownload()); - $this->assertInstanceOf(Account::class, $transactionList->getAccount()); - $this->assertInstanceOf(Transaction::class, $transactionList->getTransactions()[0]); + Assert::assertSame(500.0, $transactionList->getOpeningBalance()); + Assert::assertSame(1000.0, $transactionList->getClosingBalance()); + Assert::assertEquals(new \DateTimeImmutable('2015-03-30+0200'), $transactionList->getDateStart()); + Assert::assertEquals(new \DateTimeImmutable('2015-03-31+0200'), $transactionList->getDateEnd()); + Assert::assertSame((float) 1111111111, $transactionList->getIdFrom()); + Assert::assertSame((float) 1111111999, $transactionList->getIdTo()); + Assert::assertNull($transactionList->getIdLastDownload()); } public function testEmptyTransactionList(): void @@ -28,14 +28,13 @@ public function testEmptyTransactionList(): void $transactionList = TransactionList::create($transactionList->accountStatement); - $this->assertSame(0.0, $transactionList->getOpeningBalance()); - $this->assertSame(0.0, $transactionList->getClosingBalance()); - $this->assertEquals(new \DateTimeImmutable('2017-08-06+0200'), $transactionList->getDateStart()); - $this->assertEquals(new \DateTimeImmutable('2017-08-08+0200'), $transactionList->getDateEnd()); - $this->assertNull($transactionList->getIdFrom()); - $this->assertNull($transactionList->getIdTo()); - $this->assertNull($transactionList->getIdLastDownload()); - $this->assertInstanceOf(Account::class, $transactionList->getAccount()); - $this->assertCount(0, $transactionList->getTransactions()); + Assert::assertSame(0.0, $transactionList->getOpeningBalance()); + Assert::assertSame(0.0, $transactionList->getClosingBalance()); + Assert::assertEquals(new \DateTimeImmutable('2017-08-06+0200'), $transactionList->getDateStart()); + Assert::assertEquals(new \DateTimeImmutable('2017-08-08+0200'), $transactionList->getDateEnd()); + Assert::assertNull($transactionList->getIdFrom()); + Assert::assertNull($transactionList->getIdTo()); + Assert::assertNull($transactionList->getIdLastDownload()); + Assert::assertCount(0, $transactionList->getTransactions()); } } diff --git a/tests/FioApi/TransactionTest.php b/tests/FioApi/TransactionTest.php index 8533c63..09caa2d 100644 --- a/tests/FioApi/TransactionTest.php +++ b/tests/FioApi/TransactionTest.php @@ -3,6 +3,8 @@ namespace FioApi; +use PHPUnit\Framework\Assert; + class TransactionTest extends \PHPUnit\Framework\TestCase { public function testAccountValuesAreProperlySet(): void @@ -11,23 +13,23 @@ public function testAccountValuesAreProperlySet(): void $transaction = Transaction::create($transaction); - $this->assertSame('1111111111', $transaction->getId()); - $this->assertEquals(new \DateTimeImmutable('2015-03-30+0200'), $transaction->getDate()); - $this->assertSame(127.0, $transaction->getAmount()); - $this->assertSame('CZK', $transaction->getCurrency()); - $this->assertSame('214498596', $transaction->getSenderAccountNumber()); - $this->assertSame('2100', $transaction->getSenderBankCode()); - $this->assertSame('HUJER MARTIN', $transaction->getSenderName()); - $this->assertSame('0', $transaction->getVariableSymbol()); - $this->assertSame(null, $transaction->getConstantSymbol()); - $this->assertSame(null, $transaction->getSpecificSymbol()); - $this->assertSame('Banka, a.s.', $transaction->getSenderBankName()); - $this->assertSame('HUJER MARTIN', $transaction->getUserIdentity()); - $this->assertSame('Platba eshop', $transaction->getUserMessage()); - $this->assertSame('Bezhotovostní příjem', $transaction->getTransactionType()); - $this->assertSame(null, $transaction->getPerformedBy()); - $this->assertSame('Comment? Comment!', $transaction->getComment()); - $this->assertSame((float) 1111122222, $transaction->getPaymentOrderId()); - $this->assertSame('1500.00 EUR', $transaction->getSpecification()); + Assert::assertSame('1111111111', $transaction->getId()); + Assert::assertEquals(new \DateTimeImmutable('2015-03-30+0200'), $transaction->getDate()); + Assert::assertSame(127.0, $transaction->getAmount()); + Assert::assertSame('CZK', $transaction->getCurrency()); + Assert::assertSame('214498596', $transaction->getSenderAccountNumber()); + Assert::assertSame('2100', $transaction->getSenderBankCode()); + Assert::assertSame('HUJER MARTIN', $transaction->getSenderName()); + Assert::assertSame('0', $transaction->getVariableSymbol()); + Assert::assertNull($transaction->getConstantSymbol()); + Assert::assertNull($transaction->getSpecificSymbol()); + Assert::assertSame('Banka, a.s.', $transaction->getSenderBankName()); + Assert::assertSame('HUJER MARTIN', $transaction->getUserIdentity()); + Assert::assertSame('Platba eshop', $transaction->getUserMessage()); + Assert::assertSame('Bezhotovostní příjem', $transaction->getTransactionType()); + Assert::assertNull($transaction->getPerformedBy()); + Assert::assertSame('Comment? Comment!', $transaction->getComment()); + Assert::assertSame((float) 1111122222, $transaction->getPaymentOrderId()); + Assert::assertSame('1500.00 EUR', $transaction->getSpecification()); } } diff --git a/tests/FioApi/UrlBuilderTest.php b/tests/FioApi/UrlBuilderTest.php index ba33c96..9a95ce0 100644 --- a/tests/FioApi/UrlBuilderTest.php +++ b/tests/FioApi/UrlBuilderTest.php @@ -3,6 +3,8 @@ namespace FioApi; +use PHPUnit\Framework\Assert; + class UrlBuilderTest extends \PHPUnit\Framework\TestCase { public function testMissingTokenExceptionIsThrownForEmptyToken(): void @@ -15,20 +17,20 @@ public function testMissingTokenExceptionIsThrownForEmptyToken(): void public function testTokenCanBeSetThroughConstructor(): void { $urlBuilder = new UrlBuilder('token1'); - $this->assertSame('token1', $urlBuilder->getToken()); + Assert::assertSame('token1', $urlBuilder->getToken()); } public function testTokenCanBeChangedThroughSetter(): void { $urlBuilder = new UrlBuilder('token1'); $urlBuilder->setToken('token2'); - $this->assertSame('token2', $urlBuilder->getToken()); + Assert::assertSame('token2', $urlBuilder->getToken()); } public function testBuildPeriodsUrlReturnValidUrl(): void { $urlBuilder = new UrlBuilder('token1'); - $this->assertSame( + Assert::assertSame( 'https://fioapi.fio.cz/v1/rest/periods/token1/2015-03-25/2015-03-31/transactions.json', $urlBuilder->buildPeriodsUrl( new \DateTimeImmutable('2015-03-25'),