Skip to content

Commit e0d22a0

Browse files
committed
fix: line numbers align
closes #64
1 parent f3762ce commit e0d22a0

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-5
lines changed

src/LogicStream/LogicStreamWrapper.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class LogicStreamWrapper {
1616
private string $contents;
1717

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

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

3434
// phpcs:ignore Generic.NamingConventions.CamelCapsFunctionName
35-
function stream_tell():int {
35+
public function stream_tell():int {
3636
return $this->position;
3737
}
3838

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

@@ -126,8 +126,11 @@ private function checkAndAppendNamespace(
126126
}
127127

128128
if($line) {
129+
if($this->contents === "<?php\n") {
130+
$this->contents = "<?php\t";
131+
}
129132
$namespace = new LogicStreamNamespace($this->path, self::NAMESPACE_PREFIX);
130-
$this->contents .= "namespace $namespace;\n$originalLine\t";
133+
$this->contents .= "namespace $namespace;\n$originalLine";
131134
return true;
132135
}
133136
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
namespace Gt\Routing\Test\LogicStream;
3+
4+
use Gt\Routing\LogicStream\LogicStreamWrapper;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class LogicStreamWrapperTest extends TestCase {
8+
public function testStreamOpen_notPhpFile():void {
9+
$tmpFile = "/tmp/phpgt-routing-example-" . uniqid();
10+
$logicPath = "phpgt-test://$tmpFile";
11+
12+
$contents = bin2hex(random_bytes(16));
13+
file_put_contents($tmpFile, $contents);
14+
15+
$sut = new LogicStreamWrapper();
16+
self::expectExceptionMessage("Logic file at $tmpFile must start by opening a PHP tag.");
17+
$sut->stream_open($logicPath);
18+
}
19+
20+
public function testStreamOpen():void {
21+
$uniqid = uniqid();
22+
$tmpFile = "/tmp/phpgt-routing-example-" . $uniqid;
23+
$logicPath = "phpgt-test://$tmpFile";
24+
25+
$contents = "<?php\n" . bin2hex(random_bytes(16));
26+
file_put_contents($tmpFile, $contents);
27+
28+
$sut = new LogicStreamWrapper();
29+
$sut->stream_open($logicPath);
30+
self::assertSame(0, $sut->stream_tell());
31+
32+
$namespaceLine = "namespace Gt\\AppLogic\\\\tmp\\phpgt_routing_example_$uniqid;";
33+
$contentsWithNamespace = substr_replace($contents, "$namespaceLine\n", strpos($contents, "\n") + 1, 0);
34+
$contentsWithNamespace = substr_replace($contentsWithNamespace, "\t", strpos($contentsWithNamespace, "\n"), 1);
35+
36+
self::assertSame($contentsWithNamespace, $sut->stream_read(1024));
37+
}
38+
39+
public function testStreamRead():void {
40+
$uniqid = uniqid();
41+
$tmpFile = "/tmp/phpgt-routing-example-" . $uniqid;
42+
$logicPath = "phpgt-test://$tmpFile";
43+
44+
$contents = <<<PHP
45+
<?php
46+
use Something;
47+
use SomethingElse;
48+
49+
function example():void {
50+
// This is line 6
51+
}
52+
PHP;
53+
file_put_contents($tmpFile, $contents);
54+
55+
$sut = new LogicStreamWrapper();
56+
$sut->stream_open($logicPath);
57+
$actualContents = $sut->stream_read(1024);
58+
59+
self::assertStringContainsString(
60+
"// This is line 6",
61+
explode("\n", $actualContents)[5],
62+
);
63+
}
64+
}

0 commit comments

Comments
 (0)