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 
37 namespace Hoa\Stream\Test\Unit;
38 
39 use Hoa\Stream as LUT;
40 use Hoa\Stream\Context as SUT;
41 use Hoa\Test;
42 
43 /**
44  * Class \Hoa\Stream\Test\Unit\Context.
45  *
46  * Test suite of the context stream class.
47  *
48  * @copyright  Copyright © 2007-2017 Hoa community
49  * @license    New BSD License
50  */
51 class Context extends Test\Unit\Suite
52 {
53     public function case_get_instance_with_empty_id()
54     {
55         $this
56             ->exception(function () {
57                 SUT::getInstance(null);
58             })
59             ->isInstanceOf(LUT\Exception::class);
60     }
61 
62     public function case_get_new_instance()
63     {
64         $this
65             ->when($result = SUT::getInstance('foo'))
66             ->then
67                 ->object($result)
68                     ->isInstanceOf(SUT::class);
69     }
70 
71     public function case_get_new_instances()
72     {
73         $this
74             ->when($result = SUT::getInstance('foo'))
75             ->then
76                 ->object($result)
77                     ->isNotIdenticalTo(SUT::getInstance('bar'));
78     }
79 
80     public function case_get_same_instance()
81     {
82         $this
83             ->when($result = SUT::getInstance('foo'))
84             ->then
85                 ->object($result)
86                     ->isIdenticalTo(SUT::getInstance('foo'));
87     }
88 
89     public function case_get_id()
90     {
91         $this
92             ->given(
93                 $id      = 'foo',
94                 $context = SUT::getInstance($id)
95             )
96             ->when($result = $context->getId())
97             ->then
98                 ->string($result)
99                     ->isEqualTo($id);
100     }
101 
102     public function case_context_exists()
103     {
104         $this
105             ->given(
106                 $id = 'foo',
107                 SUT::getInstance($id)
108             )
109             ->when($result = SUT::contextExists($id))
110             ->then
111                 ->boolean($result)
112                     ->isTrue();
113     }
114 
115     public function case_context_does_not_exist()
116     {
117         $this
118             ->when($result = SUT::contextExists('foo'))
119             ->then
120                 ->boolean($result)
121                     ->isFalse();
122     }
123 
124     public function case_set_options()
125     {
126         $this
127             ->given(
128                 $context = SUT::getInstance('foo'),
129                 $options = ['bar' => ['baz' => 'qux']]
130             )
131             ->when($result = $context->setOptions($options))
132             ->then
133                 ->boolean($result)
134                     ->isTrue();
135     }
136 
137     public function case_get_options()
138     {
139         $this
140             ->given(
141                 $context = SUT::getInstance('foo'),
142                 $options = ['bar' => ['baz' => 'qux']],
143                 $context->setOptions($options)
144             )
145             ->when($result = $context->getOptions())
146             ->then
147                 ->array($result)
148                     ->isEqualTo($options);
149     }
150 
151     public function case_set_parameters()
152     {
153         $this
154             ->given(
155                 $context    = SUT::getInstance('foo'),
156                 $parameters = [
157                     'notificaion' => 'callback',
158                     'options'     => ['bar' => ['baz' => 'qux']]
159                 ]
160             )
161             ->when($result = $context->setParameters($parameters))
162             ->then
163                 ->boolean($result)
164                     ->isTrue();
165     }
166 
167     public function case_get_parameters()
168     {
169         $this
170             ->given(
171                 $context    = SUT::getInstance('foo'),
172                 $parameters = [
173                     'notification' => 'callback',
174                     'options'      => ['bar' => ['baz' => 'qux']]
175                 ],
176                 $context->setParameters($parameters)
177             )
178             ->when($result = $context->getParameters())
179             ->then
180                 ->array($result)
181                     ->isEqualTo($parameters);
182     }
183 
184     public function case_get_context()
185     {
186         $this
187             ->given($context = SUT::getInstance('foo'))
188             ->when($result = $context->getContext())
189             ->then
190                 ->resource($result)
191                     ->isStreamContext();
192     }
193 }
194