|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/* |
| 3 | + * This file is part of PHPUnit. |
| 4 | + * |
| 5 | + * (c) Sebastian Bergmann <sebastian@phpunit.de> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | +namespace PHPUnit\TestFixture\Issue5884; |
| 11 | + |
| 12 | +use const E_USER_DEPRECATED; |
| 13 | +use function chmod; |
| 14 | +use function file_get_contents; |
| 15 | +use function file_put_contents; |
| 16 | +use function sys_get_temp_dir; |
| 17 | +use function tempnam; |
| 18 | +use function trigger_error; |
| 19 | +use function unlink; |
| 20 | +use Exception; |
| 21 | +use PHPUnit\Framework\Attributes\IgnoreDeprecations; |
| 22 | +use PHPUnit\Framework\Attributes\RequiresPhpunit; |
| 23 | +use PHPUnit\Framework\Attributes\WithoutErrorHandler; |
| 24 | +use PHPUnit\Framework\TestCase; |
| 25 | + |
| 26 | +final class FooTest extends TestCase |
| 27 | +{ |
| 28 | + #[RequiresPhpunit('^11.0')] |
| 29 | + public function testExpectUserDeprecationMessageNOTIgnoringDeprecations(): void |
| 30 | + { |
| 31 | + $this->expectUserDeprecationMessage('foo'); |
| 32 | + |
| 33 | + trigger_error('foo', E_USER_DEPRECATED); |
| 34 | + } |
| 35 | + |
| 36 | + #[RequiresPhpunit('^11.0')] |
| 37 | + #[IgnoreDeprecations] |
| 38 | + public function testExpectUserDeprecationMessageANDIgnoringDeprecations(): void |
| 39 | + { |
| 40 | + $this->expectUserDeprecationMessage('foo'); |
| 41 | + |
| 42 | + trigger_error('foo', E_USER_DEPRECATED); |
| 43 | + } |
| 44 | + |
| 45 | + public function testPcreHasUtf8Support(): void |
| 46 | + { |
| 47 | + $this->assertIsBool(Foo::pcreHasUtf8Support()); |
| 48 | + } |
| 49 | + |
| 50 | + public function testStreamToNonWritableFileWithPHPUnitErrorHandler(): void |
| 51 | + { |
| 52 | + // Create an unwritable file. |
| 53 | + $filename = tempnam(sys_get_temp_dir(), 'RLT'); |
| 54 | + |
| 55 | + if (file_put_contents($filename, 'foo')) { |
| 56 | + chmod($filename, 0o444); |
| 57 | + } |
| 58 | + |
| 59 | + try { |
| 60 | + Foo::openFile($filename); |
| 61 | + } catch (Exception $e) { |
| 62 | + // This "Failed to open stream" exception is expected. |
| 63 | + } |
| 64 | + |
| 65 | + // Now verify the original file is unchanged. |
| 66 | + $contents = file_get_contents($filename); |
| 67 | + $this->assertSame('foo', $contents); |
| 68 | + |
| 69 | + chmod($filename, 0o755); |
| 70 | + unlink($filename); |
| 71 | + } |
| 72 | + |
| 73 | + #[WithoutErrorHandler] |
| 74 | + public function testStreamToNonWritableFileWithoutPHPUnitErrorHandler(): void |
| 75 | + { |
| 76 | + // Create an unwritable file. |
| 77 | + $filename = tempnam(sys_get_temp_dir(), 'RLT'); |
| 78 | + |
| 79 | + if (file_put_contents($filename, 'foo')) { |
| 80 | + chmod($filename, 0o444); |
| 81 | + } |
| 82 | + |
| 83 | + try { |
| 84 | + Foo::openFile($filename); |
| 85 | + } catch (Exception $e) { |
| 86 | + // This "Failed to open stream" exception is expected. |
| 87 | + } |
| 88 | + |
| 89 | + // Now verify the original file is unchanged. |
| 90 | + $contents = file_get_contents($filename); |
| 91 | + $this->assertSame('foo', $contents); |
| 92 | + |
| 93 | + chmod($filename, 0o755); |
| 94 | + unlink($filename); |
| 95 | + } |
| 96 | + |
| 97 | + public function testStreamToInvalidFile(): void |
| 98 | + { |
| 99 | + $filename = tempnam(sys_get_temp_dir(), 'RLT') . '/missing/directory'; |
| 100 | + |
| 101 | + $this->expectException(Exception::class); |
| 102 | + // First character (F) can be upper or lowercase depending on PHP version. |
| 103 | + $this->expectExceptionMessage('ailed to open stream'); |
| 104 | + |
| 105 | + Foo::openFile($filename); |
| 106 | + } |
| 107 | +} |
0 commit comments