Skip to content

Commit 22ab8bb

Browse files
committed
Exception, container
1 parent 7e83187 commit 22ab8bb

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
"require": {
2626
"php": "^8.1",
2727
"phrity/util-errorhandler": "^1.1",
28+
"phrity/util-interpolator": "^1.1",
2829
"psr/http-factory": "^1.0",
29-
"psr/http-message": "^1.1 | ^2.0"
30+
"psr/http-message": "^1.1 || ^2.0"
3031
},
3132
"require-dev": {
3233
"phpstan/phpstan": "^2.0",
33-
"phpunit/phpunit": "^10.0 | ^11.0 | ^12.0",
34+
"phpunit/phpunit": "^10.0 || ^11.0 || ^12.0",
3435
"phrity/net-uri": "^2.0",
3536
"robiningelbrecht/phpunit-coverage-tools": "^1.9",
3637
"squizlabs/php_codesniffer": "^3.5"

src/StreamException.php

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

33
namespace Phrity\Net;
44

5+
use Phrity\Util\Interpolator\InterpolatorTrait;
56
use RuntimeException;
67
use Throwable;
78

@@ -10,6 +11,8 @@
1011
*/
1112
class StreamException extends RuntimeException
1213
{
14+
use InterpolatorTrait;
15+
1316
// Stream errors
1417
public const STREAM_DETACHED = 1000;
1518
public const NOT_READABLE = 1010;
@@ -73,9 +76,7 @@ class StreamException extends RuntimeException
7376
public function __construct(int $code, array $data = [], Throwable|null $previous = null)
7477
{
7578
$message = self::$messages[$code];
76-
foreach ($data as $key => $content) {
77-
$message = str_replace('{' . $key . '}', $content, $message);
78-
}
79+
$message = $this->interpolate($message, $data);
7980
if ($previous) {
8081
$message .= " ({$previous->getMessage()})";
8182
}

tests/mock/StreamContainer.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/**
4+
* Tests for Net\SocketServer class.
5+
* @package Phrity > Net > Stream
6+
*/
7+
8+
namespace Phrity\Net\Test;
9+
10+
use Phrity\Net\{
11+
Stream,
12+
StreamContainerInterface,
13+
StreamFactory,
14+
};
15+
16+
class StreamContainer implements StreamContainerInterface
17+
{
18+
public function getStream(): Stream
19+
{
20+
$factory = new StreamFactory();
21+
return $factory->createStream('This is a temporary test stream');
22+
}
23+
}

tests/suites/StreamCollectionTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
StreamCollection,
1818
StreamException
1919
};
20+
use Phrity\Net\Test\StreamContainer;
2021
use Phrity\Net\Uri;
2122
use TypeError;
2223

@@ -71,6 +72,24 @@ public function testCollection(): void
7172
$this->assertCount(1, $collection);
7273
}
7374

75+
public function testContainer(): void
76+
{
77+
$container = new StreamContainer();
78+
$collection = new StreamCollection();
79+
$this->assertEquals('@container', $collection->attach($container, '@container'));
80+
81+
$this->assertCount(1, $collection);
82+
foreach ($collection as $key => $item) {
83+
$this->assertSame($container, $item);
84+
}
85+
foreach ($collection->getReadable() as $key => $item) {
86+
$this->assertSame($container, $item);
87+
}
88+
foreach ($collection->getWritable() as $key => $item) {
89+
$this->assertSame($container, $item);
90+
}
91+
}
92+
7493
public function testInvalidTimeout(): void
7594
{
7695
$collection = new StreamCollection();

0 commit comments

Comments
 (0)