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\Unit\Wrapper;
38
39use Hoa\Stream as LUT;
40use Hoa\Stream\Wrapper\Wrapper as SUT;
41use Hoa\Test;
42
43/**
44 * Class \Hoa\Stream\Test\Unit\Wrapper\Wrapper.
45 *
46 * Test suite of the wrapper class.
47 *
48 * @copyright  Copyright © 2007-2017 Hoa community
49 * @license    New BSD License
50 */
51class Wrapper extends Test\Unit\Suite
52{
53    public function case_register()
54    {
55        $this
56            ->given($oldIsRegistered = SUT::isRegistered('foo'))
57            ->when($result = SUT::register('foo', 'StdClass'))
58            ->then
59                ->boolean($result)
60                    ->isTrue()
61                ->boolean($oldIsRegistered)
62                    ->isFalse()
63                ->boolean(SUT::isRegistered('foo'))
64                    ->isTrue();
65    }
66
67    public function case_register_already_registered()
68    {
69        $this
70            ->exception(function () {
71                SUT::register('php', 'ClassName');
72            })
73                ->isInstanceOf(LUT\Exception::class)
74                ->hasMessage('The protocol php is already registered.');
75    }
76
77    public function case_register_implementation_does_not_exist()
78    {
79        $this
80            ->exception(function () {
81                SUT::register('foo', 'ClassName');
82            })
83                ->isInstanceOf(LUT\Exception::class)
84                ->hasMessage(
85                    'Cannot use the ClassName class for the implementation ' .
86                    'of the foo protocol because it is not found.'
87                );
88    }
89
90    public function case_unregister()
91    {
92        $this
93            ->given(
94                SUT::register('foo', 'StdClass'),
95                $oldIsRegistered = SUT::isRegistered('foo')
96            )
97            ->when($result = SUT::unregister('foo'))
98            ->then
99                ->boolean($result)
100                    ->isTrue()
101                ->boolean($oldIsRegistered)
102                    ->isTrue()
103                ->boolean(SUT::isRegistered('foo'))
104                    ->isFalse();
105    }
106
107    public function case_unregister_unregistered_protocol()
108    {
109        $this
110            ->when($result = SUT::unregister('foo'))
111            ->then
112                ->boolean($result)
113                    ->isFalse();
114    }
115
116    public function case_restore_registered_protocol()
117    {
118        $this
119            ->when($result = SUT::restore('php'))
120            ->then
121                ->boolean($result)
122                    ->isTrue();
123    }
124
125    public function case_restore_unregistered_protocol()
126    {
127        $this
128            ->when($result = SUT::restore('foo'))
129            ->then
130                ->boolean($result)
131                    ->isFalse();
132    }
133
134    public function case_is_registered()
135    {
136        $this
137            ->when($result = SUT::isRegistered('php'))
138            ->then
139                ->boolean($result)
140                    ->isTrue();
141    }
142
143    public function case_is_not_registered()
144    {
145        $this
146            ->when($result = SUT::isRegistered('foo'))
147            ->then
148                ->boolean($result)
149                    ->isFalse();
150    }
151
152    public function case_get_registered()
153    {
154        $this
155            ->when($result = SUT::getRegistered())
156            ->then
157                ->array($result)
158                    ->containsValues([
159                        'https',
160                        'php',
161                        'file',
162                        'glob',
163                        'data',
164                        'http',
165                        'hoa'
166                    ]);
167    }
168
169    public function case_get_registered_dynamically()
170    {
171        $this
172            ->given($oldCount = count(SUT::getRegistered()))
173            ->when(
174                SUT::register('foo', \StdClass::class),
175                $result = SUT::getRegistered()
176            )
177            ->then
178                ->integer(count($result))
179                    ->isEqualTo($oldCount + 1)
180
181            ->when(
182                SUT::unregister('foo'),
183                $result = SUT::getRegistered()
184            )
185            ->then
186                ->integer(count($result))
187                    ->isEqualTo($oldCount);
188    }
189}
190