PHPackages                             phrity/net-mock - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. phrity/net-mock

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

phrity/net-mock
===============

Mock layer for phrity/net-stream

2.4.0(1mo ago)014.5k↓50%2MITPHPPHP ^8.1CI passing

Since Apr 15Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/sirn-se/phrity-net-mock)[ Packagist](https://packagist.org/packages/phrity/net-mock)[ Docs](https://phrity.sirn.se/)[ RSS](/packages/phrity-net-mock/feed)WikiDiscussions main-v2 Synced 1mo ago

READMEChangelog (10)Dependencies (15)Versions (13)Used By (2)

[![Phrity Net Mock](docs/logotype.png)](docs/logotype.png)

[![Build Status](https://github.com/sirn-se/phrity-net-mock/actions/workflows/acceptance.yml/badge.svg)](https://github.com/sirn-se/phrity-net-mock/actions)

Introduction
============

[](#introduction)

Writing tests that use streams is problematic. This library provides a mocking layer for [phrity/net-stream](https://phrity.sirn.se/net-stream/), allowing developers to verify and mock stream interactions.

Installation
------------

[](#installation)

Install with [Composer](https://getcomposer.org/);

```
composer require phrity/net-mock

```

Usage
=====

[](#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
----------------

[](#log-interactions)

By adding a [PSR-3](https://www.php-fig.org/psr/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
-----------------

[](#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
-----------------------------

[](#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
--------

[](#versions)

VersionPHP`2.3``^8.1`[phrity/net-stream v2.3](https://phrity.sirn.se/net-stream/2.3.0)`2.2``^8.1`[phrity/net-stream v2.2](https://phrity.sirn.se/net-stream/2.2.0)`2.1``^8.0`[phrity/net-stream v2.1](https://phrity.sirn.se/net-stream/2.1.0)`2.0``^8.0`[phrity/net-stream v2.0](https://phrity.sirn.se/net-stream/2.0.0)`1.3``^7.4|^8.0`[phrity/net-stream v1.3](https://phrity.sirn.se/net-stream/1.3.0)`1.2``^7.4|^8.0`[phrity/net-stream v1.2](https://phrity.sirn.se/net-stream/1.2.0)`1.1``^7.4|^8.0`[phrity/net-stream v1.1](https://phrity.sirn.se/net-stream/1.1.0)`1.0``^7.4|^8.0`[phrity/net-stream v1.0](https://phrity.sirn.se/net-stream/1.0.0)

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance89

Actively maintained with recent releases

Popularity24

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~107 days

Recently: every ~139 days

Total

11

Last Release

54d ago

Major Versions

1.3.0 → 2.0.02024-03-06

PHP version history (3 changes)1.0.0PHP ^7.4 | ^8.0

2.0.0PHP ^8.0

2.2.0PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4255391?v=4)[Sören Jensen](/maintainers/sirn-se)[@sirn-se](https://github.com/sirn-se)

---

Top Contributors

[![sirn-se](https://avatars.githubusercontent.com/u/4255391?v=4)](https://github.com/sirn-se "sirn-se (70 commits)")

---

Tags

mockphpphp-library

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phrity-net-mock/health.svg)

```
[![Health](https://phpackages.com/badges/phrity-net-mock/health.svg)](https://phpackages.com/packages/phrity-net-mock)
```

###  Alternatives

[ecotone/ecotone

Supporting you in building DDD, CQRS, Event Sourcing applications with ease.

558549.8k17](/packages/ecotone-ecotone)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

728272.9k20](/packages/civicrm-civicrm-core)[j0k3r/php-readability

Automatic article extraction from HTML

186808.8k6](/packages/j0k3r-php-readability)[symfony/ai-platform

PHP library for interacting with AI platform provider.

51927.7k136](/packages/symfony-ai-platform)[symfony/ai-agent

PHP library for building agentic applications.

30536.7k44](/packages/symfony-ai-agent)[spomky-labs/pwa-bundle

Progressive Web App Manifest Generator Bundle for Symfony.

6144.4k1](/packages/spomky-labs-pwa-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
