Skip to content

Commit

Permalink
Added FileConnector and accompany test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Nov 2, 2017
1 parent 826af37 commit e088824
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -17,7 +17,7 @@
},
"autoload": {
"psr-4": {
"ScriptFUSION\\Porter\\Net\\File\\": "src"
"ScriptFUSION\\Porter\\Connector\\File\\": "src"
}
},
"autoload-dev": {
Expand Down
22 changes: 22 additions & 0 deletions src/FileConnector.php
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);

namespace ScriptFUSION\Porter\Connector\File;

use ScriptFUSION\Porter\Connector\ConnectionContext;
use ScriptFUSION\Porter\Connector\Connector;
use ScriptFUSION\Porter\Options\EncapsulatedOptions;

class FileConnector implements Connector
{
public function fetch(ConnectionContext $context, $source, EncapsulatedOptions $options = null)
{
if (preg_match('[^(\w+)://]', $source, $matches) && strtolower($matches[1]) !== 'file') {
throw new InvalidFilePathException($source);
}

return $context->retry(function () use ($source) {
return file_get_contents($source);
});
}
}
12 changes: 12 additions & 0 deletions src/InvalidFilePathException.php
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

namespace ScriptFUSION\Porter\Connector\File;

final class InvalidFilePathException extends \RuntimeException
{
public function __construct(string $path)
{
parent::__construct("The specified file path was invalid: \"$path\".");
}
}
23 changes: 23 additions & 0 deletions test/FixtureFactory.php
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

namespace ScriptFUSIONTest;

use ScriptFUSION\Porter\Connector\ConnectionContext;
use ScriptFUSION\StaticClass;

final class FixtureFactory
{
use StaticClass;

public static function createConnectionContext(): ConnectionContext
{
return new ConnectionContext(
false,
function () {
// Intentionally empty.
},
1
);
}
}
78 changes: 78 additions & 0 deletions test/Functional/FileConnectorTest.php
@@ -0,0 +1,78 @@
<?php
declare(strict_types=1);

namespace ScriptFUSIONTest\Functional;

use PHPUnit\Framework\TestCase;
use ScriptFUSION\Porter\Connector\File\FileConnector;
use ScriptFUSION\Porter\Connector\File\InvalidFilePathException;
use ScriptFUSION\Retry\FailingTooHardException;
use ScriptFUSIONTest\FixtureFactory;

/**
* @see FileConnector
*/
final class FileConnectorTest extends TestCase
{
/**
* @var FileConnector
*/
private $connector;

protected function setUp()
{
$this->connector = new FileConnector;
}

/**
* Tests that an empty path fails.
*/
public function testEmpty()
{
$this->expectException(FailingTooHardException::class);

$this->fetch('');
}

/**
* Tests that absolute paths succeed.
*/
public function testAbsolutePath()
{
self::assertStringEqualsFile(__FILE__, $this->fetch(__FILE__));
}

/**
* Tests that relative paths succeed.
*/
public function testRelativePath()
{
chdir(__DIR__);

self::assertStringEqualsFile(__FILE__, $this->fetch(basename(__FILE__)));
}

/**
* Tests that accessing the file:// protocol, with any capitalization, succeeds.
*/
public function testFileProtocol()
{
self::assertStringEqualsFile(__FILE__, $this->fetch('file://' . __FILE__));
self::assertStringEqualsFile(__FILE__, $this->fetch('FILE://' . __FILE__));
}

/**
* Tests that accessing any protocol other than file:// throws an exception.
*/
public function testHttpProtocol()
{
$this->expectException(InvalidFilePathException::class);

$this->fetch('http://example.com');
}

private function fetch(string $file)
{
return $this->connector->fetch(FixtureFactory::createConnectionContext(), $file);
}
}

0 comments on commit e088824

Please sign in to comment.