phrity/net-mock

Mock layer for phrity/net-stream

2.3.0 2025-05-22 08:36 UTC

This package is auto-updated.

Last update: 2025-05-27 06:50:26 UTC


README

Build Status Coverage Status

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, Closure $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($params);
});

$my_stream_user = new StreamUsingClass();
$my_stream_user->setStreamfactory(new StreamFactory());
$my_stream_user->run();

Expect stack in PhpUnit tests

All methods have correspondent expectations that can be used in PhpUnit tests. Classes are imported as Traits, and methods are named by keyword + class name + method name.

use Phrity\Net\Mock\ExpectSocketStreamTrait;
use PHPUnit\Framework\TestCase;

class MyTest extends TestCase
{
    use ExpectSocketStreamTrait;

    public function setUp(): void
    {
        // Prepare except stack
        $this->setUpStack();
    }

    public function tearDown(): void
    {
        // Assert that except stack is now empty
        $this->tearDownStack();
    }

    public funcion myTest(): void
    {
        $this->expectSocketStream();
        $this->expectSocketStreamGetMetadata();
        $this->expectContext();
        $stream = new SocketStream($resource);
    }
}

To assert input

    public funcion myTest(): void
    {
        $this->expectSocketStreamWrite()->addAssert(function (string $method, array $params) {
            // Assert input
            $this->assertEquals('hello', $params[0]);
        });
        $stream->write('hello');
    }

To overwrite return

    public funcion myTest(): void
    {
        $this->expectSocketStreamGetLocalName()->setReturn(function () {
            // Overwrite return
            return 'my-mock-local-name';
        });
        $stream->getLocalName();
    }

Versions

Version PHP
2.3 ^8.1 phrity/net-stream v2.3
2.2 ^8.1 phrity/net-stream v2.2
2.1 ^8.0 phrity/net-stream v2.1
2.0 ^8.0 phrity/net-stream v2.0
1.3 ^7.4|^8.0 phrity/net-stream v1.3
1.2 ^7.4|^8.0 phrity/net-stream v1.2
1.1 ^7.4|^8.0 phrity/net-stream v1.1
1.0 ^7.4|^8.0 phrity/net-stream v1.0