<commit_before><?php
namespace Nats\tests\Unit;

use Nats;
use Nats\ConnectionOptions;
use Nats\StreamWrapper;
use Prophecy\Argument;

/**
 * Class ConnectionTest.
 */
class ConnectionTest extends \PHPUnit_Framework_TestCase
{
    /**
     * Client.
     *
     * @var resource Client
     */
    private $c;

    /**
     * Process.
     *
     * @var resource A separated process.
     */
    private static $process;

    /**
     * Gnatsd switch.
     *
     * @var bool Am I using a real or a fake server?
     */
    private static $isGnatsd = false;


    /**
     * SetUp test suite.
     *
     * @return void
     */
    public function setUp()
    {
        $options = new ConnectionOptions();
        $streamWrapper = $this->getMockStreamSocketClient();

        $this->c = new Nats\Connection($options);
        $this->c->setStreamWrapper($streamWrapper->reveal());
        $this->c->connect();
    }

    /**
     * Function for building Socket Mock.
     *
     * @return StreamWrapper
    */
    private function getMockStreamSocketClient()
    {
        $streamWrapper = $this->prophesize("Nats\StreamWrapper");
        $streamWrapper->setStreamTimeout(Argument::any(), Argument::any())->willReturn(true);
        $streamWrapper->getStreamSocketClient(Argument::any(), Argument::any(), Argument::any(), Argument::any(), Argument::any())->will(function ($args) {
            $fileName = "/tmp/".uniqid();
            $f  = fopen($fileName, 'w');
            fwrite($f, "INFO: \n");
            fwrite($f, "-ERR \n");

            return $f;

        });

        return $streamWrapper;
    }


    /**
     * Test Connection.
     *
     * @return void
     */
    public function testConnection()
    {
        // Connect
        $this->c->connect();
        $this->assertTrue($this->c->isConnected());

        // Disconnect
        $this->c->close();
        $this->assertFalse($this->c->isConnected());
    }


    /**
     * Test Ping command.
     *
     * @return void
     */
    public function testPing()
    {
        $this->c->ping();
        $count = $this->c->pingsCount();
        $this->assertInternalType('int', $count);
        $this->assertGreaterThan(0, $count);
        $this->c->close();
    }

    /**
     * Test Publish command.
     *
     * @return void
     */
    public function testPublish()
    {
        $this->c->ping();
        $this->c->publish('foo', 'bar');
        $count = $this->c->pubsCount();
        $this->assertInternalType('int', $count);
        $this->assertGreaterThan(0, $count);
        $this->c->close();
    }

    /**
     * Test Reconnect command.
     * @return void
     */
    public function testReconnect()
    {
        $this->c->reconnect();
        $count = $this->c->reconnectsCount();
        $this->assertInternalType('int', $count);
        $this->assertGreaterThan(0, $count);
        $this->c->close();
    }

    /**
     * Test Subscription command.
     *
     * @return void
     */
    public function testSubscription()
    {
        $callback = function ($message) {
            $this->assertNotNull($message);
            $this->assertEquals($message, 'bar');
        };

        $this->c->subscribe('foo', $callback);
        $this->assertGreaterThan(0, $this->c->subscriptionsCount());
        $subscriptions = $this->c->getSubscriptions();
        $this->assertInternalType('array', $subscriptions);

        $this->c->publish('foo', 'bar');
        $this->assertEquals(1, $this->c->pubsCount());
/*
        $process = new BackgroundProcess('/usr/bin/php ./tests/Util/ClientServerStub.php ');
        $process->run();
*/
        // time_nanosleep(1, 0);
        $this->c->wait(1);
    }

