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\Consistency\Test\Unit;
38
39use Hoa\Consistency\Consistency as SUT;
40use Hoa\Test;
41
42/**
43 * Class \Hoa\Consistency\Test\Unit\Consistency.
44 *
45 * Test suite of the consistency class.
46 *
47 * @copyright  Copyright © 2007-2017 Hoa community
48 * @license    New BSD License
49 */
50class Consistency extends Test\Unit\Suite
51{
52    protected function _entity_exists_with_xxx($class, $interface, $trait)
53    {
54        $this
55            ->given(
56                $this->function->class_exists     = $class,
57                $this->function->interface_exists = $interface,
58                $this->function->trait_exists     = $trait
59            )
60            ->when($result = SUT::entityExists('foo'))
61            ->then
62                ->boolean($result)
63                    ->isTrue();
64    }
65
66    public function case_entity_exists_with_class()
67    {
68        return $this->_entity_exists_with_xxx(true, false, false);
69    }
70
71    public function case_entity_exists_with_interface()
72    {
73        return $this->_entity_exists_with_xxx(false, true, false);
74    }
75
76    public function case_entity_exists_with_trait()
77    {
78        return $this->_entity_exists_with_xxx(false, false, true);
79    }
80
81    public function case_entity_does_not_exists()
82    {
83        $this
84            ->given(
85                $this->function->class_exists     = false,
86                $this->function->interface_exists = false,
87                $this->function->trait_exists     = false
88            )
89            ->when($result = SUT::entityExists('foo'))
90            ->then
91                ->boolean($result)
92                    ->isFalse();
93    }
94
95    public function case_get_entity_shortest_name()
96    {
97        $this
98            ->when($result = SUT::getEntityShortestName('Foo\Bar\Bar'))
99            ->then
100                ->string($result)
101                    ->isEqualTo('Foo\Bar');
102    }
103
104    public function case_get_entity_shortest_name_with_already_the_shortest()
105    {
106        $this
107            ->when($result = SUT::getEntityShortestName('Foo\Bar'))
108            ->then
109                ->string($result)
110                    ->isEqualTo('Foo\Bar');
111    }
112
113    public function case_get_entity_shortest_name_with_no_namespace()
114    {
115        $this
116            ->when($result = SUT::getEntityShortestName('Foo'))
117            ->then
118                ->string($result)
119                    ->isEqualTo('Foo');
120    }
121
122    public function case_is_keyword()
123    {
124        $this
125            ->given(
126                $keywords = [
127                    '__HALT_COMPILER',
128                    'abstract',
129                    'and',
130                    'array',
131                    'as',
132                    'bool',
133                    'break',
134                    'callable',
135                    'case',
136                    'catch',
137                    'class',
138                    'clone',
139                    'const',
140                    'continue',
141                    'declare',
142                    'default',
143                    'die',
144                    'do',
145                    'echo',
146                    'else',
147                    'elseif',
148                    'empty',
149                    'enddeclare',
150                    'endfor',
151                    'endforeach',
152                    'endif',
153                    'endswitch',
154                    'endwhile',
155                    'eval',
156                    'exit',
157                    'extends',
158                    'false',
159                    'final',
160                    'float',
161                    'for',
162                    'foreach',
163                    'function',
164                    'global',
165                    'goto',
166                    'if',
167                    'implements',
168                    'include',
169                    'include_once',
170                    'instanceof',
171                    'insteadof',
172                    'int',
173                    'interface',
174                    'isset',
175                    'list',
176                    'mixed',
177                    'namespace',
178                    'new',
179                    'null',
180                    'numeric',
181                    'object',
182                    'or',
183                    'print',
184                    'private',
185                    'protected',
186                    'public',
187                    'require',
188                    'require_once',
189                    'resource',
190                    'return',
191                    'static',
192                    'string',
193                    'switch',
194                    'throw',
195                    'trait',
196                    'true',
197                    'try',
198                    'unset',
199                    'use',
200                    'var',
201                    'void',
202                    'while',
203                    'xor',
204                    'yield',
205                    '__CLASS__',
206                    '__DIR__',
207                    '__FILE__',
208                    '__FUNCTION__',
209                    '__LINE__',
210                    '__METHOD__',
211                    '__NAMESPACE__',
212                    '__TRAIT__'
213                ]
214            )
215            ->when(function () use ($keywords) {
216                foreach ($keywords as $keyword) {
217                    $this
218                        ->boolean(SUT::isKeyword($keyword))
219                            ->isTrue();
220                }
221            });
222    }
223
224    public function case_is_identifier()
225    {
226        $this
227            ->given($_identifier = $this->realdom->regex('#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x80-\xff]*$#'))
228            ->when(function () use ($_identifier) {
229                foreach ($this->sampleMany($_identifier, 1000) as $identifier) {
230                    $this
231                        ->boolean(SUT::isIdentifier($identifier))
232                            ->isTrue();
233                }
234            });
235    }
236
237    public function case_register_shutdown_function()
238    {
239        $self = $this;
240
241        $this
242            ->given(
243                $callable = function () {
244                },
245                $this->function->register_shutdown_function = function ($_callable) use (&$called, $self, &$callable) {
246                    $called = true;
247
248                    $self
249                        ->variable($_callable)
250                            ->isEqualTo($callable);
251
252                    return true;
253                }
254            )
255            ->when($result = SUT::registerShutdownFunction($callable))
256            ->then
257                ->boolean($result)
258                    ->isTrue();
259    }
260
261    public function case_get_php_binary_with_constant()
262    {
263        $this
264            ->given($this->constant->PHP_BINARY = '/foo/php')
265            ->when($result = SUT::getPHPBinary())
266            ->then
267                ->string($result)
268                    ->isEqualTo('/foo/php');
269    }
270
271    public function case_get_php_binary_with_server()
272    {
273        $this
274            ->given(
275                $this->function->defined = false,
276                $_SERVER['_'] = '/bar/php'
277            )
278            ->when($result = SUT::getPHPBinary())
279            ->then
280                ->string($result)
281                    ->isEqualTo('/bar/php');
282    }
283
284    public function case_get_php_binary_with_bin_directory()
285    {
286        unset($_SERVER['_']);
287
288        $this
289            ->given(
290                $this->function->defined = false,
291                $this->function->file_exists = true,
292                $this->function->realpath = '/baz/php'
293            )
294            ->when($result = SUT::getPHPBinary())
295            ->then
296                ->string($result)
297                    ->isEqualTo('/baz/php');
298    }
299
300    public function case_uuid()
301    {
302        $this
303            ->given($this->function->mt_rand = 42)
304            ->when($result = SUT::uuid())
305            ->then
306                ->string($result)
307                    ->isEqualTo('002a002a-002a-402a-802a-002a002a002a');
308    }
309
310    public function case_uuid_all_differents()
311    {
312        $this
313            ->when(function () {
314                $uuids = [];
315
316                for ($i = 0; $i < 10000; ++$i) {
317                    $uuids[] = SUT::uuid();
318                }
319
320                $this
321                    ->integer(count($uuids))
322                        ->isEqualTo(count(array_unique($uuids)));
323            });
324    }
325}
326