Skip to content

Commit

Permalink
Add FileLoader tests
Browse files Browse the repository at this point in the history
  • Loading branch information
usox committed Dec 19, 2023
1 parent d74e1a1 commit d1a990d
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/Io/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
use Generator;
use Usox\TalI18nExtract\ExtractorDecorator;

/**
* Gets file-paths and returns fully-loaded XPath-objects one-by-one
*/
final class FileLoader implements FileLoaderInterface
{
/**
* Loads every given-file path and return a XPath object
*
* @param iterable<string> $filePaths
*
* @return Generator<DOMXPath>
Expand All @@ -20,15 +25,13 @@ public function read(
iterable $filePaths
): Generator {
foreach ($filePaths as $path) {
$path = (string) realpath($path);

if (!file_exists($path)) {
trigger_error('Not existing: '.$path, E_USER_WARNING);

continue;
}

$content = file_get_contents($path);
$content = @file_get_contents($path);
if ($content === false) {
trigger_error('Not readable: '.$path, E_USER_WARNING);

Expand All @@ -43,10 +46,7 @@ public function read(
continue;
}

$xpath = new DOMXPath($dom);
$xpath->registerNamespace('i18n', ExtractorDecorator::I18N_NAMESPACE);

yield $xpath;
yield new DOMXPath($dom);
}
}
}
2 changes: 2 additions & 0 deletions src/Io/FileLoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
interface FileLoaderInterface
{
/**
* Loads every given-file path and return a XPath object
*
* @param iterable<string> $filePaths
*
* @return Generator<DOMXPath>
Expand Down
104 changes: 104 additions & 0 deletions tests/Io/FileLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace Usox\TalI18nExtract\Io;

use DOMXPath;
use Exception;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamFile;
use PHPUnit\Framework\TestCase;

class FileLoaderTest extends TestCase
{
private vfsStreamDirectory $vfsStream;

private FileLoader $subject;

protected function setUp(): void
{
$this->subject = new FileLoader();

$this->vfsStream = vfsStream::setup('/');

set_error_handler(static function (int $errno, string $errstr): never {
throw new Exception($errstr, $errno);
}, E_USER_WARNING);
}

public function testReadFailsIfFileDoesNotExist(): void
{
$filename = $this->vfsStream->url() . '/snafu';

static::expectException(Exception::class);
static::expectExceptionMessage('Not existing: '.$filename);

static::assertSame(
[],
iterator_to_array($this->subject->read([$filename]))
);
}

public function testReadFailsIfFileIsNotReadable(): void
{
$file = new vfsStreamFile('snafu', 0000);

$this->vfsStream->addChild($file);

$filename = $file->url();

static::expectException(Exception::class);
static::expectExceptionMessage('Not readable: '.$filename);

static::assertSame(
[],
iterator_to_array($this->subject->read([$filename]))
);
}

public function testReadFailsIfFileDoesNotContainValidXml(): void
{
$file = new vfsStreamFile('snafu');

$this->vfsStream->addChild($file);

$filename = $file->url();

file_put_contents($filename, 'snafu');

static::expectException(Exception::class);
static::expectExceptionMessage('Does not contain valid xml: '.$filename);

static::assertSame(
[],
iterator_to_array($this->subject->read([$filename]))
);
}

public function testReadReturnsPreparedXPathObject(): void
{
$file = new vfsStreamFile('snafu');

$this->vfsStream->addChild($file);

$filename = $file->url();

file_put_contents($filename, '<?xml version="1.0" encoding="utf-8"?><root>foobar</root>');

$xpath = current(
iterator_to_array($this->subject->read([$filename]))
);

static::assertInstanceOf(
DOMXPath::class,
$xpath
);
}

protected function tearDown(): void
{
restore_error_handler();
}
}

0 comments on commit d1a990d

Please sign in to comment.