|
| 1 | +<?php |
| 2 | +namespace Gt\Routing\Test\LogicStream; |
| 3 | + |
| 4 | +use Gt\Routing\LogicStream\LogicStreamWrapper; |
| 5 | +use Gt\Routing\RoutingException; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use RuntimeException; |
| 8 | + |
| 9 | +class LogicStreamWrapperTest extends TestCase { |
| 10 | + public function testFileDoesNotExist():void { |
| 11 | + $sut = new LogicStreamWrapper(); |
| 12 | + self::expectException(RuntimeException::class); |
| 13 | + self::expectExceptionMessage("Failed to open stream: No such file or directory"); |
| 14 | + $sut->stream_open("/does/not/exist"); |
| 15 | + } |
| 16 | + |
| 17 | + public function testStreamRead_withNamespace():void { |
| 18 | + stream_wrapper_register("gt-routing-test", LogicStreamWrapper::class); |
| 19 | + $sourcePath = "TmpFiles/" . uniqid() . ".php"; |
| 20 | + $source = <<<PHP |
| 21 | + <?php |
| 22 | + |
| 23 | + namespace Example; |
| 24 | +
|
| 25 | + class Test { |
| 26 | + } |
| 27 | + PHP; |
| 28 | + if(!is_dir(dirname($sourcePath))) { |
| 29 | + mkdir(dirname($sourcePath), recursive: true); |
| 30 | + } |
| 31 | + |
| 32 | + file_put_contents($sourcePath, $source); |
| 33 | + |
| 34 | + $actualContents = file_get_contents("gt-routing-test://$sourcePath"); |
| 35 | + unlink($sourcePath); |
| 36 | + rmdir(dirname($sourcePath)); |
| 37 | + self::assertSame($source, $actualContents); |
| 38 | + } |
| 39 | + |
| 40 | + public function testStreamRead_withoutNamespace():void { |
| 41 | + stream_wrapper_register("gt-routing-test", LogicStreamWrapper::class); |
| 42 | + $uniqid = uniqid(); |
| 43 | + $sourcePath = "TmpFiles/$uniqid.php"; |
| 44 | + $source = <<<PHP |
| 45 | + <?php |
| 46 | + |
| 47 | + function example() { |
| 48 | + // This is just an example that should be wrapped in a class. |
| 49 | + } |
| 50 | + PHP; |
| 51 | + if(!is_dir($sourcePath)) { |
| 52 | + mkdir(dirname($sourcePath), recursive: true); |
| 53 | + } |
| 54 | + file_put_contents($sourcePath, $source); |
| 55 | + |
| 56 | + $actualContents = file_get_contents("gt-routing-test://$sourcePath"); |
| 57 | + |
| 58 | + unlink($sourcePath); |
| 59 | + rmdir(dirname($sourcePath)); |
| 60 | + |
| 61 | + $expectedContents = <<<PHP |
| 62 | + <?php |
| 63 | + |
| 64 | + namespace Gt\AppLogic\TmpFiles\\${uniqid}_php; function example() { |
| 65 | + // This is just an example that should be wrapped in a class. |
| 66 | + } |
| 67 | + PHP; |
| 68 | + |
| 69 | + |
| 70 | + self::assertSame($expectedContents, $actualContents); |
| 71 | + } |
| 72 | + |
| 73 | + public function testStreamRead_withComment():void { |
| 74 | + stream_wrapper_register("gt-routing-test", LogicStreamWrapper::class); |
| 75 | + $uniqid = uniqid(); |
| 76 | + $sourcePath = "TmpFiles/$uniqid.php"; |
| 77 | + $source = <<<PHP |
| 78 | + <?php |
| 79 | + /** |
| 80 | + * This is an example docblock comment! |
| 81 | + */ |
| 82 | + function example() { |
| 83 | + // This is just an example that should be wrapped in a class. |
| 84 | + } |
| 85 | + PHP; |
| 86 | + if(!is_dir($sourcePath)) { |
| 87 | + mkdir(dirname($sourcePath), recursive: true); |
| 88 | + } |
| 89 | + file_put_contents($sourcePath, $source); |
| 90 | + |
| 91 | + $actualContents = file_get_contents("gt-routing-test://$sourcePath"); |
| 92 | + |
| 93 | + unlink($sourcePath); |
| 94 | + rmdir(dirname($sourcePath)); |
| 95 | + |
| 96 | + $expectedContents = <<<PHP |
| 97 | + <?php |
| 98 | + /** |
| 99 | + * This is an example docblock comment! |
| 100 | + */ |
| 101 | + namespace Gt\AppLogic\TmpFiles\\${uniqid}_php; function example() { |
| 102 | + // This is just an example that should be wrapped in a class. |
| 103 | + } |
| 104 | + PHP; |
| 105 | + |
| 106 | + self::assertSame($expectedContents, $actualContents); |
| 107 | + } |
| 108 | + |
| 109 | + public function testStreamRead_notPhp():void { |
| 110 | + stream_wrapper_register("gt-routing-test", LogicStreamWrapper::class); |
| 111 | + $uniqid = uniqid(); |
| 112 | + $sourcePath = "TmpFiles/$uniqid.php"; |
| 113 | + $source = <<<PHP |
| 114 | + /** |
| 115 | + * This is a PHP file that doesn't start with the opening tags. |
| 116 | + */ |
| 117 | + <h1>Hello, World!</h1> |
| 118 | + PHP; |
| 119 | + if(!is_dir($sourcePath)) { |
| 120 | + mkdir(dirname($sourcePath), recursive: true); |
| 121 | + } |
| 122 | + file_put_contents($sourcePath, $source); |
| 123 | + |
| 124 | + $exception = null; |
| 125 | + try { |
| 126 | + file_get_contents("gt-routing-test://$sourcePath"); |
| 127 | + } |
| 128 | + catch(RoutingException $exception) {} |
| 129 | + |
| 130 | + unlink($sourcePath); |
| 131 | + rmdir(dirname($sourcePath)); |
| 132 | + |
| 133 | + self::assertInstanceOf(RoutingException::class, $exception); |
| 134 | + self::assertStringContainsString("Logic file at TmpFiles/$uniqid.php must start by opening a PHP tag", $exception->getMessage()); |
| 135 | + } |
| 136 | +} |
0 commit comments