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

WritableFifoTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 2
cbo 3
dl 0
loc 73
rs 10
1
<?php
2
namespace Slince\Process\Tests\Pipe;
3
4
use PHPUnit\Framework\TestCase;
5
use Slince\Process\Pipe\WritableFifo;
6
use Slince\Process\Tests\Utils;
7
8
class WritableFifoTest extends TestCase
9
{
10
    protected $lastPd;
11
12
    public function setUp()
13
    {
14
        file_exists('/tmp/test2.pipe') && unlink('/tmp/test2.pipe');
15
    }
16
17
    public function testSimpleWrite()
18
    {
19
        $fifo = new WritableFifo('/tmp/test2.pipe');
20
        $fifo->write('hello');
21
        $readFifo = Utils::makeNativeReadFifo('/tmp/test2.pipe');
22
        $this->assertEquals('hello', fread($readFifo, 5));
23
        $fifo->close();
24
    }
25
26
    public function testNonBlockingWrite()
27
    {
28
        $fifo = new WritableFifo('/tmp/test2.pipe', false);
29
        $bytes = $fifo->write(str_repeat('a', 65500));
30
        $this->assertEquals(65500, $bytes);
31
32
        $this->syncExecute(sprintf("php %s %d %d", __DIR__ . '/ReadFifo.php', 1024, 1));
33
34
        $bytes = $fifo->write(str_repeat('a', 65500));
35
        $this->assertEquals(0, $bytes);
36
37
        $bytes = $fifo->write(str_repeat('a', 35));
38
        $this->assertEquals(35, $bytes);
39
40
        $fifo->close();
41
    }
42
43
    public function testBlockingWrite()
44
    {
45
        $fifo = new WritableFifo('/tmp/test2.pipe', true);
46
        $bytes = $fifo->write(str_repeat('a', 65500));
47
        $this->assertEquals(65500, $bytes);
48
49
        $this->syncExecute(sprintf("php %s %d %d", __DIR__ . '/ReadFifo.php', 65500, 1));
50
51
        $bytes = $fifo->write(str_repeat('a', 65500));
52
        $this->assertEquals(65500, $bytes);
53
54
        $fifo->close();
55
    }
56
57
    public function testGetStream()
58
    {
59
        $fifo = new WritableFifo('/tmp/test2.pipe');
60
        $this->assertTrue(is_resource($fifo->getStream()));
61
    }
62
63
    public function testIsBlocking()
64
    {
65
        $fifo = new WritableFifo('/tmp/test2.pipe');
66
        $this->assertTrue($fifo->isBlocking());
67
        $fifo->setBlocking(false);
68
        $this->assertFalse($fifo->isBlocking());
69
    }
70
71
    protected function syncExecute($command)
72
    {
73
        $this->lastPd = Utils::asyncExecute($command);
74
    }
75
76
    public function tearDown()
77
    {
78
        is_resource($this->lastPd) && pclose($this->lastPd);
79
    }
80
}
81