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