|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Terremoth\AsyncTest; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use InvalidArgumentException; |
| 7 | +use PHPUnit\Framework\Attributes\CoversNothing; |
| 8 | +use PHPUnit\Framework\Attributes\Test; |
| 9 | +use Terremoth\Async\PhpFile; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 12 | + |
| 13 | +class PhpFileTest extends TestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @throws Exception |
| 17 | + */ |
| 18 | + #[Test] |
| 19 | + #[CoversNothing] |
| 20 | + public function shouldOnlyAcceptPhpFiles(): void |
| 21 | + { |
| 22 | + $this->expectException(Exception::class); |
| 23 | + $tempFile = tmpfile(); |
| 24 | + $htmlString = '<!DOCTYPE html><html lang="en"><body><h1>Hello World</h1></body></html>'; |
| 25 | + fwrite($tempFile, $htmlString); |
| 26 | + $path = stream_get_meta_data($tempFile)['uri']; |
| 27 | + $phpFile = new PhpFile($path); |
| 28 | + $phpFile->run(); |
| 29 | + fclose($tempFile); |
| 30 | + unlink($path); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @throws InvalidArgumentException|Exception |
| 35 | + */ |
| 36 | + #[Test] |
| 37 | + #[CoversNothing] |
| 38 | + public function shouldOnlyAcceptReadablePhpFiles(): void |
| 39 | + { |
| 40 | + $this->expectException(InvalidArgumentException::class); |
| 41 | + |
| 42 | + if (PHP_OS_FAMILY == 'Windows') { |
| 43 | + $this->expectException(Exception::class); |
| 44 | + } |
| 45 | + |
| 46 | + $tempFile = tmpfile(); |
| 47 | + $htmlString = '<!DOCTYPE html><html lang="en"><body><h1>Hello World</h1></body></html>'; |
| 48 | + fwrite($tempFile, $htmlString); |
| 49 | + $path = stream_get_meta_data($tempFile)['uri']; |
| 50 | + $writeAndExecOnly = 0330; |
| 51 | + chmod($path, $writeAndExecOnly); // set as only write and exec, not readable |
| 52 | + $phpFile = new PhpFile($path); |
| 53 | + $phpFile->run(); |
| 54 | + fclose($tempFile); |
| 55 | + unlink($path); |
| 56 | + } |
| 57 | +} |
0 commit comments