Skip to content

Commit 167c671

Browse files
committed
test EmptyStream and UriResolver
1 parent e170b99 commit 167c671

File tree

5 files changed

+47
-26
lines changed

5 files changed

+47
-26
lines changed

src/Psr18/UriResolver/UriResolver.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ class UriResolver implements UriResolverInterface
99
{
1010
/**
1111
* @param UriFactoryInterface $uriFactory
12-
* @param bool $strict
1312
*/
1413
public function __construct(
15-
protected UriFactoryInterface $uriFactory,
16-
protected bool $strict = true
14+
protected UriFactoryInterface $uriFactory
1715
)
1816
{
1917
}
@@ -23,10 +21,6 @@ public function __construct(
2321
*/
2422
public function resolve(UriInterface $baseUri, UriInterface $relativeUri): UriInterface
2523
{
26-
if (!$this->strict && $relativeUri->getScheme() === $baseUri->getScheme()) {
27-
$relativeUri = $relativeUri->withScheme("");
28-
}
29-
3024
if ($relativeUri->getScheme() !== "") {
3125
return $relativeUri;
3226
}

src/Psr7/Stream/StreamMetaDataTrait.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44

55
trait StreamMetaDataTrait
66
{
7+
/**
8+
* @return string|null
9+
*/
10+
protected function approximateMode(): ?string
11+
{
12+
if ($this->isReadable() && $this->isWritable()) {
13+
return "r+";
14+
}
15+
if ($this->isReadable()) {
16+
return "r";
17+
}
18+
if ($this->isWritable()) {
19+
return "w";
20+
}
21+
return null;
22+
}
23+
724
/**
825
* @inheritDoc
926
*/

src/Psr7/Stream/StringStream.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -164,21 +164,4 @@ public function getContents(): string
164164
}
165165
return substr($this->data, $this->position);
166166
}
167-
168-
/**
169-
* @return string|null
170-
*/
171-
protected function approximateMode(): ?string
172-
{
173-
if ($this->isReadable() && $this->isWritable()) {
174-
return "r+";
175-
}
176-
if ($this->isReadable()) {
177-
return "r";
178-
}
179-
if ($this->isWritable()) {
180-
return "w";
181-
}
182-
return null;
183-
}
184167
}

tests/StreamTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests;
44

5+
use Aternos\CurlPsr\Psr7\Stream\EmptyStream;
56
use Aternos\CurlPsr\Psr7\Stream\Stream;
67
use PHPUnit\Framework\TestCase;
78
use ReflectionObject;
@@ -254,4 +255,29 @@ public function testStatFails(): void
254255

255256
$this->assertNull($stream->getSize());
256257
}
258+
259+
public function testEmptyStream(): void
260+
{
261+
$stream = new EmptyStream();
262+
$this->assertEquals("", (string)$stream);
263+
$this->assertNull($stream->detach());
264+
$this->assertEquals(0, $stream->getSize());
265+
$this->assertEquals(0, $stream->tell());
266+
$this->assertTrue($stream->eof());
267+
$this->assertTrue($stream->isSeekable());
268+
$this->assertTrue($stream->isReadable());
269+
$this->assertFalse($stream->isWritable());
270+
$this->assertIsArray($stream->getMetadata());
271+
$this->assertEquals(0, $stream->getMetadata("unread_bytes"));
272+
$this->assertEquals("", $stream->read(10));
273+
$this->assertEquals("", $stream->getContents());
274+
275+
// Do nothing
276+
$stream->seek(2);
277+
$stream->rewind();
278+
$stream->close();
279+
280+
$this->expectException(RuntimeException::class);
281+
$stream->write("test");
282+
}
257283
}

tests/UriResolverTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ class UriResolverTest extends TestCase
3636
#[TestWith(["../../g", "http://a/g"])]
3737
#[TestWith(["../../../g", "http://a/g"])]
3838
#[TestWith(["../../../../g", "http://a/g"])]
39-
public function testRelativeResolution(string $relativeUri, string $expectedUri)
39+
#[TestWith(["abc", "http://a/abc", "http://a"])]
40+
public function testRelativeResolution(string $relativeUri, string $expectedUri, string $baseUriString = self::BASE_URI): void
4041
{
4142
$factory = new Psr17Factory();
4243
$resolver = new UriResolver($factory);
43-
$baseUri = $factory->createUri(self::BASE_URI);
44+
$baseUri = $factory->createUri($baseUriString);
4445
$relativeUri = $factory->createUri($relativeUri);
4546
$resolvedUri = $resolver->resolve($baseUri, $relativeUri);
4647
$this->assertEquals($expectedUri, (string)$resolvedUri);

0 commit comments

Comments
 (0)