Skip to content

Commit

Permalink
Merge pull request #26 from grummbeer/modernize-test-fix
Browse files Browse the repository at this point in the history
Some minor changes
  • Loading branch information
amenk authored Dec 11, 2023
2 parents 1fdb337 + 81f6b2f commit afc9696
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 30 deletions.
2 changes: 0 additions & 2 deletions src/Command/ListAccountsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

class ListAccountsCommand extends AbstractCommand
{
public const RETURN_VAR_NOT_FOUND = 4;

public const BANK = 'bank';

public const NUMBER = 'number';
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ListUsersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 1 addition & 4 deletions src/Command/ShellCommandExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ShellCommandExecutor/ResultAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/ContextXmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/PinFile/PinFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public function getFileName(): string

public function getPath(): string
{
return $this->dir . '/' . $this->getFileName();
return rtrim($this->dir, '/') . '/' . $this->getFileName();
}
}
2 changes: 1 addition & 1 deletion src/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
];
Expand Down
2 changes: 2 additions & 0 deletions tests/Command/ShellCommandExecutorTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Tests\Command;

use AqBanking\Command\ShellCommandExecutor;
Expand Down
20 changes: 17 additions & 3 deletions tests/ContextXmlRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
/**
* @covers \AqBanking\ContextXmlRenderer
* @uses \AqBanking\ContentXmlRenderer\MoneyElementRenderer
* @uses \AqBanking\Balance
*/
class ContextXmlRendererTest extends TestCase
{
Expand Down Expand Up @@ -78,9 +79,22 @@ public function testCanRenderTransactions()
$this->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');
Expand Down
51 changes: 36 additions & 15 deletions tests/PinFile/PinFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace PinFile;
namespace Tests\PinFile;

use AqBanking\Bank;
use AqBanking\BankCode;
Expand All @@ -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,
],
];
}
}

0 comments on commit afc9696

Please sign in to comment.