Skip to content

Commit 9176b9a

Browse files
harm-lessharm-less
authored andcommitted
Created tests for the DirSelection classes
Created tests for the DirSelection classes
1 parent e80cea7 commit 9176b9a

File tree

4 files changed

+177
-8
lines changed

4 files changed

+177
-8
lines changed

src/FQ/query/FilesQueryBuilder.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ public function fileName($fileName) {
3939
protected function getFileName() {
4040
return $this->_fileName;
4141
}
42+
protected function _getRequirements() {
43+
return $this->_requirements;
44+
}
45+
protected function _isReversed() {
46+
return $this->_reverse === null ? false : $this->_reverse;
47+
}
48+
protected function _getFilters() {
49+
return $this->_filters;
50+
}
4251

4352
public function includeRootDirs($rootDirs) {
4453
if (is_array($rootDirs)) {
@@ -143,14 +152,23 @@ public function filters($filters = null) {
143152
return $this;
144153
}
145154

146-
public function run() {
155+
public function run($fileName = null) {
147156
$query = $this->_files()->query($this->rootSelection(), $this->childSelection(), $this->_reset);
148-
$fileName = $this->getFileName();
157+
$requirements = $query->requirements();
158+
$requirements->addRequirements($this->_getRequirements());
159+
$query->reverse($this->_isReversed());
160+
161+
$filters = $this->_getFilters();
162+
if ($filters !== null) {
163+
$query->filters($filters);
164+
}
165+
166+
$fileNameToUse = $fileName !== null ? $fileName : $this->getFileName();
149167

150-
if (!is_string($fileName)) {
151-
throw new FileQueryBuilderException('No filename has been set. Use filename() to use a filename for the query');
168+
if (!is_string($fileNameToUse)) {
169+
throw new FileQueryBuilderException('No filename has been set. Use filename() to use a filename for the query or supply it this this run() method');
152170
}
153-
$query->run($fileName);
171+
$query->run($fileNameToUse);
154172

155173
return $query;
156174
}

src/FQ/query/selection/DirSelection.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ public function validateQuerySelection($availableDirs) {
148148
}
149149
}
150150
else if ($this->hasExcludedDirsByDir()) {
151-
foreach ($this->getExcludedDirsByDir() as $includedDir) {
152-
$dirIds[] = $includedDir->id();
151+
foreach ($this->getExcludedDirsByDir() as $excludedDir) {
152+
$dirIds[] = $excludedDir->id();
153153
}
154154
}
155155

@@ -201,6 +201,8 @@ public function getSelection($availableDirs) {
201201
unset($selection[$includedDir->id()]);
202202
}
203203
}
204+
// reset keys
205+
$selection = array_values($selection);
204206
}
205207

206208
return $selection;

tests/FQ/Tests/AbstractFQTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected function nonPublicMethodObject($object = null) {
9090
return $this->_nonPublicMethodObject;
9191
}
9292

93-
protected function callNonPublicMethod($name, $args) {
93+
protected function callNonPublicMethod($name, $args = null) {
9494
return $this->callObjectWithNonPublicMethod($this->nonPublicMethodObject(), $name, $args);
9595
}
9696
protected function callObjectWithNonPublicMethod($obj, $name, $args) {
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
3+
namespace FQ\Tests\Query\Selection;
4+
5+
use FQ\Query\Selection\DirSelection;
6+
use FQ\Tests\Query\AbstractFilesQueryTests;
7+
8+
class DirSelectionTest extends AbstractFilesQueryTests {
9+
10+
/**
11+
* @var DirSelection
12+
*/
13+
protected $_dirSelection;
14+
15+
protected function setUp() {
16+
parent::setUp();
17+
18+
$this->_dirSelection = new DirSelection();
19+
$this->nonPublicMethodObject($this->dirSelection());
20+
}
21+
protected function dirSelection() {
22+
return $this->_dirSelection;
23+
}
24+
25+
public function testConstructor() {
26+
$dirSelection = new DirSelection();
27+
$this->assertNotNull($dirSelection);
28+
}
29+
30+
public function testReset() {
31+
$selection = $this->dirSelection();
32+
$selection->reset();
33+
34+
$this->assertEquals(array(), $selection->getIncludedDirsById());
35+
$this->assertEquals(array(), $selection->getIncludedDirsByDir());
36+
$this->assertEquals(array(), $selection->getExcludedDirsById());
37+
$this->assertEquals(array(), $selection->getExcludedDirsByDir());
38+
39+
$this->assertFalse($selection->hasIncludedDirsByDir());
40+
$this->assertFalse($selection->hasIncludedDirsById());
41+
$this->assertFalse($selection->hasIncludedDirs());
42+
}
43+
44+
public function testIncludeDirById() {
45+
$selection = $this->dirSelection();
46+
47+
$selection->includeDirById('dir1');
48+
$selection->includeDirById('dir2');
49+
50+
$this->assertEquals(array('dir1', 'dir2'), $selection->getIncludedDirsById());
51+
$this->assertFalse($selection->hasIncludedDirsByDir());
52+
$this->assertTrue($selection->hasIncludedDirsById());
53+
$this->assertTrue($selection->hasIncludedDirs());
54+
}
55+
56+
public function testExcludeDirById() {
57+
$selection = $this->dirSelection();
58+
59+
$selection->excludeDirById('dir1');
60+
$selection->excludeDirById('dir2');
61+
62+
$this->assertEquals(array('dir1', 'dir2'), $selection->getExcludedDirsById());
63+
$this->assertFalse($selection->hasExcludedDirsByDir());
64+
$this->assertTrue($selection->hasExcludedDirsById());
65+
$this->assertTrue($selection->hasExcludedDirs());
66+
}
67+
68+
public function testIncludeDirByDir() {
69+
$selection = $this->dirSelection();
70+
71+
$dir1 = $this->_newActualRootDir();
72+
$dir2 = $this->_newActualRootDirSecond();
73+
$selection->includeDir($dir1);
74+
$selection->includeDir($dir2);
75+
76+
$this->assertEquals(array($dir1, $dir2), $selection->getIncludedDirsByDir());
77+
$this->assertTrue($selection->hasIncludedDirsByDir());
78+
$this->assertFalse($selection->hasIncludedDirsById());
79+
$this->assertTrue($selection->hasIncludedDirs());
80+
}
81+
82+
public function testExcludeDirByDir() {
83+
$selection = $this->dirSelection();
84+
$dir1 = $this->_newActualRootDir();
85+
$dir2 = $this->_newActualRootDirSecond();
86+
$selection->excludeDir($dir1);
87+
$selection->excludeDir($dir2);
88+
89+
$this->assertEquals(array($dir1, $dir2), $selection->getExcludedDirsByDir());
90+
$this->assertTrue($selection->hasExcludedDirsByDir());
91+
$this->assertFalse($selection->hasExcludedDirsById());
92+
$this->assertTrue($selection->hasExcludedDirs());
93+
}
94+
95+
public function testIncludeAndExcludeADirAtTheSameTime() {
96+
$this->setExpectedException('FQ\Exceptions\FileQueryException', 'Cannot exclude a dir when you\'ve already defined included directories');
97+
$selection = $this->dirSelection();
98+
$dir1 = $this->_newActualRootDir();
99+
$dir2 = $this->_newActualRootDirSecond();
100+
$selection->includeDir($dir1);
101+
$selection->excludeDir($dir2);
102+
}
103+
public function testExcludeAndIncludeADirAtTheSameTime() {
104+
$this->setExpectedException('FQ\Exceptions\FileQueryException', 'Cannot include a dir when you\'ve already defined excluded directories');
105+
$selection = $this->dirSelection();
106+
$dir1 = $this->_newActualRootDir();
107+
$dir2 = $this->_newActualRootDirSecond();
108+
$selection->excludeDir($dir1);
109+
$selection->includeDir($dir2);
110+
}
111+
112+
public function testValidateQuerySelectionWhenDirIsNotAvailable() {
113+
$this->setExpectedException('FQ\Exceptions\FileQueryException', 'Query selection validation failed because one ore more selection IDs (root1) could not be found in the available directories. Available directory ids are "root2"');
114+
$selection = $this->dirSelection();
115+
$dir1 = $this->_newActualRootDir();
116+
$dir2 = $this->_newActualRootDirSecond();
117+
$selection->includeDir($dir1);
118+
$selection->validateQuerySelection(array($dir2));
119+
}
120+
public function testValidateQuerySelectionWithExcludingWhenDirIsNotAvailable() {
121+
$this->setExpectedException('FQ\Exceptions\FileQueryException', 'Query selection validation failed because one ore more selection IDs (root1) could not be found in the available directories. Available directory ids are "root2"');
122+
$selection = $this->dirSelection();
123+
$dir1 = $this->_newActualRootDir();
124+
$dir2 = $this->_newActualRootDirSecond();
125+
$selection->excludeDir($dir1);
126+
$selection->validateQuerySelection(array($dir2));
127+
}
128+
129+
public function testGetSelectionWithoutSelection() {
130+
$selection = $this->dirSelection();
131+
$dir1 = $this->_newActualRootDir();
132+
$dir2 = $this->_newActualRootDirSecond();
133+
$this->assertEquals(array($dir1, $dir2), $selection->getSelection(array($dir1, $dir2)));
134+
}
135+
public function testGetSelectionByInclusion() {
136+
$selection = $this->dirSelection();
137+
$dir1 = $this->_newActualRootDir();
138+
$dir2 = $this->_newActualRootDirSecond();
139+
$selection->includeDir($dir1);
140+
$this->assertEquals(array($dir1), $selection->getSelection(array($dir1, $dir2)));
141+
}
142+
public function testGetSelectionByExclusion() {
143+
$selection = $this->dirSelection();
144+
$dir1 = $this->_newActualRootDir();
145+
$dir2 = $this->_newActualRootDirSecond();
146+
$selection->excludeDir($dir1);
147+
$this->assertEquals(array($dir2), $selection->getSelection(array($dir1, $dir2)));
148+
}
149+
}

0 commit comments

Comments
 (0)