Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Net Mock Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Build 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
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
craftcms/url-validator
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony