1<?php
2
3/**
4 * Hoa
5 *
6 *
7 * @license
8 *
9 * New BSD License
10 *
11 * Copyright © 2007-2017, Hoa community. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 *     * Redistributions of source code must retain the above copyright
16 *       notice, this list of conditions and the following disclaimer.
17 *     * Redistributions in binary form must reproduce the above copyright
18 *       notice, this list of conditions and the following disclaimer in the
19 *       documentation and/or other materials provided with the distribution.
20 *     * Neither the name of the Hoa nor the names of its contributors may be
21 *       used to endorse or promote products derived from this software without
22 *       specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37namespace Hoa\Stream\Test\Integration;
38
39use Hoa\Event;
40use Hoa\Stream as LUT;
41use Hoa\Test;
42
43/**
44 * Class \Hoa\Stream\Test\Integration\Stream.
45 *
46 * Test suite of the stream class.
47 *
48 * @copyright  Copyright © 2007-2017 Hoa community
49 * @license    New BSD License
50 */
51class Stream extends Test\Integration\Suite
52{
53    public function case_notifications()
54    {
55        $self = $this;
56
57        $this
58            ->given(
59                $port = mt_rand(10000, 12000),
60                exec(
61                    sprintf(
62                        'php -S 127.0.0.1:%d -t %s > /dev/null 2>&1 & echo $! && sleep 0.2',
63                        $port,
64                        dirname(__DIR__) . DS . 'Fixtures'
65                    ),
66                    $outputs
67                ),
68                $pid    = $outputs[0],
69                $stream = new SUT('http://127.0.0.1:' . $port, null, true),
70
71                $stream->on(
72                    'connect',
73                    function (Event\Bucket $bucket) use ($self, &$connectCalled) {
74                        $connectCalled = true;
75                        $data          = $bucket->getData();
76
77                        $self
78                            ->array($data)
79                                ->isEqualTo([
80                                    'code'        => 0,
81                                    'severity'    => 0,
82                                    'message'     => null,
83                                    'transferred' => 0,
84                                    'max'         => 0
85                                ]);
86                    }
87                ),
88                $stream->on(
89                    'mimetype',
90                    function (Event\Bucket $bucket) use ($self, &$mimetypeCalled) {
91                        $mimetypeCalled = true;
92                        $data           = $bucket->getData();
93
94                        $self
95                            ->array($data)
96                                ->isEqualTo([
97                                    'code'        => 0,
98                                    'severity'    => 0,
99                                    'message'     => 'text/html; charset=UTF-8',
100                                    'transferred' => 0,
101                                    'max'         => 0
102                                ]);
103                    }
104                ),
105                $stream->on(
106                    'size',
107                    function (Event\Bucket $bucket) use ($self, &$sizeCalled) {
108                        $sizeCalled = true;
109                        $data       = $bucket->getData();
110
111                        $self
112                            ->array($data)
113                            ->isEqualTo([
114                                'code'        => 0,
115                                'severity'    => 0,
116                                'message'     => 'Content-Length: 14',
117                                'transferred' => 0,
118                                'max'         => 14
119                            ]);
120                    }
121                ),
122                $stream->on(
123                    'progress',
124                    function (Event\Bucket $bucket) use ($self, &$progressCalled) {
125                        $progressCalled = true;
126                        $data           = $bucket->getData();
127
128                        $self
129                            ->array($data)
130                            ->isEqualTo([
131                                'code'        => 0,
132                                'severity'    => 0,
133                                'message'     => null,
134                                'transferred' => 0,
135                                'max'         => 14
136                            ]);
137                    }
138                )
139            )
140            ->when($stream->open())
141            ->then
142                ->boolean($connectCalled)
143                    ->isTrue()
144                ->boolean($mimetypeCalled)
145                    ->isTrue()
146                ->boolean($sizeCalled)
147                    ->isTrue()
148                ->boolean($progressCalled)
149                    ->isTrue()
150                ->let(!empty($pid) && exec('kill ' . $pid));
151    }
152}
153
154class SUT extends LUT\Stream
155{
156    protected function &_open($streamName, LUT\Context $context = null)
157    {
158        if (null === $context) {
159            $out = fopen($streamName, 'rb');
160        } else {
161            $out = fopen($streamName, 'rb', false, $context->getContext());
162        }
163
164        return $out;
165    }
166
167    protected function _close()
168    {
169        return fclose($this->getStream());
170    }
171}
172