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\Iterator\Test\Unit;
38
39use Hoa\Iterator as LUT;
40use Hoa\Test;
41
42/**
43 * Class \Hoa\Iterator\Test\Unit\Directory.
44 *
45 * Test suite of the directory iterator.
46 *
47 * @copyright  Copyright © 2007-2017 Hoa community
48 * @license    New BSD License
49 */
50class Directory extends Test\Unit\Suite
51{
52    public function case_classic()
53    {
54        $this
55            ->given(
56                $root = resolve('hoa://Test/Vfs/Root?type=directory'),
57                resolve('hoa://Test/Vfs/Root/A?type=file'),
58                resolve('hoa://Test/Vfs/Root/Aa?type=file'),
59                resolve('hoa://Test/Vfs/Root/Aaa?type=file'),
60                $iterator = new LUT\Directory($root),
61                $result   = []
62            )
63            ->when(function () use ($iterator, &$result) {
64                foreach ($iterator as $key => $file) {
65                    $result[$key] = $file->getFilename();
66
67                    $this
68                        ->object($file)
69                            ->isInstanceOf(LUT\Directory::class);
70                }
71            })
72            ->then
73                ->array($result)
74                    ->isEqualTo([
75                        0 => 'A',
76                        1 => 'Aa',
77                        2 => 'Aaa'
78                    ]);
79    }
80
81    public function case_seek_and_dots()
82    {
83        $this
84            ->given(
85                $root = resolve('hoa://Test/Vfs/Root?type=directory'),
86                resolve('hoa://Test/Vfs/Root/.?type=directory'),
87                resolve('hoa://Test/Vfs/Root/..?type=directory'),
88                resolve('hoa://Test/Vfs/Root/Skip?type=file'),
89                resolve('hoa://Test/Vfs/Root/Gotcha?type=file'),
90                $iterator = new LUT\Directory($root)
91            )
92            ->when($result = $iterator->current())
93            ->then
94                ->boolean($result->isDot())
95                    ->isTrue()
96
97            ->when(
98                $iterator->next(),
99                $result = $iterator->current()
100            )
101            ->then
102                ->boolean($result->isDot())
103                    ->isTrue()
104
105            ->when(
106                $iterator->seek(3),
107                $result = $iterator->current()
108            )
109            ->then
110                ->string($result->getFilename())
111                    ->isEqualTo('Gotcha')
112
113            ->when(
114                $iterator->seek(2),
115                $result = $iterator->current()
116            )
117            ->then
118                ->string($result->getFilename())
119                    ->isEqualTo('Skip');
120    }
121
122    public function case_recursive()
123    {
124        $this
125            ->given(
126                $root = resolve('hoa://Test/Vfs/Root?type=directory'),
127                resolve('hoa://Test/Vfs/Root/A?type=file'),
128                resolve('hoa://Test/Vfs/Root/Aa?type=file'),
129                resolve('hoa://Test/Vfs/Root/Aaa?type=file'),
130                resolve('hoa://Test/Vfs/Root/Foo?type=directory'),
131                resolve('hoa://Test/Vfs/Root/Foo/Bar?type=directory'),
132                resolve('hoa://Test/Vfs/Root/Foo/Bar/B?type=file'),
133                resolve('hoa://Test/Vfs/Root/Foo/Bar/Bb?type=file'),
134                resolve('hoa://Test/Vfs/Root/Foo/Bar/Bbb?type=file'),
135                resolve('hoa://Test/Vfs/Root/Foo/C?type=file'),
136                resolve('hoa://Test/Vfs/Root/Foo/Cc?type=file'),
137                resolve('hoa://Test/Vfs/Root/Foo/Ccc?type=file'),
138                $directory = new LUT\Recursive\Directory($root),
139                $iterator  = new LUT\Recursive\Iterator($directory),
140                $result    = []
141            )
142            ->when(function () use ($iterator, &$result) {
143                foreach ($iterator as $file) {
144                    $result[] = $file->getFilename();
145                }
146            })
147            ->then
148                ->array($result)
149                    ->isEqualTo([
150                        'A',
151                        'Aa',
152                        'Aaa',
153                        'B',
154                        'Bb',
155                        'Bbb',
156                        'C',
157                        'Cc',
158                        'Ccc'
159                    ]);
160    }
161
162    public function case_splFileClassInfo()
163    {
164        $this
165            ->given(
166                $splFileInfo = 'Hoa\Iterator\SplFileInfo',
167                $root        = resolve('hoa://Test/Vfs/Root?type=directory'),
168                resolve('hoa://Test/Vfs/Root/a?type=file'),
169                resolve('hoa://Test/Vfs/Root/b?type=file'),
170                resolve('hoa://Test/Vfs/Root/c?type=file'),
171                resolve('hoa://Test/Vfs/Root/d?type=file'),
172                resolve('hoa://Test/Vfs/Root/e?type=file'),
173                resolve('hoa://Test/Vfs/Root/f?type=file'),
174                $iterator = new LUT\Directory(
175                    $root,
176                    $splFileInfo
177                ),
178                $result   = []
179            )
180            ->when(function () use ($iterator, $splFileInfo, &$result) {
181                foreach ($iterator as $file) {
182                    $this
183                        ->object($file)
184                            ->isInstanceOf($splFileInfo);
185
186                    $result[] = $file->getFilename();
187                }
188            })
189            ->then
190                ->array($result)
191                    ->isEqualTo([
192                        'a',
193                        'b',
194                        'c',
195                        'd',
196                        'e',
197                        'f'
198                    ]);
199    }
200
201    public function case_recursive_splFileClassInfo()
202    {
203        $this
204            ->given(
205                $splFileInfo = 'Hoa\Iterator\SplFileInfo',
206                $root        = resolve('hoa://Test/Vfs/Root?type=directory'),
207                resolve('hoa://Test/Vfs/Root/A?type=directory'),
208                resolve('hoa://Test/Vfs/Root/A/a?type=file'),
209                resolve('hoa://Test/Vfs/Root/A/b?type=file'),
210                resolve('hoa://Test/Vfs/Root/A/c?type=file'),
211                resolve('hoa://Test/Vfs/Root/B?type=directory'),
212                resolve('hoa://Test/Vfs/Root/B/d?type=file'),
213                resolve('hoa://Test/Vfs/Root/B/e?type=file'),
214                resolve('hoa://Test/Vfs/Root/B/c?type=directory'),
215                resolve('hoa://Test/Vfs/Root/B/c/f?type=file'),
216                $directory = new LUT\Recursive\Directory(
217                    $root,
218                    LUT\FileSystem::CURRENT_AS_FILEINFO,
219                    $splFileInfo
220                ),
221                $iterator  = new LUT\Recursive\Iterator($directory),
222                $result    = []
223            )
224            ->when(function () use ($iterator, $splFileInfo, &$result) {
225                foreach ($iterator as $file) {
226                    $this
227                        ->object($file)
228                            ->isInstanceOf($splFileInfo);
229
230                    $result[] = $file->getFilename();
231                }
232            })
233            ->then
234                ->array($result)
235                    ->isEqualTo([
236                        'a',
237                        'b',
238                        'c',
239                        'd',
240                        'e',
241                        'f'
242                    ]);
243    }
244}
245