PHP code example of phrity / net-mock

1. Go to this page and download the library: Download phrity/net-mock library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phrity / net-mock example snippets


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();

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();

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();


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);
    }
}

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

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