Skip to content

Commit

Permalink
Fixed StreamWrapper invalid schema
Browse files Browse the repository at this point in the history
  • Loading branch information
spajxo committed Feb 22, 2023
1 parent d0180d4 commit 8ebcbb7
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes will be documented in this file.

Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## [0.2.1] 2022-02-22
### Fixed
- Fixed StreamWrapper invalid schema

## [0.2.0] 2022-02-22
### Changed
- Refactor Stream to implement PSR-7 StreamInterface
Expand Down
3 changes: 2 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
forceCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
>
Expand Down
16 changes: 8 additions & 8 deletions src/StreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
final class StreamWrapper
{
/** @var resource */
public $resource;
public $context;
private StreamInterface $stream;
private string $mode;

Expand All @@ -35,7 +35,7 @@ public static function from(StreamInterface $stream)
throw new InvalidArgumentException('The stream must be readable, writable, or both.');
}

$resource = fopen('digitalcz_streams://stream', $mode, false, self::createStreamContext($stream));
$resource = fopen('digitalcz-streams://stream', $mode, false, self::createStreamContext($stream));

if (!is_resource($resource)) {
throw new StreamException('Unable to create StreamWrapper');
Expand All @@ -52,7 +52,7 @@ public static function from(StreamInterface $stream)
public static function createStreamContext(StreamInterface $stream)
{
return stream_context_create([
'digitalcz_streams' => ['stream' => $stream],
'digitalcz-streams' => ['stream' => $stream],
]);
}

Expand All @@ -61,22 +61,22 @@ public static function createStreamContext(StreamInterface $stream)
*/
public static function register(): void
{
if (!in_array('digitalcz_streams', stream_get_wrappers(), true)) {
stream_wrapper_register('digitalcz_streams', self::class);
if (!in_array('digitalcz-streams', stream_get_wrappers(), true)) {
stream_wrapper_register('digitalcz-streams', self::class);
}
}

// phpcs:ignore
public function stream_open(string $path, string $mode, int $options, ?string &$opened_path = null): bool
{
$options = stream_context_get_options($this->resource);
$options = stream_context_get_options($this->context);

if (!isset($options['digitalcz_streams']['stream'])) {
if (!isset($options['digitalcz-streams']['stream'])) {
return false;
}

$this->mode = $mode;
$this->stream = $options['digitalcz_streams']['stream'];
$this->stream = $options['digitalcz-streams']['stream'];

return true;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/CachingStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use PHPUnit\Framework\TestCase;

/**
* @covers \DigitalCz\Streams\CachingStream
*/
class CachingStreamTest extends TestCase
{
public function testUseOriginalIfAvailable(): void
Expand Down
3 changes: 3 additions & 0 deletions tests/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use PHPUnit\Framework\TestCase;

/**
* @covers \DigitalCz\Streams\File
*/
class FileTest extends TestCase
{
public function testOpenNonExistentFile(): void
Expand Down
3 changes: 3 additions & 0 deletions tests/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use LogicException;
use Throwable;

/**
* @covers \DigitalCz\Streams\Stream
*/
class StreamTest extends StreamIntegrationTest
{
public function testConstructorThrowsExceptionOnInvalidArgument(): void
Expand Down
150 changes: 150 additions & 0 deletions tests/StreamWrapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

declare(strict_types=1);

namespace DigitalCz\Streams;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

/**
* @covers \DigitalCz\Streams\StreamWrapper
*/
class StreamWrapperTest extends TestCase
{
public function testResource(): void
{
$stream = Stream::from('foo');
$handle = StreamWrapper::from($stream);
self::assertSame('foo', fread($handle, 3));
self::assertSame(3, ftell($handle));
self::assertSame(3, fwrite($handle, 'bar'));
self::assertSame(0, fseek($handle, 0));
self::assertSame('foobar', fread($handle, 6));
self::assertSame('', fread($handle, 1));
self::assertTrue(feof($handle));

$stBlksize = defined('PHP_WINDOWS_VERSION_BUILD') ? -1 : 0;

self::assertEquals([
'dev' => 0,
'ino' => 0,
'mode' => 33206,
'nlink' => 0,
'uid' => 0,
'gid' => 0,
'rdev' => 0,
'size' => 6,
'atime' => 0,
'mtime' => 0,
'ctime' => 0,
'blksize' => $stBlksize,
'blocks' => $stBlksize,
0 => 0,
1 => 0,
2 => 33206,
3 => 0,
4 => 0,
5 => 0,
6 => 0,
7 => 6,
8 => 0,
9 => 0,
10 => 0,
11 => $stBlksize,
12 => $stBlksize,
], fstat($handle));

self::assertTrue(fclose($handle));
self::assertSame('foobar', (string) $stream);
}

public function testStreamContext(): void
{
StreamWrapper::register();
$stream = Stream::from('foo');

self::assertSame(
'foo',
file_get_contents('digitalcz-streams://stream', false, StreamWrapper::createStreamContext($stream)),
);
}

public function testStreamCast(): void
{
$streams = [
StreamWrapper::from(Stream::from('foo')),
StreamWrapper::from(Stream::from('bar')),
];
$write = null;
$except = null;
self::assertIsInt(stream_select($streams, $write, $except, 0));
}

public function testValidatesStream(): void
{
$stream = $this->createMock(StreamInterface::class);
$stream->expects(self::once())
->method('isReadable')
->willReturn(false);
$stream->expects(self::once())
->method('isWritable')
->willReturn(false);

$this->expectException(InvalidArgumentException::class);
StreamWrapper::from($stream);
}

public function testCanOpenReadonlyStream(): void
{
$stream = $this->createMock(StreamInterface::class);
$stream->expects(self::once())
->method('isReadable')
->willReturn(false);
$stream->expects(self::once())
->method('isWritable')
->willReturn(true);
$r = StreamWrapper::from($stream);
self::assertIsResource($r);
fclose($r);
}

public function testUrlStat(): void
{
StreamWrapper::register();

$stBlksize = defined('PHP_WINDOWS_VERSION_BUILD') ? -1 : 0;

self::assertEquals(
[
'dev' => 0,
'ino' => 0,
'mode' => 0,
'nlink' => 0,
'uid' => 0,
'gid' => 0,
'rdev' => 0,
'size' => 0,
'atime' => 0,
'mtime' => 0,
'ctime' => 0,
'blksize' => $stBlksize,
'blocks' => $stBlksize,
0 => 0,
1 => 0,
2 => 0,
3 => 0,
4 => 0,
5 => 0,
6 => 0,
7 => 0,
8 => 0,
9 => 0,
10 => 0,
11 => $stBlksize,
12 => $stBlksize,
],
stat('digitalcz-streams://stream'),
);
}
}
3 changes: 3 additions & 0 deletions tests/TempFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use PHPUnit\Framework\TestCase;

/**
* @covers \DigitalCz\Streams\TempFile
*/
class TempFileTest extends TestCase
{
public function testCreateAndDelete(): void
Expand Down

0 comments on commit 8ebcbb7

Please sign in to comment.