diff --git a/src/Command/ListAccountsCommand.php b/src/Command/ListAccountsCommand.php index c84375e..0f2f738 100644 --- a/src/Command/ListAccountsCommand.php +++ b/src/Command/ListAccountsCommand.php @@ -8,8 +8,6 @@ class ListAccountsCommand extends AbstractCommand { - public const RETURN_VAR_NOT_FOUND = 4; - public const BANK = 'bank'; public const NUMBER = 'number'; diff --git a/src/Command/ListUsersCommand.php b/src/Command/ListUsersCommand.php index 1b1957d..7a0b43d 100644 --- a/src/Command/ListUsersCommand.php +++ b/src/Command/ListUsersCommand.php @@ -20,7 +20,7 @@ public function execute(): ?DOMDocument $result = $this->getShellCommandExecutor()->execute($shellCommand); - if (4 === $result->getReturnVar()) { + if (self::RETURN_VAR_NOT_FOUND === $result->getReturnVar()) { return null; } diff --git a/src/Command/ShellCommandExecutor.php b/src/Command/ShellCommandExecutor.php index b0d6636..61a4b13 100644 --- a/src/Command/ShellCommandExecutor.php +++ b/src/Command/ShellCommandExecutor.php @@ -25,10 +25,7 @@ public function execute(string $shellCommand): Result exec($shellCommand . ' 2>' . $tempFile, $output, $returnVar); - $errorOutput = file($tempFile); - $errorOutput = array_map(function ($line) { - return rtrim($line, "\r\n"); - }, $errorOutput); + $errorOutput = array_map(fn ($line) => rtrim($line, "\r\n"), file($tempFile)); unlink($tempFile); return new Result($output, $errorOutput, $returnVar); diff --git a/src/Command/ShellCommandExecutor/DefectiveResultException.php b/src/Command/ShellCommandExecutor/DefectiveResultException.php index 5beae26..ab90a86 100644 --- a/src/Command/ShellCommandExecutor/DefectiveResultException.php +++ b/src/Command/ShellCommandExecutor/DefectiveResultException.php @@ -17,7 +17,7 @@ public function __construct(string $message = '', int $code = 0, Exception $prev parent::__construct( $message . " - Command: " . $shellCommand . - " - Errors: " . implode(PHP_EOL, $result->getErrors()), + " - Errors: " . implode(PHP_EOL, $result?->getErrors()), $code, $previous ); diff --git a/src/Command/ShellCommandExecutor/ResultAnalyzer.php b/src/Command/ShellCommandExecutor/ResultAnalyzer.php index fdefa8e..d9f05f5 100644 --- a/src/Command/ShellCommandExecutor/ResultAnalyzer.php +++ b/src/Command/ShellCommandExecutor/ResultAnalyzer.php @@ -59,7 +59,7 @@ public function isDefectiveResult(Result $result): bool private function resultHasErrors(Result $result): bool { - if (1 === \count($result->getErrors()) && preg_match('/accepting valid new certificate/', $result->getErrors()[0])) { + if (1 === \count($result->getErrors()) && str_contains($result->getErrors()[0], 'accepting valid new certificate')) { // When calling getsysid with wrong PIN, we don't get any error message. // The only significant aspect of the error is that the output is just one line with // "accepting valid new certificate" diff --git a/src/ContextXmlRenderer.php b/src/ContextXmlRenderer.php index ff8ee42..459d8db 100644 --- a/src/ContextXmlRenderer.php +++ b/src/ContextXmlRenderer.php @@ -121,7 +121,7 @@ private function renderMultiLineElement(DOMNodeList $nodes): string $lines = []; foreach ($nodes as $node) { $line = trim($node->nodeValue); - if (false !== strpos($line, '|')) { + if (str_contains($line, '|')) { throw new RuntimeException('Unexpected character'); } $lines[] = $line; diff --git a/src/PinFile/PinFile.php b/src/PinFile/PinFile.php index d1c6e6a..fcc85e3 100644 --- a/src/PinFile/PinFile.php +++ b/src/PinFile/PinFile.php @@ -25,6 +25,6 @@ public function getFileName(): string public function getPath(): string { - return $this->dir . '/' . $this->getFileName(); + return rtrim($this->dir, '/') . '/' . $this->getFileName(); } } diff --git a/src/Transaction.php b/src/Transaction.php index 7e36f16..a48a708 100644 --- a/src/Transaction.php +++ b/src/Transaction.php @@ -83,7 +83,7 @@ public function toArray(): array 'currency' => $this->getValue()->getCurrency()->getCode(), 'priceUnit' => 100, ], - 'valutaDate' => $this->getValutaDate() ? $this->getValutaDate()->format('Y-m-d') : null, + 'valutaDate' => $this->getValutaDate()?->format('Y-m-d'), 'primaNota' => $this->getPrimaNota(), 'customerReference' => $this->getCustomerReference(), ]; diff --git a/tests/Command/ShellCommandExecutorTest.php b/tests/Command/ShellCommandExecutorTest.php index 4801c09..ebab084 100644 --- a/tests/Command/ShellCommandExecutorTest.php +++ b/tests/Command/ShellCommandExecutorTest.php @@ -1,5 +1,7 @@ assertEquals($expectedTransactions, $sut->getTransactions()); } - /** - * @throws Exception - */ + public function testCanRenderBalances() + { + $fixture = file_get_contents(__DIR__ . '/fixtures/test_context_file_transactions.xml'); + $domDocument = new \DOMDocument(); + $domDocument->loadXML($fixture); + + $sut = new ContextXmlRenderer($domDocument); + + $balances = $sut->getBalances(); + + $this->assertCount(3, $balances); + $this->assertInstanceOf(\DateTime::class, $balances[0]->getDate()); + $this->assertInstanceOf(Money::class, $balances[0]->getValue()); + $this->assertSame('temporary', $balances[0]->getType()); + } + public function testThrowsExceptionIfDataContainsReservedChar() { $fixture = file_get_contents(__DIR__ . '/fixtures/test_context_file_transactions_with_reserved_char.xml'); diff --git a/tests/PinFile/PinFileTest.php b/tests/PinFile/PinFileTest.php index e510b61..95b1349 100644 --- a/tests/PinFile/PinFileTest.php +++ b/tests/PinFile/PinFileTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace PinFile; +namespace Tests\PinFile; use AqBanking\Bank; use AqBanking\BankCode; @@ -20,30 +20,51 @@ */ class PinFileTest extends TestCase { - public function testPinFile(): void + private static string $userId = '1'; + + private static string $bankCode = '50050010'; + + /** + * @dataProvider samples + */ + public function testPinFile(string $dir, User $user, string $expectedFileName, string $expectedPath): void { - $userId = '1'; - $bankCode = '50050010'; + $sut = new PinFile( + dir: $dir, + user: $user + ); + + $this->assertSame($expectedFileName, $sut->getFileName()); + $this->assertSame($expectedPath, $sut->getPath()); + } + public static function samples(): array + { $user = new User( - userId: $userId, + userId: self::$userId, userName: 'Max Mustermann', bank: new Bank( - bankCode: new BankCode($bankCode), + bankCode: new BankCode(self::$bankCode), hbciUrl: 'https://fints.bank.de/fints', hbciVersion: new HbciVersion('1.2.3'), ) ); - $dir = sys_get_temp_dir(); - $sut = new PinFile( - dir: $dir, - user: $user - ); - - $expectedFileName = 'pinfile_' . $bankCode . '_' . $userId; + $expectedFileName = 'pinfile_' . self::$bankCode . '_' . self::$userId; - $this->assertSame($expectedFileName, $sut->getFileName()); - $this->assertSame(implode('/', [$dir, $expectedFileName]), $sut->getPath()); + return [ + 'trailing slash' => [ + './', + $user, + $expectedFileName, + './' . $expectedFileName, + ], + 'no trailing slash' => [ + '/home', + $user, + $expectedFileName, + '/home/' . $expectedFileName, + ], + ]; } }