1<?php
2
3abstract class Swift_Transport_StreamBuffer_AbstractStreamBufferAcceptanceTest extends \PHPUnit_Framework_TestCase
4{
5    protected $_buffer;
6
7    abstract protected function _initializeBuffer();
8
9    protected function setUp()
10    {
11        if (true == getenv('TRAVIS')) {
12            $this->markTestSkipped(
13                'Will fail on travis-ci if not skipped due to travis blocking '.
14                'socket mailing tcp connections.'
15             );
16        }
17
18        $this->_buffer = new Swift_Transport_StreamBuffer(
19            $this->getMockBuilder('Swift_ReplacementFilterFactory')->getMock()
20        );
21    }
22
23    public function testReadLine()
24    {
25        $this->_initializeBuffer();
26
27        $line = $this->_buffer->readLine(0);
28        $this->assertRegExp('/^[0-9]{3}.*?\r\n$/D', $line);
29        $seq = $this->_buffer->write("QUIT\r\n");
30        $this->assertTrue((bool) $seq);
31        $line = $this->_buffer->readLine($seq);
32        $this->assertRegExp('/^[0-9]{3}.*?\r\n$/D', $line);
33        $this->_buffer->terminate();
34    }
35
36    public function testWrite()
37    {
38        $this->_initializeBuffer();
39
40        $line = $this->_buffer->readLine(0);
41        $this->assertRegExp('/^[0-9]{3}.*?\r\n$/D', $line);
42
43        $seq = $this->_buffer->write("HELO foo\r\n");
44        $this->assertTrue((bool) $seq);
45        $line = $this->_buffer->readLine($seq);
46        $this->assertRegExp('/^[0-9]{3}.*?\r\n$/D', $line);
47
48        $seq = $this->_buffer->write("QUIT\r\n");
49        $this->assertTrue((bool) $seq);
50        $line = $this->_buffer->readLine($seq);
51        $this->assertRegExp('/^[0-9]{3}.*?\r\n$/D', $line);
52        $this->_buffer->terminate();
53    }
54
55    public function testBindingOtherStreamsMirrorsWriteOperations()
56    {
57        $this->_initializeBuffer();
58
59        $is1 = $this->_createMockInputStream();
60        $is2 = $this->_createMockInputStream();
61
62        $is1->expects($this->at(0))
63            ->method('write')
64            ->with('x');
65        $is1->expects($this->at(1))
66            ->method('write')
67            ->with('y');
68        $is2->expects($this->at(0))
69            ->method('write')
70            ->with('x');
71        $is2->expects($this->at(1))
72            ->method('write')
73            ->with('y');
74
75        $this->_buffer->bind($is1);
76        $this->_buffer->bind($is2);
77
78        $this->_buffer->write('x');
79        $this->_buffer->write('y');
80    }
81
82    public function testBindingOtherStreamsMirrorsFlushOperations()
83    {
84        $this->_initializeBuffer();
85
86        $is1 = $this->_createMockInputStream();
87        $is2 = $this->_createMockInputStream();
88
89        $is1->expects($this->once())
90            ->method('flushBuffers');
91        $is2->expects($this->once())
92            ->method('flushBuffers');
93
94        $this->_buffer->bind($is1);
95        $this->_buffer->bind($is2);
96
97        $this->_buffer->flushBuffers();
98    }
99
100    public function testUnbindingStreamPreventsFurtherWrites()
101    {
102        $this->_initializeBuffer();
103
104        $is1 = $this->_createMockInputStream();
105        $is2 = $this->_createMockInputStream();
106
107        $is1->expects($this->at(0))
108            ->method('write')
109            ->with('x');
110        $is1->expects($this->at(1))
111            ->method('write')
112            ->with('y');
113        $is2->expects($this->once())
114            ->method('write')
115            ->with('x');
116
117        $this->_buffer->bind($is1);
118        $this->_buffer->bind($is2);
119
120        $this->_buffer->write('x');
121
122        $this->_buffer->unbind($is2);
123
124        $this->_buffer->write('y');
125    }
126
127    private function _createMockInputStream()
128    {
129        return $this->getMockBuilder('Swift_InputByteStream')->getMock();
130    }
131}
132