Skip to content

Commit 41f6543

Browse files
harm-lessharm-less
authored andcommitted
Simple sample tests
1 parent 8f0477a commit 41f6543

13 files changed

+164
-25
lines changed

index.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
$sampleSimple = new \FQ\Samples\Simple();
1010
pr($sampleSimple->queryFile1FromChild1());
11+
pr($sampleSimple->queryFile1FromRoot1AndFromChild1());
12+
pr($sampleSimple->queryFile1InReverse());
13+
pr($sampleSimple->queryNonExistingFileWithRequirement());
1114

1215

1316
function pr($var) {

samples/FQ/Samples/SampleBootstrapper.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66

77
class SampleBootstrapper extends Files {
88

9-
public $root;
9+
public $_root;
1010

1111
function __construct($sampleDir) {
1212
parent::__construct();
13-
$this->root = __DIR__ . '/' . $sampleDir . '/';
13+
$this->_root = __DIR__ . '\\' . $sampleDir . '\\';
14+
}
15+
16+
public function root() {
17+
return $this->_root;
1418
}
1519

1620
}

samples/FQ/Samples/Simple.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,37 @@
55
use FQ\Dirs\ChildDir;
66
use FQ\Dirs\RootDir;
77
use FQ\Query\FilesQueryBuilder;
8+
use FQ\Query\FilesQueryRequirements;
89

910
class Simple extends SampleBootstrapper {
1011

1112
function __construct() {
1213
parent::__construct('simple');
1314

14-
$this->addRootDir(new RootDir('root1', $this->root . 'root1'));
15-
$this->addRootDir(new RootDir('root2', $this->root . 'root2'));
15+
$this->addRootDir(new RootDir('root1', $this->root() . 'root1'));
16+
$this->addRootDir(new RootDir('root2', $this->root() . 'root2'));
1617

1718
$this->addChildDir(new ChildDir('child1', 'child1'));
1819
$this->addChildDir(new ChildDir('child2', 'child2'));
1920
}
2021

2122
public function queryFile1FromChild1() {
22-
23-
//pr($this->queryPath('File2'));
24-
//var_dump($this->queryPath('does-not'));
25-
26-
2723
$builder = new FilesQueryBuilder($this);
28-
$builder->excludeRootDirs('root1')->includeChildDirs('child1')->fileName('File1');
24+
return $builder->includeChildDirs('child1')->run('File1')->listPaths();
25+
}
2926

30-
$query = $builder->run();
27+
public function queryFile1FromRoot1AndFromChild1() {
28+
$builder = new FilesQueryBuilder($this);
29+
return $builder->includeRootDirs('root1')->includeChildDirs('child1')->run('File1')->listPaths();
30+
}
31+
public function queryFile1InReverse() {
32+
$builder = new FilesQueryBuilder($this);
33+
return $builder->reverse(true)->run('File1')->listPaths();
34+
}
3135

32-
return $query->listPaths();
36+
public function queryNonExistingFileWithRequirement() {
37+
$builder = new FilesQueryBuilder($this);
38+
return $builder->addRequirement(FilesQueryRequirements::LEVELS_ONE)->run('does-not-exist')->listPaths();
3339
}
3440

3541
}

src/FQ/Files.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public function getFullPath($rootDir, $childDir) {
231231
* @param bool $reverseLoad
232232
* @return false|string
233233
*/
234-
public function queryPath($fileName, ChildSelection $childSelection = null, RootSelection $rootSelection = null, $reverseLoad = true) {
234+
public function queryPath($fileName, ChildSelection $childSelection = null, RootSelection $rootSelection = null, $reverseLoad = false) {
235235
$query = $this->query($rootSelection, $childSelection, true, true);
236236
$query->reverse($reverseLoad);
237237
$query->requirements(FilesQueryRequirements::LEVELS_ONE);
@@ -249,7 +249,7 @@ public function queryPath($fileName, ChildSelection $childSelection = null, Root
249249
* @param bool $reverseLoad
250250
* @return bool
251251
*/
252-
public function loadFile($fileName, ChildSelection $childSelection = null, RootSelection $rootSelection = null, $reverseLoad = true) {
252+
public function loadFile($fileName, ChildSelection $childSelection = null, RootSelection $rootSelection = null, $reverseLoad = false) {
253253
$path = $this->queryPath($fileName, $childSelection, $rootSelection, $reverseLoad);
254254
if ($path) {
255255
return $this->requireOnce($path);
@@ -265,7 +265,7 @@ public function loadFile($fileName, ChildSelection $childSelection = null, RootS
265265
* @param bool $reverseLoad
266266
* @return bool
267267
*/
268-
public function loadFiles($fileName, RootSelection $rootDirs = null, ChildSelection $children = null, $requiredLevels = FilesQueryRequirements::LEVELS_ONE, $reverseLoad = true)
268+
public function loadFiles($fileName, RootSelection $rootDirs = null, ChildSelection $children = null, $requiredLevels = FilesQueryRequirements::LEVELS_ONE, $reverseLoad = false)
269269
{
270270
$query = $this->query($rootDirs, $children, true, true);
271271
$query->reverse($reverseLoad);

src/FQ/query/FilesQuery.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ public function listPaths() {
280280
$paths = array_merge_recursive($paths, $childQuery->filteredAbsolutePaths());
281281
}
282282
}
283+
if ($this->isReversed()) {
284+
$paths = $this->reversePaths($paths);
285+
}
283286
return $paths;
284287
}
285288

@@ -295,9 +298,16 @@ public function listBasePaths() {
295298
foreach($this->queryChildDirs() as $childQuery) {
296299
$paths = array_merge($paths, $childQuery->filteredBasePaths());
297300
}
301+
if ($this->isReversed()) {
302+
$paths = $this->reversePaths($paths);
303+
}
298304
return $paths;
299305
}
300306

307+
public function reversePaths($paths) {
308+
return array_reverse($paths);
309+
}
310+
301311
/**
302312
* @return bool
303313
*/

src/FQ/query/FilesQueryBuilder.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class FilesQueryBuilder {
1818
private $_requirements;
1919
private $_filters;
2020
private $_reverse;
21+
private $_showErrors;
2122
private $_reset;
2223

2324
function __construct(Files $files) {
@@ -48,6 +49,9 @@ protected function _isReversed() {
4849
protected function _getFilters() {
4950
return $this->_filters;
5051
}
52+
protected function _showErrors() {
53+
return $this->_showErrors === null ? true : $this->_showErrors;
54+
}
5155

5256
public function includeRootDirs($rootDirs) {
5357
if (is_array($rootDirs)) {
@@ -152,6 +156,11 @@ public function filters($filters = null) {
152156
return $this;
153157
}
154158

159+
public function showErrors($showErrors) {
160+
$this->_showErrors = $showErrors;
161+
return $this;
162+
}
163+
155164
public function run($fileName = null) {
156165
$query = $this->_files()->query($this->rootSelection(), $this->childSelection(), $this->_reset);
157166
$requirements = $query->requirements();
@@ -168,8 +177,10 @@ public function run($fileName = null) {
168177
if (!is_string($fileNameToUse)) {
169178
throw new FileQueryBuilderException('No filename has been set. Use filename() to use a filename for the query or supply it this this run() method');
170179
}
171-
$query->run($fileNameToUse);
172-
180+
$result = $query->run($fileNameToUse);
181+
if ($result !== true && $this->_showErrors()) {
182+
throw $query->queryError();
183+
}
173184
return $query;
174185
}
175186
}

src/FQ/query/FilesQueryRequirements.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function tryRequirement($id, FilesQueryChild $child) {
187187
*/
188188
protected function requirementAtLeastOne(FilesQueryChild $child) {
189189
if (!$child->totalExistingPaths() >= 1) {
190-
return new FileQueryRequirementsException(sprintf('At least 1 file must be available for file "%s" in child with an id of "%s". Please create the file in any of these locations: %s', $child->relativePath(), $child->childDir()->id(), implode($child->rawAbsolutePaths())));
190+
return new FileQueryRequirementsException(sprintf('At least 1 file must be available for file "%s" in child with an id of "%s". Please create the file in any of these locations: "%s"', $child->relativePath(), $child->childDir()->id(), implode('", "', $child->rawAbsolutePaths())));
191191
}
192192
return true;
193193
}

tests/FQ/Tests/FilesTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,20 @@ public function testGetFullPath() {
157157
$this->files()->getFullPath($rootDir, null);
158158
}
159159

160-
public function testQueryPath() {
160+
public function testQueryPathWithTwoRootDirs() {
161161
$files = $this->files();
162162
$this->_addRootDir();
163+
$this->_addRootDir(null, $this->_newActualRootDirSecond());
164+
$this->_addChildDir();
165+
$this->assertEquals(self::ROOT_DIR_SECOND_ABSOLUTE_PATH . '/child1/File1.php', $files->queryPath('File1'));
166+
$this->assertFalse($files->queryPath('does-not-exist'));
167+
}
168+
public function testQueryPathWithTwoRootDirsWhenReversed() {
169+
$files = $this->files();
170+
$this->_addRootDir();
171+
$this->_addRootDir(null, $this->_newActualRootDirSecond());
163172
$this->_addChildDir();
164-
$this->assertEquals(self::ROOT_DIR_DEFAULT_ABSOLUTE_PATH . '/child1/File2.php', $files->queryPath('File2'));
173+
$this->assertEquals(self::ROOT_DIR_DEFAULT_ABSOLUTE_PATH . '/child1/File1.php', $files->queryPath('File1', null, null, true));
165174
$this->assertFalse($files->queryPath('does-not-exist'));
166175
}
167176

tests/FQ/Tests/Query/FilesQueryBuilderTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ public function testReverse() {
117117
$this->assertFalse($this->callNonPublicMethod('_isReversed'));
118118
}
119119

120+
public function testShowErrors() {
121+
$this->setExpectedException('FQ\Exceptions\FileQueryRequirementsException');
122+
$builder = $this->builder();
123+
$builder->showErrors(true)->addRequirement(FilesQueryRequirements::LEVELS_ONE)->run('does-not-exist');
124+
}
125+
120126
public function testFilter() {
121127
$builder = $this->builder();
122128
$this->assertNull($this->callNonPublicMethod('_getFilters'));

tests/FQ/Tests/Query/FilesQueryTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,25 @@ public function testListBasePathsWhenQueryHasRan() {
203203
self::ROOT_DIR_DEFAULT_ID => self::ROOT_DIR_DEFAULT_BASE_PATH . '/child1/File2.php'
204204
), $query->listBasePaths());
205205
}
206+
public function testListBasePathsWithReversedSetToTrue() {
207+
$this->files()->addRootDir($this->_newActualRootDirSecond());
208+
$query = $this->query();
209+
$query->reverse(true);
210+
$this->runQuery('File1');
211+
$paths = $query->listBasePaths();
212+
$index = 0;
213+
foreach ($paths as $rootDirId => $path) {
214+
if ($index === 0) {
215+
$this->assertEquals(self::ROOT_DIR_DEFAULT_ID, $rootDirId);
216+
$this->assertEquals(self::ROOT_DIR_DEFAULT_BASE_PATH . '/child1/File1.php', $path);
217+
}
218+
else if ($index === 1) {
219+
$this->assertEquals(self::ROOT_DIR_SECOND_ID, $rootDirId);
220+
$this->assertEquals(self::ROOT_DIR_SECOND_BASE_PATH . '/child1/File1.php', $path);
221+
}
222+
$index++;
223+
}
224+
}
206225

207226
public function testHasPathsWhenQueryHasNotRan() {
208227
$this->setExpectedException('FQ\Exceptions\FileQueryException', 'You must first call the "run" method before you can retrieve query information');

0 commit comments

Comments
 (0)