Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added FileConnector and accompany test.
- Loading branch information
Showing
6 changed files
with
130 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,10 @@ sudo: false | |
language: php | ||
|
||
php: | ||
- 5.5 | ||
- 5.6 | ||
- 7.0 | ||
- 7.1 | ||
- 7.2 | ||
|
||
env: | ||
matrix: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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\"."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |