diff --git a/samples/FQ/Samples/Simple.php b/samples/FQ/Samples/Simple.php index efbacb8..735e6ba 100644 --- a/samples/FQ/Samples/Simple.php +++ b/samples/FQ/Samples/Simple.php @@ -20,30 +20,30 @@ function __construct() { } public function queryFile1FromChild1() { - $builder = new FilesQueryBuilder($this); + $builder = new FilesQueryBuilder($this->query()); return $builder->includeChildDirs('child1')->run('File1')->listPaths(); } public function queryFile1FromRoot1AndFromChild1() { - $builder = new FilesQueryBuilder($this); + $builder = new FilesQueryBuilder($this->query()); return $builder->includeRootDirs('root1')->includeChildDirs('child1')->run('File1')->listPaths(); } public function queryFile1InReverse() { - $builder = new FilesQueryBuilder($this); + $builder = new FilesQueryBuilder($this->query()); return $builder->reverse(true)->run('File1')->listPaths(); } public function queryNonExistingFileWithRequirementOne() { - $builder = new FilesQueryBuilder($this); + $builder = new FilesQueryBuilder($this->query()); return $builder->addRequirement(FilesQueryRequirements::REQUIRE_ONE)->run('File1')->listPaths(); } public function queryNonExistingFileWithRequirementLast() { - $builder = new FilesQueryBuilder($this); + $builder = new FilesQueryBuilder($this->query()); return $builder->addRequirement(FilesQueryRequirements::REQUIRE_LAST)->run('File1')->listPaths(); } public function queryNonExistingFileWithRequirementAll() { - $builder = new FilesQueryBuilder($this); + $builder = new FilesQueryBuilder($this->query()); return $builder->addRequirement(FilesQueryRequirements::REQUIRE_ALL)->run('File1')->listPaths(); } } \ No newline at end of file diff --git a/src/FQ/Files.php b/src/FQ/Files.php index fc814de..2d85a3b 100644 --- a/src/FQ/Files.php +++ b/src/FQ/Files.php @@ -2,10 +2,10 @@ namespace FQ; -use FQ\Collections\ChildDirCollection; use FQ\Collections\RootDirCollection; -use FQ\Dirs\ChildDir; +use FQ\Collections\ChildDirCollection; use FQ\Dirs\RootDir; +use FQ\Dirs\ChildDir; use FQ\Exceptions\FilesException; use FQ\Query\FilesQuery; use FQ\Query\FilesQueryRequirements; @@ -274,6 +274,19 @@ public function getFullPath($rootDir, $childDir) { return $childObj->fullAbsolutePath($rootDirObj); } + public function prepareQuery(RootSelection $rootSelection = null, ChildSelection $childSelection = null, $reset = true, $resetSelection = true) { + $query = $this->query(); + if ($reset) $query->reset(); + if ($resetSelection) $query->resetSelection(); + if ($rootSelection !== null) { + $query->setRootDirSelection($rootSelection); + } + if ($childSelection !== null) { + $query->setChildDirSelection($childSelection); + } + return $query; + } + /** * @param string $fileName * @param ChildSelection $childSelection @@ -282,7 +295,7 @@ public function getFullPath($rootDir, $childDir) { * @return false|string */ public function queryPath($fileName, ChildSelection $childSelection = null, RootSelection $rootSelection = null, $reverseLoad = false) { - $query = $this->query($rootSelection, $childSelection, true, true); + $query = $this->prepareQuery($rootSelection, $childSelection); $query->reverse($reverseLoad); $query->requirements(FilesQueryRequirements::REQUIRE_ONE); if ($query->run($fileName)) { @@ -309,15 +322,15 @@ public function loadFile($fileName, ChildSelection $childSelection = null, RootS /** * @param string $fileName - * @param RootSelection $rootDirs - * @param ChildSelection $children + * @param RootSelection $rootSelection + * @param ChildSelection $childSelection * @param string $requiredLevels * @param bool $reverseLoad * @return bool */ - public function loadFiles($fileName, RootSelection $rootDirs = null, ChildSelection $children = null, $requiredLevels = FilesQueryRequirements::REQUIRE_ONE, $reverseLoad = false) + public function loadFiles($fileName, RootSelection $rootSelection = null, ChildSelection $childSelection = null, $requiredLevels = FilesQueryRequirements::REQUIRE_ONE, $reverseLoad = false) { - $query = $this->query($rootDirs, $children, true, true); + $query = $this->prepareQuery($rootSelection, $childSelection); $query->reverse($reverseLoad); $query->requirements($requiredLevels); if ($query->run($fileName)) { @@ -334,46 +347,33 @@ public function loadFiles($fileName, RootSelection $rootDirs = null, ChildSelect /** * @param string $fileName - * @param RootSelection $rootDirs - * @param ChildSelection $children + * @param RootSelection $rootSelection + * @param ChildSelection $childSelection * @return array */ - public function queryPaths($fileName, RootSelection $rootDirs = null, ChildSelection $children = null) { - $query = $this->query($rootDirs, $children, true, true); + public function queryPaths($fileName, RootSelection $rootSelection = null, ChildSelection $childSelection = null) { + $query = $this->prepareQuery($rootSelection, $childSelection); $query->run($fileName); return $query->listPaths(); } /** * @param string $fileName - * @param RootSelection $rootDirs - * @param ChildSelection $children + * @param RootSelection $rootSelection + * @param ChildSelection $childSelection * @return bool */ - public function fileExists($fileName, RootSelection $rootDirs = null, ChildSelection $children = null) { - $query = $this->query($rootDirs, $children, true, true); + public function fileExists($fileName, RootSelection $rootSelection = null, ChildSelection $childSelection = null) { + $query = $this->prepareQuery($rootSelection, $childSelection); $query->run($fileName); return $query->hasPaths(); } /** - * @param RootSelection $rootDirs - * @param ChildSelection $children - * @param bool $resetQuery - * @param bool $resetSelection * @return FilesQuery */ - public function query(RootSelection $rootDirs = null, ChildSelection $children = null, $resetQuery = true, $resetSelection = false) { - $query = $this->_query; - if ($resetQuery) { - $query->reset(); - } - if ($resetSelection) { - $query->resetSelection(); - } - if ($rootDirs) $query->setRootDirSelection($rootDirs); - if ($children) $query->setChildDirSelection($children); - return $query; + public function query() { + return $this->_query; } /** diff --git a/src/FQ/query/FilesQuery.php b/src/FQ/query/FilesQuery.php index 0bafe37..a440a68 100644 --- a/src/FQ/query/FilesQuery.php +++ b/src/FQ/query/FilesQuery.php @@ -2,13 +2,19 @@ namespace FQ\Query; -use FQ\Dirs\ChildDir; use FQ\Dirs\RootDir; +use FQ\Dirs\ChildDir; use FQ\Exceptions\FileQueryException; use FQ\Files; use FQ\Query\Selection\ChildSelection; use FQ\Query\Selection\RootSelection; +/** + * Class FilesQuery + * @package FQ\Query + * + * @todo Ability to lock selections if you have a reoccurring queries + */ class FilesQuery { /** @@ -40,6 +46,10 @@ class FilesQuery { * @var RootDir[] */ private $_cachedQueryRootDirs; + /** + * @var ChildDir[] + */ + private $_cachedQueryChildDirs; /** * @var array @@ -98,7 +108,6 @@ class FilesQuery { * @param Files $files */ function __construct(Files $files) { - $this->_requirements = new FilesQueryRequirements(); $this->_files = $files; $this->reset(); @@ -130,6 +139,9 @@ public function resetSelection() { * @return FilesQueryRequirements */ public function requirements() { + if ($this->_requirements === null) { + $this->_requirements = new FilesQueryRequirements($this); + } return $this->_requirements; } @@ -167,21 +179,29 @@ public function queryHasFilter($filter) { return in_array($filter, $this->filters()); } - public function setRootDirSelection(RootSelection $selection) { - $this->_rootDirSelection = $selection; + /** + * @param RootSelection $rootDirSelection + */ + public function setRootDirSelection(RootSelection $rootDirSelection) { + $this->_cachedQueryRootDirs = null; + $this->_rootDirSelection = $rootDirSelection; } - public function getRootDirSelection($createNewSelection = false) { - if ($this->_rootDirSelection === null || $createNewSelection) { + public function getRootDirSelection() { + if ($this->_rootDirSelection === null) { $this->_rootDirSelection = new RootSelection(); } return $this->_rootDirSelection; } - public function setChildDirSelection(ChildSelection $selection) { - $this->_childDirSelection = $selection; + /** + * @param ChildSelection $childDirSelection + */ + public function setChildDirSelection(ChildSelection $childDirSelection) { + $this->_cachedQueryChildren = null; + $this->_childDirSelection = $childDirSelection; } - public function getChildDirSelection($createNewSelection = false) { - if ($this->_childDirSelection === null || $createNewSelection) { + public function getChildDirSelection() { + if ($this->_childDirSelection === null) { $this->_childDirSelection = new ChildSelection(); } return $this->_childDirSelection; @@ -236,7 +256,7 @@ protected function _hasRunCheck() { /** * @param string $fileName The name of the file the query will be executing - * @return null|string[] + * @return bool */ public function run($fileName) { if ($this->files()->totalRootDirs() === 0) { @@ -244,20 +264,35 @@ public function run($fileName) { } $this->_queriedFileName = $fileName; + + if ($this->getRootDirSelection()->isInvalidated()) { + $this->_cachedQueryRootDirs; + } + if ($this->getChildDirSelection()->isInvalidated()) { + $this->_cachedQueryChildren; + } $rootDirsSelection = $this->_getCachedRootDirSelection(); $this->_currentQueryChildren = array(); - foreach ($this->getCurrentChildDirSelection() as $childDir) { - $this->_currentQueryChildren[$childDir->id()] = $this->_processQueryChild($childDir, $rootDirsSelection);; + foreach ($this->_getCachedChildDirSelection() as $childDir) { + $this->_currentQueryChildren[$childDir->id()] = $this->_prepareQueryChild($childDir, $rootDirsSelection); } $this->_hasRun = true; - $meetsRequirements = $this->requirements()->meetsRequirements($this, false); + $meetsRequirements = $this->requirements()->meetsRequirements(false); if ($meetsRequirements !== true) { $this->_queryError = $meetsRequirements; } return $this->_queryError === null; } + protected function _prepareQueryChild(ChildDir $childDir, $rootDirs) { + $queryChild = $this->_getQueryChild($childDir); + $queryChild->reset(); + $queryChild->setRootDirs($rootDirs); + + return $queryChild; + } + public function load() { $this->_hasRunCheck(); @@ -279,6 +314,12 @@ public function load() { public function getCurrentRootDirSelection() { return $this->getRootDirSelection()->getSelection($this->files()->rootDirs()); } + protected function _getCachedRootDirSelection() { + if ($this->_cachedQueryRootDirs === null) { + $this->_cachedQueryRootDirs = $this->getCurrentRootDirSelection(); + } + return $this->_cachedQueryRootDirs; + } /** * @return ChildDir[] @@ -286,19 +327,11 @@ public function getCurrentRootDirSelection() { public function getCurrentChildDirSelection() { return $this->getChildDirSelection()->getSelection($this->files()->childDirs()); } - - protected function _getCachedRootDirSelection() { - if ($this->_cachedQueryRootDirs === null) { - $this->_cachedQueryRootDirs = $this->getCurrentRootDirSelection(); + protected function _getCachedChildDirSelection() { + if ($this->_cachedQueryChildDirs === null) { + $this->_cachedQueryChildDirs = $this->getCurrentChildDirSelection(); } - return $this->_cachedQueryRootDirs; - } - - protected function _processQueryChild(ChildDir $childDir, $rootSelection) { - $queryChild = $this->_getQueryChild($childDir); - $queryChild->reset(); - $queryChild->setRootDirs($rootSelection); - return $queryChild; + return $this->_cachedQueryChildDirs; } /** @@ -418,6 +451,10 @@ public function listBasePaths() { return $paths; } + /** + * @param string[] $paths + * @return string[] + */ public function reversePaths($paths) { return array_reverse($paths); } diff --git a/src/FQ/query/FilesQueryBuilder.php b/src/FQ/query/FilesQueryBuilder.php index 8c825e2..99e6f65 100644 --- a/src/FQ/query/FilesQueryBuilder.php +++ b/src/FQ/query/FilesQueryBuilder.php @@ -3,55 +3,62 @@ namespace FQ\Query; use FQ\Exceptions\FileQueryBuilderException; -use FQ\Files; -use FQ\Query\Selection\ChildSelection; use FQ\Query\Selection\DirSelection; use FQ\Query\Selection\RootSelection; +use FQ\Query\Selection\ChildSelection; class FilesQueryBuilder { - private $_files; + private $_query; private $_fileName; + /** + * @var RootSelection + */ private $_rootDirSelection; + /** + * @var ChildSelection + */ private $_childDirSelection; private $_requirements; private $_filters; private $_reverse; private $_showErrors; - private $_reset; + private $_resetQuery; - function __construct(Files $files) { - $this->_childDirs = array(); - $this->_requirements = array(); + private $_isPrepared; - $this->_files = $files; + function __construct(FilesQuery $query) { + $this->_query = $query; + + $this->reset(); } - protected function _files() { - return $this->_files; + protected function _query() { + return $this->_query; } public function fileName($fileName) { $this->_fileName = $fileName; return $this; } - protected function getFileName() { + public function getFileName() { return $this->_fileName; } - protected function _reset() { - return $this->_reset === null ? true : $this->_reset; + + public function resetsQuery() { + return $this->_resetQuery === null ? true : $this->_resetQuery; } - protected function _getRequirements() { + public function getRequirements() { return $this->_requirements; } - protected function _isReversed() { + public function isReversed() { return $this->_reverse === null ? false : $this->_reverse; } - protected function _getFilters() { + public function getFilters() { return $this->_filters; } - protected function _showErrors() { + public function showsErrors() { return $this->_showErrors === null ? true : $this->_showErrors; } @@ -89,12 +96,22 @@ public function excludeRootDirs($rootDirs) { return $this; } + + /** + * @return RootSelection + */ public function rootSelection() { if ($this->_rootDirSelection === null) { $this->_rootDirSelection = new RootSelection(); } return $this->_rootDirSelection; } + /** + * @return bool + */ + protected function _hasActiveRootSelection() { + return $this->_rootDirSelection !== null; + } public function includeChildDirs($childDirs) { if ($childDirs === null) { @@ -130,12 +147,22 @@ public function excludeChildDirs($childDirs) { return $this; } + + /** + * @return ChildSelection + */ public function childSelection() { if ($this->_childDirSelection === null) { $this->_childDirSelection = new ChildSelection(); } return $this->_childDirSelection; } + /** + * @return bool + */ + protected function _hasActiveChildSelection() { + return $this->_childDirSelection !== null; + } protected function _addToDirSelection($type, DirSelection $selectionObj, $dir) { switch ($type) { @@ -173,7 +200,7 @@ public function reverse($reverse) { return $this; } - public function filters($filters = null) { + public function filters($filters) { $this->_filters = $filters; return $this; } @@ -183,16 +210,25 @@ public function showErrors($showErrors) { return $this; } + public function reset() { + if ($this->_childDirSelection !== null) { + $this->_childDirSelection->reset(); + } + if ($this->_rootDirSelection !== null) { + $this->_rootDirSelection->reset(); + } + $this->_filters = null; + $this->_fileName = null; + $this->_isPrepared = null; + $this->_reverse = null; + $this->_showErrors = null; + $this->_requirements = array(); + return $this; + } + public function run($fileName = null) { - $query = $this->_files()->query($this->rootSelection(), $this->childSelection(), $this->_reset(), $this->_reset()); - $requirements = $query->requirements(); - $requirements->addRequirements($this->_getRequirements()); - $query->reverse($this->_isReversed()); - $filters = $this->_getFilters(); - if ($filters !== null) { - $query->filters($filters); - } + $query = $this->prepare(); $fileNameToUse = $fileName !== null ? $fileName : $this->getFileName(); if (!is_string($fileNameToUse)) { @@ -200,9 +236,41 @@ public function run($fileName = null) { } $result = $query->run($fileNameToUse); - if ($result !== true && $this->_showErrors()) { + if ($result !== true && $this->showsErrors()) { throw $query->queryError(); } return $query; } + + public function prepare() { + $query = $this->_query(); + + // reset the query if that is necessary + if($this->resetsQuery()) { + $query->reset(); + } + + // add the requirements to the query + $requirements = $query->requirements(); + $requirements->addRequirements($this->getRequirements()); + + // reverse the query if necessary + $query->reverse($this->isReversed()); + + // apply the set filters to the query + $filters = $this->getFilters(); + if ($filters !== null) { + $query->filters($filters); + } + + if ($this->_hasActiveRootSelection() !== null) { + $query->setRootDirSelection($this->rootSelection()); + } + if ($this->_hasActiveChildSelection() !== null) { + $query->setChildDirSelection($this->childSelection()); + } + + $this->_isPrepared = true; + return $query; + } } \ No newline at end of file diff --git a/src/FQ/query/FilesQueryChild.php b/src/FQ/query/FilesQueryChild.php index c14c65d..470b7a9 100644 --- a/src/FQ/query/FilesQueryChild.php +++ b/src/FQ/query/FilesQueryChild.php @@ -2,11 +2,10 @@ namespace FQ\Query; -use FQ\Dirs\ChildDir; use FQ\Dirs\RootDir; +use FQ\Dirs\ChildDir; use FQ\Exceptions\FileQueryException; use FQ\Files; -use FQ\Query\Selection\RootSelection; class FilesQueryChild { @@ -69,8 +68,6 @@ function __construct(FilesQuery $filesQuery, ChildDir $childDir) { /** * Resets the FilesQueryChild so it can be reused - * - * @todo Add a soft reset so the raw paths don't have to be reset each time a query request has been done */ public function reset() { $this->_rawRelativePath = null; diff --git a/src/FQ/query/FilesQueryRequirements.php b/src/FQ/query/FilesQueryRequirements.php index 4c7f524..1a6a8c5 100644 --- a/src/FQ/query/FilesQueryRequirements.php +++ b/src/FQ/query/FilesQueryRequirements.php @@ -6,6 +6,7 @@ class FilesQueryRequirements { + private $_query; /** * @var string[] Requirements of the query */ @@ -23,7 +24,8 @@ class FilesQueryRequirements { const REQUIRE_LAST = 'require_last'; const REQUIRE_ALL = 'require_all'; - function __construct() { + function __construct(FilesQuery $query) { + $this->_query = $query; $this->_requirements = array(); $this->registerRequirement(self::REQUIRE_ONE, array($this, 'requirementAtLeastOne')); @@ -246,21 +248,20 @@ protected function requirementAll(FilesQuery $query) { /** * Checks if the query meets all its requirements * - * @param FilesQuery $query * @param bool $throwExceptionOnFail * @throws \Exception When $throwExceptionOnFail is set to true and one of the requirements fails, it will throw * the exception from that fail. Otherwise this exception will be returned * @return mixed Returns true if all requirements are met. Otherwise returns an un-thrown exception * if 'throwExceptionOnFail' is set to false or the response from the requirement */ - public function meetsRequirements(FilesQuery $query, $throwExceptionOnFail = true) { + public function meetsRequirements($throwExceptionOnFail = true) { // if there are no requirements it certainly is valid and it can be returned immediately if (!$this->hasRequirements()) { return true; } foreach ($this->requirements() as $requirement) { - $attempt = $this->tryRequirement($requirement, $query); + $attempt = $this->tryRequirement($requirement, $this->_query); if ($attempt instanceof \Exception && $throwExceptionOnFail === true) { throw $attempt; } diff --git a/src/FQ/query/selection/ChildSelection.php b/src/FQ/query/selection/ChildSelection.php index 84d72c5..5fec263 100644 --- a/src/FQ/query/selection/ChildSelection.php +++ b/src/FQ/query/selection/ChildSelection.php @@ -7,4 +7,18 @@ class ChildSelection extends DirSelection { function __construct() { parent::__construct(); } + + /** + * @return ChildSelection + */ + public function copy() { + return parent::copy(); + } + + /** + * @return ChildSelection + */ + protected function _createInstance() { + return new ChildSelection(); + } } \ No newline at end of file diff --git a/src/FQ/query/selection/DirSelection.php b/src/FQ/query/selection/DirSelection.php index 66b6c03..bd51e11 100644 --- a/src/FQ/query/selection/DirSelection.php +++ b/src/FQ/query/selection/DirSelection.php @@ -4,6 +4,7 @@ use FQ\Dirs\Dir; use FQ\Exceptions\FileQueryException; +use FQ\Files; class DirSelection { @@ -13,6 +14,10 @@ class DirSelection { private $_dirsIncludedId; private $_dirsExcludedId; + private $_invalidated; + + private $_isLocked; + const FILTER_INCLUDE = 'include'; const FILTER_EXCLUDE = 'exclude'; @@ -23,15 +28,52 @@ function __construct() { /** * Include a directory to the selection by its ID * @param string $id ID of the directory + * @return bool */ public function includeDirById($id) { + if ($this->isLocked()) { + return false; + } + $this->invalidate(); $this->_addSelectionDir(self::FILTER_INCLUDE); $this->_dirsIncludedId[] = $id; + return true; + } + /** + * @param string $id + * @return bool|null + */ + public function removeIncludedDirById($id) { + if ($this->isLocked()) { + return null; + } + if (($index = array_search($id, $this->_dirsIncludedId)) !== false) { + $this->invalidate(); + array_splice($this->_dirsIncludedId, $index, 1); + return true; + } + return false; + } + /** + * @return bool + */ + public function removeAllIncludedDirsById() { + if ($this->isLocked()) { + return false; + } + $this->invalidate(); + $this->_dirsIncludedId = array(); + return true; } + /** + * @return string[] + */ public function getIncludedDirsById() { return $this->_dirsIncludedId; } - + /** + * @return bool + */ public function hasIncludedDirsById() { $includedDirs = $this->getIncludedDirsById(); return count($includedDirs) > 0; @@ -40,15 +82,52 @@ public function hasIncludedDirsById() { /** * Exclude a directory from the selection by its ID * @param string $id ID of the directory + * @return bool */ public function excludeDirById($id) { + if ($this->isLocked()) { + return false; + } + $this->invalidate(); $this->_addSelectionDir(self::FILTER_EXCLUDE); $this->_dirsExcludedId[] = $id; + return true; } + /** + * @param string $id + * @return bool + */ + public function removeExcludedDirById($id) { + if ($this->isLocked()) { + return null; + } + if (($index = array_search($id, $this->_dirsExcludedId)) !== false) { + $this->invalidate(); + array_splice($this->_dirsExcludedId, $index, 1); + return true; + } + return false; + } + /** + * @return bool + */ + public function removeAllExcludedDirsById() { + if ($this->isLocked()) { + return false; + } + $this->invalidate(); + $this->_dirsExcludedId = array(); + return true; + } + /** + * @return string[] + */ public function getExcludedDirsById() { return $this->_dirsExcludedId; } - + /** + * @return bool + */ public function hasExcludedDirsById() { $excludedDirs = $this->getExcludedDirsById(); return count($excludedDirs) > 0; @@ -57,10 +136,42 @@ public function hasExcludedDirsById() { /** * Include a directory to the selected * @param Dir $dir Directory instance + * @return bool */ public function includeDir(Dir $dir) { + if ($this->isLocked()) { + return false; + } + $this->invalidate(); $this->_addSelectionDir(self::FILTER_INCLUDE); $this->_dirsIncluded[] = $dir; + return true; + } + /** + * @param Dir $dir + * @return bool|null + */ + public function removeIncludedDir(Dir $dir) { + if ($this->isLocked()) { + return null; + } + if (($index = array_search($dir, $this->_dirsIncluded)) !== false) { + $this->invalidate(); + array_splice($this->_dirsIncluded, $index, 1); + return true; + } + return false; + } + /** + * @return bool + */ + public function removeAllIncludedDirs() { + if ($this->isLocked()) { + return false; + } + $this->invalidate(); + $this->_dirsIncluded = array(); + return true; } /** * @return Dir[] @@ -68,18 +179,53 @@ public function includeDir(Dir $dir) { public function getIncludedDirsByDir() { return $this->_dirsIncluded; } - + /** + * @return bool + */ public function hasIncludedDirsByDir() { $includedDirs = $this->getIncludedDirsByDir(); return count($includedDirs) > 0; } + /** * Exclude a directory from the selection * @param Dir $dir Directory instance + * @return bool */ public function excludeDir(Dir $dir) { + if ($this->isLocked()) { + return false; + } + $this->invalidate(); $this->_addSelectionDir(self::FILTER_EXCLUDE); $this->_dirsExcluded[] = $dir; + return true; + } + /** + * @param Dir $dir + * @return bool + */ + public function removeExcludedDir(Dir $dir) { + if ($this->isLocked()) { + return null; + } + if (($index = array_search($dir, $this->_dirsExcluded)) !== false) { + $this->invalidate(); + array_splice($this->_dirsExcluded, $index, 1); + return true; + } + return false; + } + /** + * @return bool + */ + public function removeAllExcludedDirs() { + if ($this->isLocked()) { + return false; + } + $this->invalidate(); + $this->_dirsExcluded = array(); + return true; } /** * @return Dir[] @@ -87,7 +233,9 @@ public function excludeDir(Dir $dir) { public function getExcludedDirsByDir() { return $this->_dirsExcluded; } - + /** + * @return bool + */ public function hasExcludedDirsByDir() { $excludedDirs = $this->getExcludedDirsByDir(); return count($excludedDirs) > 0; @@ -169,6 +317,8 @@ public function getSelection($availableDirs) { $this->validateQuerySelection($availableDirs); + $this->_invalidated = false; + if ($this->hasIncludedDirs() || $this->hasExcludedDirs()) { $availableDirsTemp = array(); @@ -219,4 +369,38 @@ public function reset() { $this->_dirsIncludedId = array(); $this->_dirsExcludedId = array(); } + + public function unsafeImport($includedIds, $includedDirs, $excludedIds, $excludedDirs) { + $this->_dirsIncluded = $includedDirs === null ? array() : $includedDirs; + $this->_dirsExcluded = $excludedDirs === null ? array() : $excludedDirs; + + $this->_dirsIncludedId = $includedIds === null ? array() : $includedIds; + $this->_dirsExcludedId = $excludedIds === null ? array() : $excludedIds; + } + + public function invalidate() { + $this->_invalidated = true; + } + public function isInvalidated() { + return $this->_invalidated; + } + + public function lock() { + $this->_isLocked = true; + } + public function unlock() { + $this->_isLocked = false; + } + public function isLocked() { + return $this->_isLocked === true; + } + + public function copy() { + $clone = $this->_createInstance(); + $clone->unsafeImport($this->_dirsIncludedId, $this->_dirsIncluded, $this->_dirsExcludedId, $this->_dirsExcluded); + return $clone; + } + protected function _createInstance() { + return new DirSelection(); + } } \ No newline at end of file diff --git a/src/FQ/query/selection/RootSelection.php b/src/FQ/query/selection/RootSelection.php index 8038a8b..bb6e40f 100644 --- a/src/FQ/query/selection/RootSelection.php +++ b/src/FQ/query/selection/RootSelection.php @@ -7,4 +7,18 @@ class RootSelection extends DirSelection { function __construct() { parent::__construct(); } + + /** + * @return RootSelection + */ + public function copy() { + return parent::copy(); + } + + /** + * @return RootSelection + */ + protected function _createInstance() { + return new RootSelection(); + } } \ No newline at end of file diff --git a/tests/FQ/Tests/FilesTest.php b/tests/FQ/Tests/FilesTest.php index 5247e87..812f3f4 100644 --- a/tests/FQ/Tests/FilesTest.php +++ b/tests/FQ/Tests/FilesTest.php @@ -6,6 +6,8 @@ use FQ\Dirs\RootDir; use FQ\Files; use FQ\Query\FilesQuery; +use FQ\Query\Selection\ChildSelection; +use FQ\Query\Selection\RootSelection; class FilesTest extends AbstractFQTest { @@ -201,6 +203,22 @@ public function testGetFullPath() { $this->files()->getFullPath($rootDir, null); } + public function testPrepareQuery() { + + $rootDir = $this->_addRootDir(); + $childDir = $this->_addChildDir(); + + $rootSelection = new RootSelection(); + $rootSelection->excludeDir($rootDir); + + $childSelection = new ChildSelection(); + $childSelection->excludeDir($childDir); + + $query = $this->files()->prepareQuery($rootSelection, $childSelection); + $this->assertTrue($query->run('File2')); + $this->assertEquals(array(), $query->listPaths()); + } + public function testQueryPathWithTwoRootDirs() { $files = $this->files(); $this->_addRootDir(); @@ -259,8 +277,6 @@ public function testFileExist() { } - - protected function _addRootDir($index = null, RootDir $rootDir = null, $required = false) { $rootDir = $rootDir !== null ? $rootDir : $this->_newActualRootDir(null, $required); return $this->files()->addRootDir($rootDir, $index); diff --git a/tests/FQ/Tests/Query/FilesQueryBuilderTest.php b/tests/FQ/Tests/Query/FilesQueryBuilderTest.php index 65725f3..f3e7ddd 100644 --- a/tests/FQ/Tests/Query/FilesQueryBuilderTest.php +++ b/tests/FQ/Tests/Query/FilesQueryBuilderTest.php @@ -19,7 +19,7 @@ class FilesQueryBuilderTest extends AbstractFilesQueryTests { function setUp() { parent::setUp(); - $this->_builder = new FilesQueryBuilder($this->files()); + $this->_builder = new FilesQueryBuilder($this->query()); $this->nonPublicMethodObject($this->builder()); } protected function builder() { @@ -28,7 +28,7 @@ protected function builder() { public function testConstructor() { - $builder = new FilesQueryBuilder($this->files()); + $builder = new FilesQueryBuilder($this->query()); $this->assertNotNull($builder); $this->assertTrue($builder instanceof FilesQueryBuilder); } @@ -123,18 +123,18 @@ public function testAddRequirement() { $this->assertEquals($builder, $builder->addRequirement(FilesQueryRequirements::REQUIRE_ONE)); $this->assertEquals(array( FilesQueryRequirements::REQUIRE_ONE - ), $this->callNonPublicMethod('_getRequirements')); + ), $this->callNonPublicMethod('getRequirements')); } public function testReverse() { $builder = $this->builder(); - $this->assertFalse($this->callNonPublicMethod('_isReversed')); + $this->assertFalse($this->callNonPublicMethod('isReversed')); $builder->reverse(true); - $this->assertTrue($this->callNonPublicMethod('_isReversed')); + $this->assertTrue($this->callNonPublicMethod('isReversed')); $builder->reverse(false); - $this->assertFalse($this->callNonPublicMethod('_isReversed')); + $this->assertFalse($this->callNonPublicMethod('isReversed')); } public function testShowErrors() { @@ -149,18 +149,60 @@ public function testShowErrorsException() { public function testFilter() { $builder = $this->builder(); - $this->assertNull($this->callNonPublicMethod('_getFilters')); + $this->assertNull($this->callNonPublicMethod('getFilters')); $builder->filters(array()); - $this->assertEquals(array(), $this->callNonPublicMethod('_getFilters')); + $this->assertEquals(array(), $this->callNonPublicMethod('getFilters')); $builder->filters(FilesQuery::FILTER_EXISTING); - $this->assertEquals(FilesQuery::FILTER_EXISTING, $this->callNonPublicMethod('_getFilters')); + $this->assertEquals(FilesQuery::FILTER_EXISTING, $this->callNonPublicMethod('getFilters')); $builder->filters(array(FilesQuery::FILTER_EXISTING)); $this->assertEquals(array( FilesQuery::FILTER_EXISTING - ), $this->callNonPublicMethod('_getFilters')); + ), $this->callNonPublicMethod('getFilters')); + } + + public function testReset() { + $builder = $this->builder(); + + $this->assertEquals(array(), $builder->rootSelection()->getIncludedDirsByDir()); + $this->assertEquals(array(), $builder->childSelection()->getIncludedDirsByDir()); + $this->assertNull($builder->getFilters()); + $this->assertEquals(array(), $builder->getRequirements()); + $this->assertNull($builder->getFileName()); + $this->assertFalse($builder->isReversed()); + $this->assertTrue($builder->showsErrors()); + + $rootDirFirst = $this->_newActualRootDir(); + $rootDirSecond = $this->_newActualRootDirSecond(); + $childDir = $this->_newActualChildDir(); + $builder + ->includeRootDirs(array($rootDirFirst, $rootDirSecond)) + ->includeChildDirs($childDir) + ->filters(array(), false) + ->addRequirement(FilesQueryRequirements::REQUIRE_ALL) + ->fileName('Test') + ->reverse(true) + ->showErrors(false); + + $this->assertEquals(array($rootDirFirst, $rootDirSecond), $builder->rootSelection()->getIncludedDirsByDir()); + $this->assertEquals(array($childDir), $builder->childSelection()->getIncludedDirsByDir()); + $this->assertEquals(array(), $builder->getFilters()); + $this->assertEquals(array(FilesQueryRequirements::REQUIRE_ALL), $builder->getRequirements()); + $this->assertEquals('Test', $builder->getFileName()); + $this->assertTrue($builder->isReversed()); + $this->assertFalse($builder->showsErrors()); + + $builder->reset(); + + $this->assertEquals(array(), $builder->rootSelection()->getIncludedDirsByDir()); + $this->assertEquals(array(), $builder->childSelection()->getIncludedDirsByDir()); + $this->assertNull($builder->getFilters()); + $this->assertEquals(array(), $builder->getRequirements()); + $this->assertNull($builder->getFileName()); + $this->assertFalse($builder->isReversed()); + $this->assertTrue($builder->showsErrors()); } public function testRunBasicBuilderWithoutAFileName() { diff --git a/tests/FQ/Tests/Query/FilesQueryRequirementsTest.php b/tests/FQ/Tests/Query/FilesQueryRequirementsTest.php index a8d0186..0d36f93 100644 --- a/tests/FQ/Tests/Query/FilesQueryRequirementsTest.php +++ b/tests/FQ/Tests/Query/FilesQueryRequirementsTest.php @@ -28,7 +28,7 @@ protected function setUp() public function testConstructor() { - $filesQueryRequirements = new FilesQueryRequirements(); + $filesQueryRequirements = new FilesQueryRequirements($this->query()); $this->assertNotNull($filesQueryRequirements); $this->assertTrue($filesQueryRequirements instanceof FilesQueryRequirements); $this->assertEquals(3, $filesQueryRequirements->countRegisteredRequirements()); @@ -211,13 +211,13 @@ public function testTryRequirementFailure() { public function testMeetsRequirementsWithoutAnyActiveRequirements() { $requirements = $this->_getRequirementsInstance(); - $this->assertTrue($requirements->meetsRequirements($this->query())); + $this->assertTrue($requirements->meetsRequirements()); } public function testMeetsRequirementsWithOneRequirement() { $requirements = $this->_getRequirementsInstance(); $requirements->addRequirement(FilesQueryRequirements::REQUIRE_ONE); $this->runQuery(); - $this->assertTrue($requirements->meetsRequirements($this->query())); + $this->assertTrue($requirements->meetsRequirements()); } public function testMeetsRequirementsWithOneRequirementThatWillFail() { $this->setExpectedException('\FQ\Exceptions\FileQueryRequirementsException'); @@ -225,7 +225,7 @@ public function testMeetsRequirementsWithOneRequirementThatWillFail() { $requirements->addRequirement(FilesQueryRequirements::REQUIRE_ALL); $this->files()->addRootDir($this->_newActualRootDirSecond()); $this->runQuery(); - $this->assertTrue($requirements->meetsRequirements($this->query())); + $this->assertTrue($requirements->meetsRequirements()); } public function testMeetsRequirementsWithOneRequirementThatWillFailButIsNotThrowingAnError() { $this->setExpectedException('\FQ\Exceptions\FileQueryRequirementsException'); @@ -233,14 +233,14 @@ public function testMeetsRequirementsWithOneRequirementThatWillFailButIsNotThrow $requirements->addRequirement(FilesQueryRequirements::REQUIRE_ALL); $this->files()->addRootDir($this->_newActualRootDirSecond()); $this->runQuery(); - throw $requirements->meetsRequirements($this->query(), false); + throw $requirements->meetsRequirements(false); } /** * @return FilesQueryRequirements */ protected function _newRequirements() { - return new FilesQueryRequirements(); + return new FilesQueryRequirements($this->query()); } protected function _getRequirementsInstance() { diff --git a/tests/FQ/Tests/Query/FilesQueryTest.php b/tests/FQ/Tests/Query/FilesQueryTest.php index d14a911..1e5f2b8 100644 --- a/tests/FQ/Tests/Query/FilesQueryTest.php +++ b/tests/FQ/Tests/Query/FilesQueryTest.php @@ -103,9 +103,6 @@ public function testGetRootDirSelection() { $query = $this->query(); $currentRootDirSelection = $query->getRootDirSelection(); $this->assertEquals('FQ\Query\Selection\RootSelection', get_class($currentRootDirSelection)); - - $newRootDirSelection = $query->getRootDirSelection(true); - $this->assertNotEquals(spl_object_hash($currentRootDirSelection), spl_object_hash($newRootDirSelection)); } public function testSetChildDirSelection() { @@ -121,9 +118,6 @@ public function testGetChildDirSelection() { $query = $this->query(); $currentChildDirSelection = $query->getChildDirSelection(); $this->assertEquals('FQ\Query\Selection\ChildSelection', get_class($currentChildDirSelection)); - - $newRootDirSelection = $query->getChildDirSelection(true); - $this->assertNotEquals(spl_object_hash($currentChildDirSelection), spl_object_hash($newRootDirSelection)); } public function testIsValidChildDir() { @@ -170,7 +164,7 @@ public function testGetCurrentChildDirSelection() { public function testProcessQueryChild() { $query = $this->query(); $childDirs = $query->files()->childDirs(); - $this->assertNotFalse($this->callNonPublicMethod('_processQueryChild', array($childDirs[0], $query->getCurrentRootDirSelection()))); + $this->assertNotFalse($this->callNonPublicMethod('_prepareQueryChild', array($childDirs[0], $query->getCurrentRootDirSelection()))); } public function testRunQueryWhenRequirementsAreNotMet() { $query = $this->query(); diff --git a/tests/FQ/Tests/Query/Selection/ChildSelectionTest.php b/tests/FQ/Tests/Query/Selection/ChildSelectionTest.php new file mode 100644 index 0000000..e5a0f39 --- /dev/null +++ b/tests/FQ/Tests/Query/Selection/ChildSelectionTest.php @@ -0,0 +1,46 @@ +_dirSelection = new ChildSelection(); + $this->nonPublicMethodObject($this->dirSelection()); + } + protected function dirSelection() { + return $this->_dirSelection; + } + + public function testConstructor() { + $dirSelection = new ChildSelection(); + $this->assertNotNull($dirSelection); + } + + public function testCopy() { + $selection = $this->dirSelection(); + $dir1 = $this->_newActualRootDir(); + $dir2 = $this->_newActualRootDirSecond(); + $selection->excludeDir($dir1); + $selection->excludeDir($dir2); + + $copy = $selection->copy(); + $this->assertEquals('FQ\Query\Selection\ChildSelection', get_class($copy)); + $this->assertNotEquals($selection, $copy); + $this->assertEquals(array(), $selection->getIncludedDirsById()); + $this->assertEquals(array(), $selection->getIncludedDirsByDir()); + $this->assertEquals(array(), $selection->getExcludedDirsById()); + $this->assertEquals(array($dir1, $dir2), $selection->getExcludedDirsByDir()); + } +} \ No newline at end of file diff --git a/tests/FQ/Tests/Query/Selection/DirSelectionTest.php b/tests/FQ/Tests/Query/Selection/DirSelectionTest.php index f602000..d7222e5 100644 --- a/tests/FQ/Tests/Query/Selection/DirSelectionTest.php +++ b/tests/FQ/Tests/Query/Selection/DirSelectionTest.php @@ -44,25 +44,105 @@ public function testReset() { public function testIncludeDirById() { $selection = $this->dirSelection(); - $selection->includeDirById('dir1'); - $selection->includeDirById('dir2'); + $this->assertTrue($selection->includeDirById('dir1')); + $this->assertTrue($selection->includeDirById('dir2')); $this->assertEquals(array('dir1', 'dir2'), $selection->getIncludedDirsById()); $this->assertFalse($selection->hasIncludedDirsByDir()); $this->assertTrue($selection->hasIncludedDirsById()); $this->assertTrue($selection->hasIncludedDirs()); + + $selection->lock(); + $this->assertFalse($selection->includeDirById('dir3')); + $this->assertEquals(array('dir1', 'dir2'), $selection->getIncludedDirsById()); + + $selection->unlock(); + $this->assertTrue($selection->includeDirById('dir3')); + $this->assertEquals(array('dir1', 'dir2', 'dir3'), $selection->getIncludedDirsById()); + } + public function testRemoveIncludedDirById() { + $selection = $this->dirSelection(); + + $selection->includeDirById('dir1'); + $selection->includeDirById('dir2'); + + $this->assertTrue($selection->removeIncludedDirById('dir1')); + $this->assertFalse($selection->removeIncludedDirById('dir1')); + $this->assertEquals(array('dir2'), $selection->getIncludedDirsById()); + + $selection->lock(); + $this->assertNull($selection->removeIncludedDirById('dir2')); + $this->assertEquals(array('dir2'), $selection->getIncludedDirsById()); + + $selection->unlock(); + $this->assertTrue($selection->removeIncludedDirById('dir2')); + $this->assertEquals(array(), $selection->getIncludedDirsById()); + } + public function testRemoveAllIncludedDirById() { + $selection = $this->dirSelection(); + + $selection->includeDirById('dir1'); + $selection->includeDirById('dir2'); + + $selection->lock(); + $this->assertFalse($selection->removeAllIncludedDirsById()); + $this->assertEquals(array('dir1', 'dir2'), $selection->getIncludedDirsById()); + + $selection->unlock(); + $this->assertTrue($selection->removeAllIncludedDirsById()); + $this->assertEquals(array(), $selection->getIncludedDirsById()); } public function testExcludeDirById() { $selection = $this->dirSelection(); - $selection->excludeDirById('dir1'); - $selection->excludeDirById('dir2'); + $this->assertTrue($selection->excludeDirById('dir1')); + $this->assertTrue($selection->excludeDirById('dir2')); $this->assertEquals(array('dir1', 'dir2'), $selection->getExcludedDirsById()); $this->assertFalse($selection->hasExcludedDirsByDir()); $this->assertTrue($selection->hasExcludedDirsById()); $this->assertTrue($selection->hasExcludedDirs()); + + $selection->lock(); + $this->assertFalse($selection->excludeDirById('dir3')); + $this->assertEquals(array('dir1', 'dir2'), $selection->getExcludedDirsById()); + + $selection->unlock(); + $this->assertTrue($selection->excludeDirById('dir3')); + $this->assertEquals(array('dir1', 'dir2', 'dir3'), $selection->getExcludedDirsById()); + } + public function testRemoveExcludedDirById() { + $selection = $this->dirSelection(); + + $selection->excludeDirById('dir1'); + $selection->excludeDirById('dir2'); + + $this->assertTrue($selection->removeExcludedDirById('dir1')); + $this->assertFalse($selection->removeExcludedDirById('dir1')); + $this->assertEquals(array('dir2'), $selection->getExcludedDirsById()); + + $selection->lock(); + $this->assertNull($selection->removeExcludedDirById('dir2')); + $this->assertEquals(array('dir2'), $selection->getExcludedDirsById()); + + $selection->unlock(); + $this->assertTrue($selection->removeExcludedDirById('dir2')); + $this->assertEquals(array(), $selection->getExcludedDirsById()); + } + public function testRemoveAllExcludedDirById() { + $selection = $this->dirSelection(); + + $selection->excludeDirById('dir1'); + $selection->excludeDirById('dir2'); + + $selection->lock(); + $this->assertFalse($selection->removeAllExcludedDirsById()); + $this->assertEquals(array('dir1', 'dir2'), $selection->getExcludedDirsById()); + + $selection->unlock(); + $this->assertTrue($selection->removeAllExcludedDirsById()); + $this->assertEquals(array(), $selection->getExcludedDirsById()); } public function testIncludeDirByDir() { @@ -70,6 +150,7 @@ public function testIncludeDirByDir() { $dir1 = $this->_newActualRootDir(); $dir2 = $this->_newActualRootDirSecond(); + $dir3 = $this->_newFictitiousRootDir(); $selection->includeDir($dir1); $selection->includeDir($dir2); @@ -77,12 +158,59 @@ public function testIncludeDirByDir() { $this->assertTrue($selection->hasIncludedDirsByDir()); $this->assertFalse($selection->hasIncludedDirsById()); $this->assertTrue($selection->hasIncludedDirs()); + + $selection->lock(); + $this->assertFalse($selection->includeDir($dir3)); + $this->assertEquals(array($dir1, $dir2), $selection->getIncludedDirsByDir()); + + $selection->unlock(); + $this->assertTrue($selection->includeDir($dir3)); + $this->assertEquals(array($dir1, $dir2, $dir3), $selection->getIncludedDirsByDir()); + } + public function testRemoveIncludedDir() { + $selection = $this->dirSelection(); + + $dir1 = $this->_newActualRootDir(); + $dir2 = $this->_newActualRootDirSecond(); + + $selection->includeDir($dir1); + $selection->includeDir($dir2); + + $this->assertTrue($selection->removeIncludedDir($dir1)); + $this->assertFalse($selection->removeIncludedDir($dir1)); + $this->assertEquals(array($dir2), $selection->getIncludedDirsByDir()); + + $selection->lock(); + $this->assertNull($selection->removeIncludedDir($dir2)); + $this->assertEquals(array($dir2), $selection->getIncludedDirsByDir()); + + $selection->unlock(); + $this->assertTrue($selection->removeIncludedDir($dir2)); + $this->assertEquals(array(), $selection->getIncludedDirsById()); + } + public function testRemoveAllIncludedDir() { + $selection = $this->dirSelection(); + + $dir1 = $this->_newActualRootDir(); + $dir2 = $this->_newActualRootDirSecond(); + + $selection->includeDir($dir1); + $selection->includeDir($dir2); + + $selection->lock(); + $this->assertFalse($selection->removeAllIncludedDirs()); + $this->assertEquals(array($dir1, $dir2), $selection->getIncludedDirsByDir()); + + $selection->unlock(); + $this->assertTrue($selection->removeAllIncludedDirs()); + $this->assertEquals(array(), $selection->getIncludedDirsByDir()); } public function testExcludeDirByDir() { $selection = $this->dirSelection(); $dir1 = $this->_newActualRootDir(); $dir2 = $this->_newActualRootDirSecond(); + $dir3 = $this->_newFictitiousRootDir(); $selection->excludeDir($dir1); $selection->excludeDir($dir2); @@ -90,6 +218,100 @@ public function testExcludeDirByDir() { $this->assertTrue($selection->hasExcludedDirsByDir()); $this->assertFalse($selection->hasExcludedDirsById()); $this->assertTrue($selection->hasExcludedDirs()); + + $selection->lock(); + $this->assertFalse($selection->excludeDir($dir3)); + $this->assertEquals(array($dir1, $dir2), $selection->getExcludedDirsByDir()); + + $selection->unlock(); + $this->assertTrue($selection->excludeDir($dir3)); + $this->assertEquals(array($dir1, $dir2, $dir3), $selection->getExcludedDirsByDir()); + } + public function testRemoveExcludedDir() { + $selection = $this->dirSelection(); + + $dir1 = $this->_newActualRootDir(); + $dir2 = $this->_newActualRootDirSecond(); + + $selection->excludeDir($dir1); + $selection->excludeDir($dir2); + + $this->assertTrue($selection->removeExcludedDir($dir1)); + $this->assertFalse($selection->removeExcludedDir($dir1)); + $this->assertEquals(array($dir2), $selection->getExcludedDirsByDir()); + + $selection->lock(); + $this->assertNull($selection->removeExcludedDir($dir2)); + $this->assertEquals(array($dir2), $selection->getExcludedDirsByDir()); + + $selection->unlock(); + $this->assertTrue($selection->removeExcludedDir($dir2)); + $this->assertEquals(array(), $selection->getExcludedDirsById()); + } + public function testRemoveAllExcludedDir() { + $selection = $this->dirSelection(); + + $dir1 = $this->_newActualRootDir(); + $dir2 = $this->_newActualRootDirSecond(); + + $selection->excludeDir($dir1); + $selection->excludeDir($dir2); + + $selection->lock(); + $this->assertFalse($selection->removeAllExcludedDirs()); + $this->assertEquals(array($dir1, $dir2), $selection->getExcludedDirsByDir()); + + $selection->unlock(); + $this->assertTrue($selection->removeAllExcludedDirs()); + $this->assertEquals(array(), $selection->getExcludedDirsByDir()); + } + + public function testUnsafeImport() { + $selection = $this->dirSelection(); + + $selection->unsafeImport(null, null, null, null); + + $this->assertEquals(array(), $selection->getIncludedDirsById()); + $this->assertEquals(array(), $selection->getIncludedDirsByDir()); + $this->assertEquals(array(), $selection->getExcludedDirsById()); + $this->assertEquals(array(), $selection->getExcludedDirsByDir()); + + $rootDir1 = $this->_newActualRootDir(); + $rootDir2 = $this->_newActualRootDirSecond(); + + // include + $selection->unsafeImport(null, array($rootDir1), null, null); + $this->assertEquals(array(), $selection->getIncludedDirsById()); + $this->assertEquals(array($rootDir1), $selection->getIncludedDirsByDir()); + $this->assertEquals(array(), $selection->getExcludedDirsById()); + $this->assertEquals(array(), $selection->getExcludedDirsByDir()); + $this->assertEquals(array($rootDir1), $selection->getSelection(array($rootDir1, $rootDir2))); + + $selection->removeAllIncludedDirs(); + + // exclude + $selection->unsafeImport(null, null, null, array($rootDir1)); + $this->assertEquals(array(), $selection->getIncludedDirsById()); + $this->assertEquals(array(), $selection->getIncludedDirsByDir()); + $this->assertEquals(array(), $selection->getExcludedDirsById()); + $this->assertEquals(array($rootDir1), $selection->getExcludedDirsByDir()); + $this->assertEquals(array($rootDir2), $selection->getSelection(array($rootDir1, $rootDir2))); + } + + public function testCopy() { + $selection = $this->dirSelection(); + $dir1 = $this->_newActualRootDir(); + $dir2 = $this->_newActualRootDirSecond(); + $selection->excludeDir($dir1); + $selection->excludeDir($dir2); + + $copy = $selection->copy(); + $this->assertEquals('FQ\Query\Selection\DirSelection', get_class($copy)); + $this->assertNotEquals($selection, $copy); + $this->assertEquals(array(), $selection->getIncludedDirsById()); + $this->assertEquals(array(), $selection->getIncludedDirsByDir()); + $this->assertEquals(array(), $selection->getExcludedDirsById()); + $this->assertEquals(array($dir1, $dir2), $selection->getExcludedDirsByDir()); } public function testIncludeAndExcludeADirAtTheSameTime() { diff --git a/tests/FQ/Tests/Query/Selection/RootSelectionTest.php b/tests/FQ/Tests/Query/Selection/RootSelectionTest.php new file mode 100644 index 0000000..ff3f20b --- /dev/null +++ b/tests/FQ/Tests/Query/Selection/RootSelectionTest.php @@ -0,0 +1,47 @@ +_dirSelection = new RootSelection(); + $this->nonPublicMethodObject($this->dirSelection()); + } + protected function dirSelection() { + return $this->_dirSelection; + } + + public function testConstructor() { + $dirSelection = new RootSelection(); + $this->assertNotNull($dirSelection); + } + + public function testCopy() { + $selection = $this->dirSelection(); + $dir1 = $this->_newActualRootDir(); + $dir2 = $this->_newActualRootDirSecond(); + $selection->excludeDir($dir1); + $selection->excludeDir($dir2); + + $copy = $selection->copy(); + $this->assertEquals('FQ\Query\Selection\RootSelection', get_class($copy)); + $this->assertNotEquals($selection, $copy); + $this->assertEquals(array(), $selection->getIncludedDirsById()); + $this->assertEquals(array(), $selection->getIncludedDirsByDir()); + $this->assertEquals(array(), $selection->getExcludedDirsById()); + $this->assertEquals(array($dir1, $dir2), $selection->getExcludedDirsByDir()); + } +} \ No newline at end of file