|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class StreamUtilTest |
| 4 | + * |
| 5 | + * @created 24.07.2023 |
| 6 | + * @author smiley <smiley@chillerlan.net> |
| 7 | + * @copyright 2023 smiley |
| 8 | + * @license MIT |
| 9 | + */ |
| 10 | + |
| 11 | +namespace chillerlan\HTTPTest\Utils; |
| 12 | + |
| 13 | +use chillerlan\HTTP\Utils\StreamUtil; |
| 14 | +use InvalidArgumentException; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use function fclose; |
| 17 | +use function fopen; |
| 18 | +use function stream_get_meta_data; |
| 19 | +use function substr; |
| 20 | +use function var_dump; |
| 21 | + |
| 22 | +/** |
| 23 | + * |
| 24 | + */ |
| 25 | +class StreamUtilTest extends TestCase{ |
| 26 | + use FactoryTrait; |
| 27 | + |
| 28 | + public function testModeAllowsRead():void{ |
| 29 | + $this::assertTrue(StreamUtil::modeAllowsRead('r+')); |
| 30 | + $this::assertFalse(StreamUtil::modeAllowsRead('w')); |
| 31 | + } |
| 32 | + |
| 33 | + public function testModeAllowsReadOnly():void{ |
| 34 | + $this::assertTrue(StreamUtil::modeAllowsReadOnly('r')); |
| 35 | + $this::assertFalse(StreamUtil::modeAllowsReadOnly('r+b')); |
| 36 | + } |
| 37 | + |
| 38 | + public function testModeAllowsWrite():void{ |
| 39 | + $this::assertTrue(StreamUtil::modeAllowsWrite('w+')); |
| 40 | + $this::assertFalse(StreamUtil::modeAllowsWrite('rb')); |
| 41 | + } |
| 42 | + |
| 43 | + public function testModeAllowsWriteOnly():void{ |
| 44 | + $this::assertTrue(StreamUtil::modeAllowsWriteOnly('a')); |
| 45 | + $this::assertFalse(StreamUtil::modeAllowsWriteOnly('c+t')); |
| 46 | + } |
| 47 | + |
| 48 | + public function testModeAllowsReadWrite():void{ |
| 49 | + $this::assertTrue(StreamUtil::modeAllowsReadWrite('r+e')); |
| 50 | + $this::assertFalse(StreamUtil::modeAllowsReadWrite('r')); |
| 51 | + } |
| 52 | + |
| 53 | + public function testCheckModeIsValidThrows():void{ |
| 54 | + $this->expectException(InvalidArgumentException::class); |
| 55 | + $this->expectExceptionMessage('invalid fopen mode: b+'); |
| 56 | + |
| 57 | + StreamUtil::validateMode('b+'); |
| 58 | + } |
| 59 | + |
| 60 | + public function testModeAllowedFlagPositionIrrelevant():void{ |
| 61 | + $mode = 'rwarrrrrw++++b12345'; |
| 62 | + $this::assertTrue(StreamUtil::modeAllowsRead($mode)); |
| 63 | + |
| 64 | + $fh = fopen(__DIR__.'/fopen-test.txt', $mode); |
| 65 | + $meta = stream_get_meta_data($fh); |
| 66 | + |
| 67 | + $this::assertSame(substr($mode, 0, 15), $meta['mode']); |
| 68 | + fclose($fh); |
| 69 | + } |
| 70 | + |
| 71 | + public function testGetContentsRewindsStream():void{ |
| 72 | + $stream = $this->streamFactory->createStream(); |
| 73 | + |
| 74 | + $stream->write('test'); |
| 75 | + |
| 76 | + $this::assertSame(4, $stream->tell()); |
| 77 | + $this::assertSame('test', StreamUtil::getContents($stream)); |
| 78 | + $this::assertSame(0, $stream->tell()); |
| 79 | + } |
| 80 | + |
| 81 | + public function testGetContentsFromUnreadableStream():void{ |
| 82 | + $stream = $this->streamFactory->createStreamFromResource(fopen(__DIR__.'/fopen-test.txt', 'a')); |
| 83 | + |
| 84 | + $this::assertFalse($stream->isReadable()); |
| 85 | + $this::assertNull(StreamUtil::getContents($stream)); |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments