phrity / net-mock
Mock layer for phrity/net-stream
Installs: 1 680
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- phrity/net-stream: ^2.1
- phrity/net-uri: ^2.0
- psr/log: ^1.0 | ^2.0 | ^3.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^9.0 | ^10.0 | ^11.0
- squizlabs/php_codesniffer: ^3.0
This package is auto-updated.
Last update: 2024-11-14 12:31:11 UTC
README
Introduction
Writing tests that use streams is problematic. This library provides a mocking layer for phrity/net-stream, allowing developers to verify and mock stream interactions.
Installation
Install with Composer;
composer require phrity/net-mock
Usage
The classes in this library are fully compatible with those of the net-stream library. By default, calling the mock classes will propagate into the real implementation classes.
use Phrity\Net\StreamFactory as RealStreamFactory; use Phrity\Net\Mock\StreamFactory as MockStreamFactory; // Your code should allow setting stream classes $my_stream_user = new StreamUsingClass(); $my_stream_user->setStreamfactory($mock ? new MockStreamFactory() : new RealStreamFactory()); $my_stream_user->run();
Log interactions
By adding a PSR-3 compatible logger, all calls will be logged. The library includes a simple EchoLogger, but any compatible logger is usable.
use Phrity\Net\Mock\EchoLogger; use Phrity\Net\Mock\Mock; use Phrity\Net\Mock\StreamFactory; Mock::setLogger(new EchoLogger()); $my_stream_user = new StreamUsingClass(); $my_stream_user->setStreamfactory(new StreamFactory()); $my_stream_user->run();
Mock interactions
By registring a callback handler, all calls will pass through the callback instead.
use Phrity\Net\Mock\Mock; use Phrity\Net\Mock\StreamFactory; Mock::setCallback(function (int $counter, string $method, array $params, callable $default) { // Assert call and parameters // The returned value will be passed back to calling code. // If you want to return the result of original code, use the $default callable return $default(); }); $my_stream_user = new StreamUsingClass(); $my_stream_user->setStreamfactory(new StreamFactory()); $my_stream_user->run();