Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/LogicStream/LogicStreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LogicStreamWrapper {
private string $contents;

// phpcs:ignore Generic.NamingConventions.CamelCapsFunctionName
function stream_open(string $path):bool {
public function stream_open(string $path):bool {
$this->position = 0;
$this->path = substr($path, strpos($path, "//") + 2);
$this->contents = "";
Expand All @@ -25,19 +25,19 @@ function stream_open(string $path):bool {
}

// phpcs:ignore Generic.NamingConventions.CamelCapsFunctionName
function stream_read(int $count):string {
public function stream_read(int $count):string {
$ret = substr($this->contents, $this->position, $count);
$this->position += strlen($ret);
return $ret;
}

// phpcs:ignore Generic.NamingConventions.CamelCapsFunctionName
function stream_tell():int {
public function stream_tell():int {
return $this->position;
}

// phpcs:ignore Generic.NamingConventions.CamelCapsFunctionName
function stream_eof():bool {
public function stream_eof():bool {
return $this->position >= strlen($this->contents);
}

Expand Down Expand Up @@ -126,8 +126,11 @@ private function checkAndAppendNamespace(
}

if($line) {
if($this->contents === "<?php\n") {
$this->contents = "<?php\t";
}
$namespace = new LogicStreamNamespace($this->path, self::NAMESPACE_PREFIX);
$this->contents .= "namespace $namespace;\n$originalLine\t";
$this->contents .= "namespace $namespace;\n$originalLine";
return true;
}
}
Expand Down
64 changes: 64 additions & 0 deletions test/phpunit/LogicStream/LogicStreamWrapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
namespace Gt\Routing\Test\LogicStream;

use Gt\Routing\LogicStream\LogicStreamWrapper;
use PHPUnit\Framework\TestCase;

class LogicStreamWrapperTest extends TestCase {
public function testStreamOpen_notPhpFile():void {
$tmpFile = "/tmp/phpgt-routing-example-" . uniqid();
$logicPath = "phpgt-test://$tmpFile";

$contents = bin2hex(random_bytes(16));
file_put_contents($tmpFile, $contents);

$sut = new LogicStreamWrapper();
self::expectExceptionMessage("Logic file at $tmpFile must start by opening a PHP tag.");
$sut->stream_open($logicPath);
}

public function testStreamOpen():void {
$uniqid = uniqid();
$tmpFile = "/tmp/phpgt-routing-example-" . $uniqid;
$logicPath = "phpgt-test://$tmpFile";

$contents = "<?php\n" . bin2hex(random_bytes(16));
file_put_contents($tmpFile, $contents);

$sut = new LogicStreamWrapper();
$sut->stream_open($logicPath);
self::assertSame(0, $sut->stream_tell());

$namespaceLine = "namespace Gt\\AppLogic\\\\tmp\\phpgt_routing_example_$uniqid;";
$contentsWithNamespace = substr_replace($contents, "$namespaceLine\n", strpos($contents, "\n") + 1, 0);
$contentsWithNamespace = substr_replace($contentsWithNamespace, "\t", strpos($contentsWithNamespace, "\n"), 1);

self::assertSame($contentsWithNamespace, $sut->stream_read(1024));
}

public function testStreamRead():void {
$uniqid = uniqid();
$tmpFile = "/tmp/phpgt-routing-example-" . $uniqid;
$logicPath = "phpgt-test://$tmpFile";

$contents = <<<PHP
<?php
use Something;
use SomethingElse;

function example():void {
// This is line 6
}
PHP;
file_put_contents($tmpFile, $contents);

$sut = new LogicStreamWrapper();
$sut->stream_open($logicPath);
$actualContents = $sut->stream_read(1024);

self::assertStringContainsString(
"// This is line 6",
explode("\n", $actualContents)[5],
);
}
}