…lines 1–34 unchanged (34 lines)
35 * @license BSD License
36 */
37
38namespace TheSeer\DirectoryScanner\Tests {
39
40 use TheSeer\DirectoryScanner\DirectoryScanner;
41
42 /**
43 * Unit tests for DirectoryScanner class
44 *
45 * @author Arne Blankerts <arne@blankerts.de>
46 * @copyright Arne Blankerts <arne@blankerts.de>, All rights reserved.
47 */
48 class DirectoryScannerTest extends \PHPUnit_Framework_TestCase {
49
50 /**
51 * Test if enabling following symbolic links works.
52 */
53 public function testSetFollowSymlinks() {
54 $tmp = new DirectoryScanner();
55 $this->assertFalse($tmp->isFollowSymlinks());
56 $tmp->setFollowSymlinks(TRUE);
57 $this->assertTrue($tmp->isFollowSymlinks());
58 $tmp->setFollowSymlinks(FALSE);
59 $this->assertFalse($tmp->isFollowSymlinks());
60 }
61
62 /**
63 * @expectedException \TheSeer\DirectoryScanner\Exception
64 * @expectedExceptionCode \TheSeer\DirectoryScanner\Exception::InvalidFlag
65 */
66 public function testSettingInvalidFlagThrowsException() {
67 $tmp = new DirectoryScanner();
68 $tmp->setFlag(-1);
69 }
70
71 /**
72 * @expectedException \TheSeer\DirectoryScanner\Exception
73 * @expectedExceptionCode \TheSeer\DirectoryScanner\Exception::InvalidFlag
74 */
75 public function testUnSettingInvalidFlagThrowsException() {
76 $tmp = new DirectoryScanner();
77 $tmp->unsetFlag(-1);
78 }
79
80 /**
81 * Set the include to be matching for *.txt via setIncludes
82 *
83 */
84 public function testSetIncludes() {
85 $test = array('*.txt');
86 $tmp = new DirectoryScanner();
87 $tmp->setIncludes($test);
88 $this->assertEquals($test, $tmp->getIncludes());
89 }
90
91 /**
92 * Set an exclude match on *.txt
93 *
94 */
95 public function testSetExcludes() {
96 $test = array('*.txt');
97 $tmp = new DirectoryScanner();
98 $tmp->setExcludes($test);
99 $this->assertEquals($test, $tmp->getExcludes());
100 }
101
102 /**
103 * Adding multiple matches include matches individually
104 *
105 */
106 public function testAddInclude() {
107 $tmp = new DirectoryScanner();
108 $tmp->addInclude('*.txt');
109 $tmp->addInclude('*.xml');
110
111 $this->assertEquals(2, count($tmp->getIncludes()));
112 $this->assertEquals(array('*.txt', '*.xml'), $tmp->getIncludes());
113 }
114
115 /**
116 * Adding multiple exclude maches individually
117 *
118 */
119 public function testAddExclude() {
120 $tmp = new DirectoryScanner();
121 $tmp->addExclude('*.txt');
122 $tmp->addExclude('*.xml');
123
124 $this->assertEquals(array('*.txt', '*.xml'), $tmp->getExcludes());
125 }
126
127 /**
128 * Trying to scan a non existend directory should throw an exception
129 *
130 * @expectedException \TheSeer\DirectoryScanner\Exception
131 */
132 public function testScanOfNonExistendPath() {
133 $tmp = new DirectoryScanner();
134 $tmp(__DIR__ . '/_data//not/existing');
135 }
136
137 /**
138 * Recursivly find all files within given test folder without any filter
139 */
140 public function testRecursiveFindAllFilesInFolder() {
141 $tmp = new DirectoryScanner();
142 $x = $tmp->getFiles(__DIR__ . '/_data');
143 $this->assertEquals(9, count($x));
144 }
145
146 /**
147 * Non-Recursivly find all files within given test folder without any filter
148 */
149 public function testNonRecursiveFindAllFilesInFolder() {
150 $tmp = new DirectoryScanner();
151 $x = $tmp->getFiles(__DIR__ . '/_data', FALSE);
152 $this->assertEquals(3, count($x));
153 }
154
155 /**
156 * Recursivly find *.xml files within test folder
157 */
158 public function testRecursiveFindXMLFilesInFolder() {
159 $tmp = new DirectoryScanner();
160 $tmp->addInclude('*.xml');
161 $x = $tmp->getFiles(__DIR__ . '/_data');
162 $this->assertEquals(3, count($x));
163 }
164
165 /**
166 * Non-Recursivly find *.xml files within test folder
167 */
168 public function testNonRecursiveFindXMLFilesInFolder() {
169 $tmp = new DirectoryScanner();
170 $tmp->addInclude('*.xml');
171 $x = $tmp->getFiles(__DIR__ . '/_data', FALSE);
172 $this->assertEquals(2, count($x));
173 }
174
175 /**
176 * Recursivly find all files, not matching *.xml
177 */
178 public function testRecursiveFindByExclude() {
179 $tmp = new DirectoryScanner();
180 $tmp->addExclude('*.xml');
181 $x = $tmp->getFiles(__DIR__ . '/_data');
182 $this->assertEquals(6, count($x));
183 }
184
185 /**
186 * Find all files matching *.txt and not being in 'nested' folder
187 */
188 public function testRecursiveFindByIncludeAndExclude() {
189 $tmp = new DirectoryScanner();
190 $tmp->addInclude('*.txt');
191 $tmp->addExclude('*/nested/*');
192 $x = $tmp->getFiles(__DIR__ . '/_data');
193 $this->assertEquals(1, count($x));
194 }
195
196 }
197
198}
199