    /**
     * Test Request command.
     *
     * @return void
     */
    public function testRequest()
    {
        $this->c->subscribe(
            "sayhello",
            function ($res) {
                $res->reply("Hello, ".$res->getBody(). " !!!");
            }
        );
<commit_msg>TASK: Add test for queueSubscribe method<commit_after><?php
namespace Nats\tests\Unit;

use Nats;
use Nats\ConnectionOptions;
use Nats\StreamWrapper;
use Prophecy\Argument;

/**
 * Class ConnectionTest.
 */
class ConnectionTest extends \PHPUnit_Framework_TestCase
{
    /**
     * Client.
     *
     * @var resource Client
     */
    private $c;

    /**
     * Process.
     *
     * @var resource A separated process.
     */
    private static $process;

    /**
     * Gnatsd switch.
     *
     * @var bool Am I using a real or a fake server?
     */
    private static $isGnatsd = false;


    /**
     * SetUp test suite.
     *
     * @return void
     */
    public function setUp()
    {
        $options = new ConnectionOptions();
        $streamWrapper = $this->getMockStreamSocketClient();

        $this->c = new Nats\Connection($options);
        $this->c->setStreamWrapper($streamWrapper->reveal());
        $this->c->connect();
    }

    /**
     * Function for building Socket Mock.
     *
     * @return StreamWrapper
    */
    private function getMockStreamSocketClient()
    {
        $streamWrapper = $this->prophesize("Nats\StreamWrapper");
        $streamWrapper->setStreamTimeout(Argument::any(), Argument::any())->willReturn(true);
        $streamWrapper->getStreamSocketClient(Argument::any(), Argument::any(), Argument::any(), Argument::any(), Argument::any())->will(function ($args) {
            $fileName = "/tmp/".uniqid();
            $f  = fopen($fileName, 'w');
            fwrite($f, "INFO: \n");
            fwrite($f, "-ERR \n");

            return $f;

        });

        return $streamWrapper;
    }


    /**
     * Test Connection.
     *
     * @return void
     */
    public function testConnection()
    {
        // Connect
        $this->c->connect();
        $this->assertTrue($this->c->isConnected());

        // Disconnect
        $this->c->close();
        $this->assertFalse($this->c->isConnected());
    }


    /**
     * Test Ping command.
     *
     * @return void
     */
    public function testPing()
    {
        $this->c->ping();
        $count = $this->c->pingsCount();
        $this->assertInternalType('int', $count);
        $this->assertGreaterThan(0, $count);
        $this->c->close();
    }

    /**
     * Test Publish command.
     *
     * @return void
     */
    public function testPublish()
    {
        $this->c->ping();
        $this->c->publish('foo', 'bar');
        $count = $this->c->pubsCount();
        $this->assertInternalType('int', $count);
        $this->assertGreaterThan(0, $count);
        $this->c->close();
    }

    /**
     * Test Reconnect command.
     * @return void
     */
    public function testReconnect()
    {
        $this->c->reconnect();
        $count = $this->c->reconnectsCount();
        $this->assertInternalType('int', $count);
        $this->assertGreaterThan(0, $count);
        $this->c->close();
    }

    /**
     * Test Subscription command.
     *
     * @return void
     */
    public function testSubscription()
    {
        $callback = function ($message) {
            $this->assertNotNull($message);
            $this->assertEquals($message, 'bar');
        };

        $this->c->subscribe('foo', $callback);
        $this->assertGreaterThan(0, $this->c->subscriptionsCount());
        $subscriptions = $this->c->getSubscriptions();
        $this->assertInternalType('array', $subscriptions);

        $this->c->publish('foo', 'bar');
        $this->assertEquals(1, $this->c->pubsCount());
/*
        $process = new BackgroundProcess('/usr/bin/php ./tests/Util/ClientServerStub.php ');
        $process->run();
*/
        // time_nanosleep(1, 0);
        $this->c->wait(1);
    }

    /**
     * Test Queue Subscription command.
     *
     * @return void
     */
    public function testQueueSubscription()
    {
        $callback = function ($message) {
            $this->assertNotNull($message);
            $this->assertEquals($message, 'bar');
        };

        $this->c->queueSubscribe('foo', 'bar', $callback);
        $this->assertGreaterThan(0, $this->c->subscriptionsCount());
        $subscriptions = $this->c->getSubscriptions();
        $this->assertInternalType('array', $subscriptions);

        $this->c->publish('foo', 'bar');
        $this->assertEquals(1, $this->c->pubsCount());
/*
        $process = new BackgroundProcess('/usr/bin/php ./tests/Util/ClientServerStub.php ');
        $process->run();
*/
        // time_nanosleep(1, 0);
        $this->c->wait(1);
    }

    /**
     * Test Request command.
     *
     * @return void
     */
    public function testRequest()
    {
        $this->c->subscribe(
            "sayhello",
            function ($res) {
                $res->reply("Hello, ".$res->getBody(). " !!!");
            }
        );

{'fraction_non_alphanumeric': 0.17463924554027951, 'fraction_numerical': 0.002386092489489831, 'mean_word_length': 1.9959155888359428, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 161, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '791849', 'n_tokens_mistral': 2655, 'n_tokens_neox': 2614, 'n_words': 467}