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 d64b646
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -6,10 +6,10 @@ sudo: false
language: php

php:
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2

env:
matrix:
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Expand Up @@ -9,15 +9,15 @@
],
"license": "LGPL-3.0",
"require": {
"php": "^7",
"php": "^5.5|^7",
"scriptfusion/porter": "4.0.x-dev"
},
"require-dev": {
"phpunit/phpunit": "^6"
"phpunit/phpunit": "^5.2"
},
"autoload": {
"psr-4": {
"ScriptFUSION\\Porter\\Net\\File\\": "src"
"ScriptFUSION\\Porter\\Connector\\File\\": "src"
}
},
"autoload-dev": {
Expand Down
20 changes: 20 additions & 0 deletions src/FileConnector.php
@@ -0,0 +1,20 @@
<?php
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);
});
}
}
10 changes: 10 additions & 0 deletions src/InvalidFilePathException.php
@@ -0,0 +1,10 @@
<?php
namespace ScriptFUSION\Porter\Connector\File;

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

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

final class FixtureFactory
{
use StaticClass;

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

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 \PHPUnit_Framework_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($file)
{
return $this->connector->fetch(FixtureFactory::createConnectionContext(), $file);
}
}

0 comments on commit d64b646

Please sign in to comment.