phrity/net-mock
Mocking layer for phrity/net-stream to simplify testing stream-based code. Drop-in compatible stream classes that can log all interactions via any PSR-3 logger and override behavior with callbacks. Includes PHPUnit expectation traits for asserting calls and parameters.
phrity/net-stream, a low-level networking library, which is often used in Laravel for custom HTTP clients, WebSocket handlers, or file I/O wrappers. The mock layer abstracts away real I/O, making it ideal for unit/integration testing of these components.StreamFactoryInterface and bind RealStreamFactory (production) and MockStreamFactory (tests) to the same interface. Use contextual binding (e.g., app()->bindWhen(...)) or environment-based resolution (e.g., config('app.env') === 'testing').uses(PhrityMocks::class)) to auto-configure mocks for all test classes.phrity/net-stream accept StreamFactoryInterface via constructor. If not, use method injection or setters (as shown in the README).Stream::socket()), refactor to inject the factory or create a testing facade that delegates to the mock..env (e.g., STREAM_MOCK=true).beforeEach or PHPUnit’s setUp() to auto-initialize mocks.phrity/net-mock must match phrity/net-stream versions. Risk: Future breaking changes in net-stream may require urgent updates to net-mock.composer.json and monitor GitHub releases for net-stream.tearDownStack()) can lead to flaky tests across test suites.phrity/net-stream? Prioritize mocking for high-risk, flaky tests.net-mock and net-stream versions? Will a composer script or CI check enforce compatibility?phrity/net-stream adds new methods? Is there a backward-compatibility guarantee from the package maintainers?phrity/net-stream?StreamFactoryInterface and bind implementations:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->bind(StreamFactoryInterface::class, function ($app) {
return config('app.env') === 'testing'
? new MockStreamFactory()
: new RealStreamFactory();
});
}
$this->app->bindWhen(
StreamFactoryInterface::class,
fn () => true,
fn () => new MockStreamFactory()
);
PestTestCase:
use Phrity\Net\Mock\Mock;
beforeEach(function () {
Mock::setLogger(new EchoLogger());
Mock::setCallback(fn ($counter, $method, $params, $default) => $default($params));
});
use Phrity\Net\Mock\ExpectSocketStreamTrait;
class MyTest extends TestCase
{
use ExpectSocketStreamTrait;
public function testStreamWrite()
{
$this->expectSocketStreamWrite()->addAssert(fn ($method, $params) =>
$this->assertEquals('data', $params[0])
);
// Test code...
}
}
EchoLogger to write to Laravel’s log channel:
Mock::setLogger(new class implements LoggerInterface {
public function log($level, $message, array $context = []): void
{
Log::channel('single')->info($message, $context);
}
});
phrity/net-stream.StreamFactoryInterface.// Before
$stream = new SocketStream(stream_socket_client(...));
// After
$stream = $this->streamFactory->createSocketStream(...);
// tests/TestCase.php
use Phrity\Net\Mock\Mock;
abstract class TestCase extends PestTestCase
{
protected function setUp(): void
{
Mock::setLogger(new EchoLogger());
parent::setUp();
}
}
net-mock@2.x. Ensure compatibility with Laravel 9+.net-mock@1.x (PHP 7.4).phrity/net-stream, ensure no conflicts in stream handling.net-mock can coexist or if a hybrid approach is needed.phrity/net-stream classes (e.g., SocketStream, Context). Custom stream types may require additional mock implementations.phrity/net-mock to composer.json.StreamFactoryInterface and bind implementations.StreamFactoryInterface into stream-using classes.How can I help you explore Laravel packages today?