aboutsummaryrefslogtreecommitdiffhomepage
path: root/assets/php/vendor/react/socket/tests/ConnectionTest.php
blob: d3563dfc46776af708f5216a317df531208e0c3c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php

namespace React\Tests\Socket;

use React\Socket\Connection;

class ConnectionTest extends TestCase
{
    public function testCloseConnectionWillCloseSocketResource()
    {
        if (defined('HHVM_VERSION')) {
            $this->markTestSkipped('HHVM does not support socket operation on test memory stream');
        }

        $resource = fopen('php://memory', 'r+');
        $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();

        $connection = new Connection($resource, $loop);
        $connection->close();

        $this->assertFalse(is_resource($resource));
    }

    public function testCloseConnectionWillRemoveResourceFromLoopBeforeClosingResource()
    {
        if (defined('HHVM_VERSION')) {
            $this->markTestSkipped('HHVM does not support socket operation on test memory stream');
        }

        $resource = fopen('php://memory', 'r+');
        $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
        $loop->expects($this->once())->method('addWriteStream')->with($resource);

        $onRemove = null;
        $loop->expects($this->once())->method('removeWriteStream')->with($this->callback(function ($param) use (&$onRemove) {
            $onRemove = is_resource($param);
            return true;
        }));

        $connection = new Connection($resource, $loop);
        $connection->write('test');
        $connection->close();

        $this->assertTrue($onRemove);
        $this->assertFalse(is_resource($resource));
    }
}