Completed
Push — master ( 8cc4ca...2aa5db )
by Taosikai
11:59
created

ReadableFifoTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 2
cbo 3
dl 0
loc 63
rs 10
1
<?php
2
namespace Slince\Process\Tests\Pipe;
3
4
use PHPUnit\Framework\TestCase;
5
use Slince\Process\Exception\RuntimeException;
6
use Slince\Process\Pipe\ReadableFifo;
7
use Slince\Process\Tests\Utils;
8
9
class ReadableFifoTest extends TestCase
10
{
11
    protected $lastPd;
12
13
    public function setUp()
14
    {
15
        file_exists('/tmp/test1.pipe') && unlink('/tmp/test1.pipe');
16
    }
17
18
    public function testSimpleRead()
19
    {
20
        $writeFifo = Utils::makeNativeWriteFifo('/tmp/test1.pipe');
21
        $writeBytes = fwrite($writeFifo, 'hello');
22
        $this->assertEquals(5, $writeBytes);
23
        $fifo = new ReadableFifo('/tmp/test1.pipe', false);
24
        $this->assertEquals('hello', $fifo->read());
25
    }
26
27
    public function testNonBlockingRead()
28
    {
29
        $this->syncExecute(sprintf("php %s %s %d", __DIR__ . '/WriteFifo.php', 'hello', 2));
30
        $fifo = new ReadableFifo('/tmp/test1.pipe', false);
31
        $this->assertEmpty($fifo->read());
32
    }
33
34
    public function testBlockingRead()
35
    {
36
        $this->syncExecute(sprintf("php %s %s %d", __DIR__ . '/WriteFifo.php', 'hello', 2));
37
        $fifo = new ReadableFifo('/tmp/test1.pipe', true);
38
        $this->assertEquals('hello', $fifo->read());
39
    }
40
41
    public function testWrite()
42
    {
43
        $fifo = new ReadableFifo('/tmp/test1.pipe');
44
        $this->setExpectedException(RuntimeException::class);
45
        $fifo->write('some message');
46
    }
47
48
    public function testGetStream()
49
    {
50
        $fifo = new ReadableFifo('/tmp/test1.pipe');
51
        $this->assertTrue(is_resource($fifo->getStream()));
52
    }
53
54
    public function testIsBlocking()
55
    {
56
        $fifo = new ReadableFifo('/tmp/test1.pipe');
57
        $this->assertTrue($fifo->isBlocking());
58
        $fifo->setBlocking(false);
59
        $this->assertFalse($fifo->isBlocking());
60
    }
61
62
    protected function syncExecute($command)
63
    {
64
        $this->lastPd = Utils::asyncExecute($command);
65
    }
66
67
    public function tearDown()
68
    {
69
        is_resource($this->lastPd) && pclose($this->lastPd);
70
    }
71
}
72