├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── composer.lock ├── phpunit.xml.dist ├── src └── phpDocumentor │ └── Fileset │ ├── Collection.php │ ├── Collection │ └── IgnorePatterns.php │ └── File.php └── tests ├── common └── bootstrap.php ├── data ├── fileWithText.txt └── test.phar └── unit └── phpDocumentor └── Fileset ├── Collection └── IgnorePatternsTest.php ├── CollectionTest.php └── FileTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | 3 | # composer & builds 4 | build 5 | vendor 6 | 7 | # Eclipse 8 | .buildpath 9 | .project 10 | .settings 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - 5.3.3 4 | - 5.3 5 | - 5.4 6 | - 5.5 7 | 8 | script: 9 | - vendor/bin/phpunit 10 | 11 | before_script: 12 | - sudo apt-get -qq update > /dev/null 13 | - phpenv rehash > /dev/null 14 | - composer selfupdate --quiet 15 | - composer install --dev --prefer-source 16 | - vendor/bin/phpunit 17 | - composer update --dev --prefer-source 18 | 19 | notifications: 20 | irc: "irc.freenode.org#phpdocumentor" 21 | email: 22 | - mike.vanriel@naenius.com 23 | - ashnazg@php.net 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010 Mike van Riel 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Fileset [![Build Status](https://secure.travis-ci.org/phpDocumentor/Fileset.png)](http://travis-ci.org/phpDocumentor/Fileset) 2 | ======= 3 | 4 | Project will be abandoned! 5 | ---- 6 | phpdocumentor is moving forward. This means that we are dropping maintenance on certain packages. This package will be replaced by other components. 7 | 8 | A fileset component that manages the collection of files using directories and filenames 9 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "phpdocumentor/fileset", 3 | "description": "Fileset component for collecting a set of files given directories and file paths", 4 | "keywords": ["phpdoc", "files", "fileset"], 5 | "homepage": "http://www.phpdoc.org", 6 | "license": "MIT", 7 | "autoload": { "psr-0":{ "phpDocumentor": ["src/", "tests/unit/"] } }, 8 | "require": { 9 | "php": ">=5.3.3", 10 | "symfony/finder": "~2.1" 11 | }, 12 | "require-dev": { 13 | "phpunit/phpunit": "~3.7" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" 5 | ], 6 | "hash": "191f6b1a1c9063b3a1215ba4d0fbe3dc", 7 | "packages": [ 8 | { 9 | "name": "symfony/finder", 10 | "version": "v2.3.2", 11 | "target-dir": "Symfony/Component/Finder", 12 | "source": { 13 | "type": "git", 14 | "url": "https://github.com/symfony/Finder.git", 15 | "reference": "v2.3.2" 16 | }, 17 | "dist": { 18 | "type": "zip", 19 | "url": "https://api.github.com/repos/symfony/Finder/zipball/v2.3.2", 20 | "reference": "v2.3.2", 21 | "shasum": "" 22 | }, 23 | "require": { 24 | "php": ">=5.3.3" 25 | }, 26 | "type": "library", 27 | "extra": { 28 | "branch-alias": { 29 | "dev-master": "2.3-dev" 30 | } 31 | }, 32 | "autoload": { 33 | "psr-0": { 34 | "Symfony\\Component\\Finder\\": "" 35 | } 36 | }, 37 | "notification-url": "https://packagist.org/downloads/", 38 | "license": [ 39 | "MIT" 40 | ], 41 | "authors": [ 42 | { 43 | "name": "Fabien Potencier", 44 | "email": "fabien@symfony.com" 45 | }, 46 | { 47 | "name": "Symfony Community", 48 | "homepage": "http://symfony.com/contributors" 49 | } 50 | ], 51 | "description": "Symfony Finder Component", 52 | "homepage": "http://symfony.com", 53 | "time": "2013-07-01 12:17:23" 54 | } 55 | ], 56 | "packages-dev": [ 57 | { 58 | "name": "phpunit/php-code-coverage", 59 | "version": "1.2.12", 60 | "source": { 61 | "type": "git", 62 | "url": "https://github.com/sebastianbergmann/php-code-coverage.git", 63 | "reference": "1.2.12" 64 | }, 65 | "dist": { 66 | "type": "zip", 67 | "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/1.2.12", 68 | "reference": "1.2.12", 69 | "shasum": "" 70 | }, 71 | "require": { 72 | "php": ">=5.3.3", 73 | "phpunit/php-file-iterator": ">=1.3.0@stable", 74 | "phpunit/php-text-template": ">=1.1.1@stable", 75 | "phpunit/php-token-stream": ">=1.1.3@stable" 76 | }, 77 | "require-dev": { 78 | "phpunit/phpunit": "3.7.*@dev" 79 | }, 80 | "suggest": { 81 | "ext-dom": "*", 82 | "ext-xdebug": ">=2.0.5" 83 | }, 84 | "type": "library", 85 | "extra": { 86 | "branch-alias": { 87 | "dev-master": "1.2.x-dev" 88 | } 89 | }, 90 | "autoload": { 91 | "classmap": [ 92 | "PHP/" 93 | ] 94 | }, 95 | "notification-url": "https://packagist.org/downloads/", 96 | "include-path": [ 97 | "" 98 | ], 99 | "license": [ 100 | "BSD-3-Clause" 101 | ], 102 | "authors": [ 103 | { 104 | "name": "Sebastian Bergmann", 105 | "email": "sb@sebastian-bergmann.de", 106 | "role": "lead" 107 | } 108 | ], 109 | "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", 110 | "homepage": "https://github.com/sebastianbergmann/php-code-coverage", 111 | "keywords": [ 112 | "coverage", 113 | "testing", 114 | "xunit" 115 | ], 116 | "time": "2013-07-06 06:26:16" 117 | }, 118 | { 119 | "name": "phpunit/php-file-iterator", 120 | "version": "1.3.3", 121 | "source": { 122 | "type": "git", 123 | "url": "git://github.com/sebastianbergmann/php-file-iterator.git", 124 | "reference": "1.3.3" 125 | }, 126 | "dist": { 127 | "type": "zip", 128 | "url": "https://github.com/sebastianbergmann/php-file-iterator/zipball/1.3.3", 129 | "reference": "1.3.3", 130 | "shasum": "" 131 | }, 132 | "require": { 133 | "php": ">=5.3.3" 134 | }, 135 | "type": "library", 136 | "autoload": { 137 | "classmap": [ 138 | "File/" 139 | ] 140 | }, 141 | "include-path": [ 142 | "" 143 | ], 144 | "license": [ 145 | "BSD-3-Clause" 146 | ], 147 | "authors": [ 148 | { 149 | "name": "Sebastian Bergmann", 150 | "email": "sb@sebastian-bergmann.de", 151 | "role": "lead" 152 | } 153 | ], 154 | "description": "FilterIterator implementation that filters files based on a list of suffixes.", 155 | "homepage": "http://www.phpunit.de/", 156 | "keywords": [ 157 | "filesystem", 158 | "iterator" 159 | ], 160 | "time": "2012-10-11 04:44:38" 161 | }, 162 | { 163 | "name": "phpunit/php-text-template", 164 | "version": "1.1.4", 165 | "source": { 166 | "type": "git", 167 | "url": "git://github.com/sebastianbergmann/php-text-template.git", 168 | "reference": "1.1.4" 169 | }, 170 | "dist": { 171 | "type": "zip", 172 | "url": "https://github.com/sebastianbergmann/php-text-template/zipball/1.1.4", 173 | "reference": "1.1.4", 174 | "shasum": "" 175 | }, 176 | "require": { 177 | "php": ">=5.3.3" 178 | }, 179 | "type": "library", 180 | "autoload": { 181 | "classmap": [ 182 | "Text/" 183 | ] 184 | }, 185 | "include-path": [ 186 | "" 187 | ], 188 | "license": [ 189 | "BSD-3-Clause" 190 | ], 191 | "authors": [ 192 | { 193 | "name": "Sebastian Bergmann", 194 | "email": "sb@sebastian-bergmann.de", 195 | "role": "lead" 196 | } 197 | ], 198 | "description": "Simple template engine.", 199 | "homepage": "https://github.com/sebastianbergmann/php-text-template/", 200 | "keywords": [ 201 | "template" 202 | ], 203 | "time": "2012-10-31 11:15:28" 204 | }, 205 | { 206 | "name": "phpunit/php-timer", 207 | "version": "1.0.5", 208 | "source": { 209 | "type": "git", 210 | "url": "https://github.com/sebastianbergmann/php-timer.git", 211 | "reference": "1.0.5" 212 | }, 213 | "dist": { 214 | "type": "zip", 215 | "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/1.0.5", 216 | "reference": "1.0.5", 217 | "shasum": "" 218 | }, 219 | "require": { 220 | "php": ">=5.3.3" 221 | }, 222 | "type": "library", 223 | "autoload": { 224 | "classmap": [ 225 | "PHP/" 226 | ] 227 | }, 228 | "notification-url": "https://packagist.org/downloads/", 229 | "include-path": [ 230 | "" 231 | ], 232 | "license": [ 233 | "BSD-3-Clause" 234 | ], 235 | "authors": [ 236 | { 237 | "name": "Sebastian Bergmann", 238 | "email": "sb@sebastian-bergmann.de", 239 | "role": "lead" 240 | } 241 | ], 242 | "description": "Utility class for timing", 243 | "homepage": "https://github.com/sebastianbergmann/php-timer/", 244 | "keywords": [ 245 | "timer" 246 | ], 247 | "time": "2013-08-02 07:42:54" 248 | }, 249 | { 250 | "name": "phpunit/php-token-stream", 251 | "version": "1.2.0", 252 | "source": { 253 | "type": "git", 254 | "url": "https://github.com/sebastianbergmann/php-token-stream.git", 255 | "reference": "1.2.0" 256 | }, 257 | "dist": { 258 | "type": "zip", 259 | "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/1.2.0", 260 | "reference": "1.2.0", 261 | "shasum": "" 262 | }, 263 | "require": { 264 | "ext-tokenizer": "*", 265 | "php": ">=5.3.3" 266 | }, 267 | "type": "library", 268 | "extra": { 269 | "branch-alias": { 270 | "dev-master": "1.2-dev" 271 | } 272 | }, 273 | "autoload": { 274 | "classmap": [ 275 | "PHP/" 276 | ] 277 | }, 278 | "notification-url": "https://packagist.org/downloads/", 279 | "include-path": [ 280 | "" 281 | ], 282 | "license": [ 283 | "BSD-3-Clause" 284 | ], 285 | "authors": [ 286 | { 287 | "name": "Sebastian Bergmann", 288 | "email": "sb@sebastian-bergmann.de", 289 | "role": "lead" 290 | } 291 | ], 292 | "description": "Wrapper around PHP's tokenizer extension.", 293 | "homepage": "https://github.com/sebastianbergmann/php-token-stream/", 294 | "keywords": [ 295 | "tokenizer" 296 | ], 297 | "time": "2013-08-04 05:57:48" 298 | }, 299 | { 300 | "name": "phpunit/phpunit", 301 | "version": "3.7.23", 302 | "source": { 303 | "type": "git", 304 | "url": "https://github.com/sebastianbergmann/phpunit.git", 305 | "reference": "3.7.23" 306 | }, 307 | "dist": { 308 | "type": "zip", 309 | "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/3.7.23", 310 | "reference": "3.7.23", 311 | "shasum": "" 312 | }, 313 | "require": { 314 | "ext-dom": "*", 315 | "ext-pcre": "*", 316 | "ext-reflection": "*", 317 | "ext-spl": "*", 318 | "php": ">=5.3.3", 319 | "phpunit/php-code-coverage": "~1.2.1", 320 | "phpunit/php-file-iterator": ">=1.3.1", 321 | "phpunit/php-text-template": ">=1.1.1", 322 | "phpunit/php-timer": ">=1.0.4", 323 | "phpunit/phpunit-mock-objects": "~1.2.0", 324 | "symfony/yaml": "~2.0" 325 | }, 326 | "require-dev": { 327 | "pear-pear/pear": "1.9.4" 328 | }, 329 | "suggest": { 330 | "ext-json": "*", 331 | "ext-simplexml": "*", 332 | "ext-tokenizer": "*", 333 | "phpunit/php-invoker": ">=1.1.0,<1.2.0" 334 | }, 335 | "bin": [ 336 | "composer/bin/phpunit" 337 | ], 338 | "type": "library", 339 | "extra": { 340 | "branch-alias": { 341 | "dev-master": "3.7.x-dev" 342 | } 343 | }, 344 | "autoload": { 345 | "classmap": [ 346 | "PHPUnit/" 347 | ] 348 | }, 349 | "notification-url": "https://packagist.org/downloads/", 350 | "include-path": [ 351 | "", 352 | "../../symfony/yaml/" 353 | ], 354 | "license": [ 355 | "BSD-3-Clause" 356 | ], 357 | "authors": [ 358 | { 359 | "name": "Sebastian Bergmann", 360 | "email": "sebastian@phpunit.de", 361 | "role": "lead" 362 | } 363 | ], 364 | "description": "The PHP Unit Testing framework.", 365 | "homepage": "http://www.phpunit.de/", 366 | "keywords": [ 367 | "phpunit", 368 | "testing", 369 | "xunit" 370 | ], 371 | "time": "2013-08-02 19:14:44" 372 | }, 373 | { 374 | "name": "phpunit/phpunit-mock-objects", 375 | "version": "1.2.3", 376 | "source": { 377 | "type": "git", 378 | "url": "git://github.com/sebastianbergmann/phpunit-mock-objects.git", 379 | "reference": "1.2.3" 380 | }, 381 | "dist": { 382 | "type": "zip", 383 | "url": "https://github.com/sebastianbergmann/phpunit-mock-objects/archive/1.2.3.zip", 384 | "reference": "1.2.3", 385 | "shasum": "" 386 | }, 387 | "require": { 388 | "php": ">=5.3.3", 389 | "phpunit/php-text-template": ">=1.1.1@stable" 390 | }, 391 | "suggest": { 392 | "ext-soap": "*" 393 | }, 394 | "type": "library", 395 | "autoload": { 396 | "classmap": [ 397 | "PHPUnit/" 398 | ] 399 | }, 400 | "notification-url": "https://packagist.org/downloads/", 401 | "include-path": [ 402 | "" 403 | ], 404 | "license": [ 405 | "BSD-3-Clause" 406 | ], 407 | "authors": [ 408 | { 409 | "name": "Sebastian Bergmann", 410 | "email": "sb@sebastian-bergmann.de", 411 | "role": "lead" 412 | } 413 | ], 414 | "description": "Mock Object library for PHPUnit", 415 | "homepage": "https://github.com/sebastianbergmann/phpunit-mock-objects/", 416 | "keywords": [ 417 | "mock", 418 | "xunit" 419 | ], 420 | "time": "2013-01-13 10:24:48" 421 | }, 422 | { 423 | "name": "symfony/yaml", 424 | "version": "v2.3.2", 425 | "target-dir": "Symfony/Component/Yaml", 426 | "source": { 427 | "type": "git", 428 | "url": "https://github.com/symfony/Yaml.git", 429 | "reference": "v2.3.2" 430 | }, 431 | "dist": { 432 | "type": "zip", 433 | "url": "https://api.github.com/repos/symfony/Yaml/zipball/v2.3.2", 434 | "reference": "v2.3.2", 435 | "shasum": "" 436 | }, 437 | "require": { 438 | "php": ">=5.3.3" 439 | }, 440 | "type": "library", 441 | "extra": { 442 | "branch-alias": { 443 | "dev-master": "2.3-dev" 444 | } 445 | }, 446 | "autoload": { 447 | "psr-0": { 448 | "Symfony\\Component\\Yaml\\": "" 449 | } 450 | }, 451 | "notification-url": "https://packagist.org/downloads/", 452 | "license": [ 453 | "MIT" 454 | ], 455 | "authors": [ 456 | { 457 | "name": "Fabien Potencier", 458 | "email": "fabien@symfony.com" 459 | }, 460 | { 461 | "name": "Symfony Community", 462 | "homepage": "http://symfony.com/contributors" 463 | } 464 | ], 465 | "description": "Symfony Yaml Component", 466 | "homepage": "http://symfony.com", 467 | "time": "2013-07-11 19:36:36" 468 | } 469 | ], 470 | "aliases": [ 471 | 472 | ], 473 | "minimum-stability": "stable", 474 | "stability-flags": [ 475 | 476 | ], 477 | "platform": { 478 | "php": ">=5.3.3" 479 | }, 480 | "platform-dev": [ 481 | 482 | ] 483 | } 484 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ./tests/ 7 | 8 | 9 | 10 | 11 | . 12 | 13 | build 14 | tests 15 | vendor 16 | 17 | 18 | 19 | 20 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/phpDocumentor/Fileset/Collection.php: -------------------------------------------------------------------------------- 1 | 8 | * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com) 9 | * @license http://www.opensource.org/licenses/mit-license.php MIT 10 | * @link http://phpdoc.org 11 | */ 12 | 13 | namespace phpDocumentor\Fileset; 14 | 15 | /** 16 | * Files container handling directory scanning, project root detection and ignores. 17 | * 18 | * Always set any filtering options (extensions, ignore patterns, hidden files, symlinks) 19 | * _before_ adding any directories or files. Such filtering is done immediately 20 | * upon loading the directory/file. As such, setting filtering options 21 | * _after_ adding directories/files will seem as though your filters were ignored. 22 | * 23 | * @author Mike van Riel 24 | * @license http://www.opensource.org/licenses/mit-license.php MIT 25 | * @link http://phpdoc.org 26 | */ 27 | class Collection extends \ArrayObject 28 | { 29 | /** @var bool Whether to follow symlinks*/ 30 | protected $follow_symlinks = false; 31 | 32 | /** @var bool Whether to ignore hidden files and folders */ 33 | protected $ignore_hidden = false; 34 | 35 | /** @var Collection\IgnorePatterns */ 36 | protected $ignore_patterns = array(); 37 | 38 | /** @var \ArrayObject Array containing a list of allowed line endings */ 39 | protected $allowed_extensions = null; 40 | 41 | /** @var string[] An array containing file names */ 42 | protected $files = array(); 43 | 44 | /** 45 | * Initializes the finding component. 46 | */ 47 | public function __construct() 48 | { 49 | $this->ignore_patterns = new Collection\IgnorePatterns(); 50 | $this->allowed_extensions = new \ArrayObject( 51 | array('php', 'php3', 'phtml') 52 | ); 53 | } 54 | 55 | /** 56 | * Sets the patterns by which to detect which files to ignore. 57 | * 58 | * @param array $patterns Glob-like patterns to filter files. 59 | * 60 | * @return void 61 | */ 62 | public function setIgnorePatterns(array $patterns) 63 | { 64 | $this->ignore_patterns = new Collection\IgnorePatterns($patterns); 65 | } 66 | 67 | /** 68 | * Returns the ignore patterns. 69 | * 70 | * @return Collection\IgnorePatterns 71 | */ 72 | public function getIgnorePatterns() 73 | { 74 | return $this->ignore_patterns; 75 | } 76 | 77 | /** 78 | * Sets a list of allowed extensions; if not used php, php3 and phtml 79 | * is assumed. 80 | * 81 | * @param array $extensions An array containing extensions to match for. 82 | * 83 | * @return void 84 | */ 85 | public function setAllowedExtensions(array $extensions) 86 | { 87 | $this->allowed_extensions = new \ArrayObject($extensions); 88 | } 89 | 90 | /** 91 | * Adds a file extension to the list of allowed extensions. 92 | * 93 | * No dot is necessary and will even prevent the extension from being 94 | * picked up. 95 | * 96 | * @param string $extension Allowed file Extension to add (i.e. php). 97 | * 98 | * @return void 99 | */ 100 | public function addAllowedExtension($extension) 101 | { 102 | $this->allowed_extensions->append($extension); 103 | } 104 | 105 | /** 106 | * Adds the content of a set of directories to the list of files to parse. 107 | * 108 | * @param array $paths The paths whose contents to add to the collection. 109 | * 110 | * @return void 111 | */ 112 | public function addDirectories(array $paths) 113 | { 114 | foreach ($paths as $path) { 115 | $this->addDirectory($path); 116 | } 117 | } 118 | 119 | /** 120 | * Retrieve all files in the given directory and add them to the parsing list. 121 | * 122 | * @param string $path A path to a folder, may be relative, absolute or 123 | * even phar. 124 | * 125 | * @return void 126 | */ 127 | public function addDirectory($path) 128 | { 129 | $finder = new \Symfony\Component\Finder\Finder(); 130 | 131 | $patterns = $this->getIgnorePatterns()->getRegularExpression(); 132 | 133 | if ($this->follow_symlinks) { 134 | $finder->followLinks(); 135 | } 136 | 137 | // restrict names to those ending in the given extensions 138 | $finder 139 | ->files() 140 | ->in($path) 141 | ->name( 142 | '/\.('.implode('|', $this->allowed_extensions->getArrayCopy()).')$/' 143 | ) 144 | ->ignoreDotFiles($this->getIgnoreHidden()) 145 | ->filter( 146 | function(\SplFileInfo $file) use ($patterns) { 147 | if (!$patterns) { 148 | return true; 149 | } 150 | 151 | // apply ignore list on path instead of file, finder 152 | // can't do that by default 153 | return !preg_match($patterns, $file->getPathname()); 154 | } 155 | ); 156 | 157 | try { 158 | /** @var \SplFileInfo $file */ 159 | foreach ($finder as $file) { 160 | $file = new File($file); 161 | $path = $file->getRealPath() 162 | ? $file->getRealPath() 163 | : $file->getPathname(); 164 | 165 | $this[$path] = $file; 166 | } 167 | } catch(\LogicException $e) 168 | { 169 | // if a logic exception is thrown then no folders were included 170 | // for phpDocumentor this is not an issue since we accept separate 171 | // files as well 172 | } 173 | } 174 | 175 | /** 176 | * Adds a list of individual files to the collection. 177 | * 178 | * @param array $paths File locations, may be absolute, relative or even phar. 179 | * 180 | * @return void 181 | */ 182 | public function addFiles(array $paths) 183 | { 184 | foreach ($paths as $path) { 185 | $this->addFile($path); 186 | } 187 | } 188 | 189 | /** 190 | * Adds a file to the collection. 191 | * 192 | * @param string $path File location, may be absolute, relative or even phar. 193 | * 194 | * @return void 195 | */ 196 | public function addFile($path) 197 | { 198 | $paths = $this->getGlobbedPaths($path); 199 | foreach ($paths as $path) { 200 | $file = new File($path); 201 | $path = $file->getRealPath() 202 | ? $file->getRealPath() 203 | : $file->getPathname(); 204 | 205 | $this[$path] = $file; 206 | } 207 | } 208 | 209 | /** 210 | * Get a globbed list out of the given path. 211 | * 212 | * This wrapper method normalizes for OS-divergent behavior of the native glob() function. 213 | * @param string $path 214 | * @return array 215 | */ 216 | protected function getGlobbedPaths($path) 217 | { 218 | $paths = glob($path); 219 | 220 | /* 221 | * Windows glob('') returns an array with one empty member... 222 | * 'nix glob('') returns an empty array... 223 | * we'd prefer to have the File('') constructor throw the exception in this edge case, 224 | * so keep the Windows behavior. 225 | */ 226 | $result = (array() === $paths) ? array('') : $paths; 227 | 228 | return $result; 229 | } 230 | 231 | /** 232 | * Returns a list of file paths that are ready to be parsed. 233 | * 234 | * Please note that the ignore pattern will be applied and all files are 235 | * converted to absolute paths. 236 | * 237 | * @return string[] 238 | */ 239 | public function getFilenames() 240 | { 241 | return array_keys($this->getArrayCopy()); 242 | } 243 | 244 | /** 245 | * Calculates the project root from the given files by determining their 246 | * highest common path. 247 | * 248 | * @return string 249 | */ 250 | public function getProjectRoot() 251 | { 252 | $base = ''; 253 | $files = array_keys($this->getArrayCopy()); 254 | $parts = explode(DIRECTORY_SEPARATOR, reset($files)); 255 | 256 | foreach ($parts as $part) { 257 | $base_part = $base . $part . DIRECTORY_SEPARATOR; 258 | foreach ($files as $dir) { 259 | if (substr($dir, 0, strlen($base_part)) != $base_part) { 260 | return $base; 261 | } 262 | } 263 | 264 | $base = $base_part; 265 | } 266 | 267 | return $base; 268 | } 269 | 270 | /** 271 | * Sets whether to ignore hidden files and folders. 272 | * 273 | * @param boolean $ignore_hidden if true skips hidden files and folders. 274 | * 275 | * @return void 276 | */ 277 | public function setIgnoreHidden($ignore_hidden) 278 | { 279 | $this->ignore_hidden = $ignore_hidden; 280 | } 281 | 282 | /** 283 | * Returns whether files and folders that are hidden are ignored. 284 | * 285 | * @return boolean 286 | */ 287 | public function getIgnoreHidden() 288 | { 289 | return $this->ignore_hidden; 290 | } 291 | 292 | /** 293 | * Sets whether to follow symlinks. 294 | * 295 | * PHP version 5.2.11 is at least required since the 296 | * RecursiveDirectoryIterator does not support the FOLLOW_SYMLINKS 297 | * constant before that version. 298 | * 299 | * @param boolean $follow_symlinks 300 | * 301 | * @return void 302 | */ 303 | public function setFollowSymlinks($follow_symlinks) 304 | { 305 | $this->follow_symlinks = $follow_symlinks; 306 | } 307 | 308 | /** 309 | * Returns whether to follow symlinks. 310 | * 311 | * @return boolean 312 | */ 313 | public function getFollowSymlinks() 314 | { 315 | return $this->follow_symlinks; 316 | } 317 | 318 | } 319 | -------------------------------------------------------------------------------- /src/phpDocumentor/Fileset/Collection/IgnorePatterns.php: -------------------------------------------------------------------------------- 1 | count() > 0) { 12 | $patterns = array(); 13 | foreach ($this as $item) { 14 | $this->convertToPregCompliant($item); 15 | $patterns[] = $item; 16 | } 17 | 18 | $pattern = '/('.implode('|', $patterns).')$/'; 19 | } 20 | 21 | return $pattern; 22 | } 23 | 24 | /** 25 | * Converts $string into a string that can be used with preg_match. 26 | * 27 | * @param string &$string Glob-like pattern with wildcards ? and *. 28 | * 29 | * @author Greg Beaver 30 | * @author mike van Riel 31 | * 32 | * @see PhpDocumentor/phpDocumentor/Io.php 33 | * 34 | * @return void 35 | */ 36 | protected function convertToPregCompliant(&$string) 37 | { 38 | $y = (DIRECTORY_SEPARATOR == '\\') ? '\\\\' : '\/'; 39 | $string = str_replace('/', DIRECTORY_SEPARATOR, $string); 40 | $x = strtr( 41 | $string, 42 | array( 43 | '?' => '.', 44 | '*' => '.*', 45 | '.' => '\\.', 46 | '\\' => '\\\\', 47 | '/' => '\\/', 48 | '[' => '\\[', 49 | ']' => '\\]', 50 | '-' => '\\-' 51 | ) 52 | ); 53 | 54 | if ((strpos($string, DIRECTORY_SEPARATOR) !== false) 55 | && (strrpos($string, DIRECTORY_SEPARATOR) === strlen($string) - 1) 56 | ) { 57 | $x = "(?:.*$y$x?.*|$x.*)"; 58 | } 59 | 60 | $string = $x; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/phpDocumentor/Fileset/File.php: -------------------------------------------------------------------------------- 1 | getPathname(); 18 | } 19 | 20 | if (!is_string($file)) { 21 | throw new \InvalidArgumentException( 22 | 'Expected filename or object of type SplFileInfo but received ' 23 | . get_class($file) 24 | ); 25 | } 26 | 27 | if (empty($file)) { 28 | throw new \InvalidArgumentException( 29 | 'Expected filename or object of type SplFileInfo but received nothing at all' 30 | ); 31 | } 32 | 33 | parent::__construct($file); 34 | } 35 | 36 | /** 37 | * Returns the mime type for this file. 38 | * 39 | * @throws \RuntimeException if finfo failed to load 40 | * and/or mime_content_type is unavailable 41 | * @throws \LogicException if the mime type could not be interpreted 42 | * from the output of finfo_file 43 | * 44 | * @return string 45 | */ 46 | public function getMimeType() 47 | { 48 | if (function_exists('finfo_open')) { 49 | $finfo = finfo_open(FILEINFO_MIME); 50 | if (!$finfo) { 51 | throw new \RuntimeException('Failed to open finfo'); 52 | } 53 | 54 | $actualInfo = @finfo_file($finfo, $this->getPathname()); 55 | if (false === $actualInfo) { 56 | throw new \RuntimeException('Failed to read file info via finfo'); 57 | } 58 | $mime = strtolower($actualInfo); 59 | finfo_close($finfo); 60 | 61 | if (!preg_match( 62 | '/^([a-z0-9]+\/[a-z0-9\-\.]+);\s+charset=(.*)$/', $mime, $matches 63 | )) { 64 | throw new \LogicException( 65 | 'An error parsing the MIME type "'.$mime.'".' 66 | ); 67 | } 68 | 69 | return $matches[1]; 70 | } elseif (function_exists('mime_content_type')) { 71 | return mime_content_type($this->getPathname()); 72 | } 73 | 74 | throw new \RuntimeException( 75 | 'The finfo extension or mime_content_type function are needed to ' 76 | .'determine the Mime Type for this file.' 77 | ); 78 | } 79 | 80 | /** 81 | * Returns the file contents as a string. 82 | * 83 | * @return string 84 | * @throws \RuntimeException if unable to open the file 85 | */ 86 | public function fread() 87 | { 88 | try { 89 | $file = $this->openFile('r'); 90 | } catch (\Exception $exc) { 91 | throw new \RuntimeException('Unable to open file', 0, $exc); 92 | } 93 | 94 | $result = ''; 95 | foreach ($file as $line) { 96 | $result .= $line; 97 | } 98 | 99 | return $result; 100 | } 101 | 102 | /** 103 | * Returns the filename, relative to the given root. 104 | * 105 | * @param string $root_path The root_path of which this file is composed. 106 | * 107 | * @throws \InvalidArgumentException if file is not in the project root. 108 | * 109 | * @return string 110 | * 111 | * @todo this protected method is unused in this class... can it be removed? 112 | */ 113 | protected function getFilenameRelativeToRoot($root_path) 114 | { 115 | // strip path from filename 116 | $result = ltrim( 117 | substr($this->getPathname(), strlen($root_path)), 118 | DIRECTORY_SEPARATOR 119 | ); 120 | 121 | if ($result === '') { 122 | throw new \InvalidArgumentException( 123 | 'File "' . $this->getPathname() . '" is not present in the ' 124 | .'given root: ' . $root_path 125 | ); 126 | } 127 | 128 | return $result; 129 | } 130 | 131 | } 132 | -------------------------------------------------------------------------------- /tests/common/bootstrap.php: -------------------------------------------------------------------------------- 1 | 8 | * @copyright 2010-2012 Mike van Riel / Naenius (http://www.naenius.com) 9 | * @license http://www.opensource.org/licenses/mit-license.php MIT 10 | * @link http://phpdoc.org 11 | */ 12 | 13 | require_once 'PHPUnit/Extensions/OutputTestCase.php'; 14 | require_once __DIR__.'/../../vendor/autoload.php'; -------------------------------------------------------------------------------- /tests/data/fileWithText.txt: -------------------------------------------------------------------------------- 1 | one line of text... 2 | another line of text. -------------------------------------------------------------------------------- /tests/data/test.phar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phpDocumentor/Fileset/64d215c07f0a0adda01fd660da3e3ca001679c6b/tests/data/test.phar -------------------------------------------------------------------------------- /tests/unit/phpDocumentor/Fileset/Collection/IgnorePatternsTest.php: -------------------------------------------------------------------------------- 1 | 8 | * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com) 9 | * @license http://www.opensource.org/licenses/mit-license.php MIT 10 | * @link http://phpdoc.org 11 | */ 12 | 13 | namespace phpDocumentor\Fileset\Collection; 14 | 15 | /** 16 | * Test case for IgnorePatterns class. 17 | * 18 | * @author Mike van Riel 19 | * @license http://www.opensource.org/licenses/mit-license.php MIT 20 | * @link http://phpdoc.org 21 | */ 22 | class IgnorePatternsTest extends \PHPUnit_Framework_TestCase 23 | { 24 | /** @var IgnorePatterns */ 25 | protected $fixture = null; 26 | 27 | /** @var bool */ 28 | protected $isWindows = false; 29 | 30 | protected function setUp() 31 | { 32 | $this->fixture = new IgnorePatterns(); 33 | if ('win' === strtolower(substr(PHP_OS, 0, 3))) { 34 | $this->isWindows = true; 35 | } 36 | } 37 | 38 | /** @covers \phpDocumentor\Fileset\Collection\IgnorePatterns::getRegularExpression() */ 39 | public function testGetRegularExpressionWhenGivenNoPatternsReturnsNothing() 40 | { 41 | $this->assertEquals('', $this->fixture->getRegularExpression()); 42 | } 43 | 44 | /** @covers \phpDocumentor\Fileset\Collection\IgnorePatterns::getRegularExpression() */ 45 | public function testGetRegularExpressionWhenGivenOnePatternsReturnsOneString() 46 | { 47 | $this->fixture->append('*r/'); 48 | $expected = ($this->isWindows) 49 | ? '/((?:.*\\\\.*r\\\?.*|.*r\\\\.*))$/' 50 | : '/((?:.*\/.*r\/?.*|.*r\/.*))$/' 51 | ; 52 | $this->assertEquals($expected, $this->fixture->getRegularExpression()); 53 | } 54 | 55 | /** @covers \phpDocumentor\Fileset\Collection\IgnorePatterns::convertToPregCompliant() */ 56 | public function testtestConvertToPregCompliantForGlobbedDir() 57 | { 58 | $this->fixture->append('*r/'); 59 | $expected = ($this->isWindows) 60 | ? '/((?:.*\\\\.*r\\\?.*|.*r\\\\.*))$/' 61 | : '/((?:.*\/.*r\/?.*|.*r\/.*))$/' 62 | ; 63 | $this->assertEquals($expected, $this->fixture->getRegularExpression()); 64 | } 65 | 66 | /** @covers \phpDocumentor\Fileset\Collection\IgnorePatterns::convertToPregCompliant() */ 67 | public function testConvertToPregCompliantForFilenameMask() 68 | { 69 | $this->fixture->append('Fileset*'); 70 | $expected = ($this->isWindows) 71 | ? '/(Fileset.*)$/' 72 | : '/(Fileset.*)$/' 73 | ; 74 | $this->assertEquals($expected, $this->fixture->getRegularExpression()); 75 | } 76 | 77 | /** @covers \phpDocumentor\Fileset\Collection\IgnorePatterns::convertToPregCompliant() */ 78 | public function testConvertToPregCompliantForFilenameMaskDotExtension() 79 | { 80 | $this->fixture->append('Fileset*.php'); 81 | $expected = ($this->isWindows) 82 | ? '/(Fileset.*\.php)$/' 83 | : '/(Fileset.*\.php)$/' 84 | ; 85 | $this->assertEquals($expected, $this->fixture->getRegularExpression()); 86 | } 87 | 88 | /** @covers \phpDocumentor\Fileset\Collection\IgnorePatterns::convertToPregCompliant() */ 89 | public function testConvertToPregCompliantForGlobbedDirAndFilenameMaskDotExtension() 90 | { 91 | $this->fixture->append('*r/Fileset*.php'); 92 | $expected = ($this->isWindows) 93 | ? '/(.*r\\\\Fileset.*\.php)$/' 94 | : '/(.*r\/Fileset.*\.php)$/' 95 | ; 96 | $this->assertEquals($expected, $this->fixture->getRegularExpression()); 97 | } 98 | 99 | /** @covers \phpDocumentor\Fileset\Collection\IgnorePatterns::convertToPregCompliant() */ 100 | public function testConvertToPregCompliantForThreeStars() 101 | { 102 | $this->fixture->append('***'); 103 | $expected = ($this->isWindows) 104 | ? '/(.*.*.*)$/' 105 | : '/(.*.*.*)$/' 106 | ; 107 | $this->assertEquals($expected, $this->fixture->getRegularExpression()); 108 | } 109 | 110 | /** @covers \phpDocumentor\Fileset\Collection\IgnorePatterns::convertToPregCompliant() */ 111 | public function testConvertToPregCompliantForPlainString() 112 | { 113 | $this->fixture->append('plainString'); 114 | $expected = ($this->isWindows) 115 | ? '/(plainString)$/' 116 | : '/(plainString)$/' 117 | ; 118 | $this->assertEquals($expected, $this->fixture->getRegularExpression()); 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /tests/unit/phpDocumentor/Fileset/CollectionTest.php: -------------------------------------------------------------------------------- 1 | 8 | * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com) 9 | * @license http://www.opensource.org/licenses/mit-license.php MIT 10 | * @link http://phpdoc.org 11 | */ 12 | 13 | namespace phpDocumentor\Fileset; 14 | 15 | use phpDocumentor\Fileset\Collection\IgnorePatterns; 16 | 17 | /** 18 | * Test case for Collection class. 19 | * 20 | * @author Mike van Riel 21 | * @license http://www.opensource.org/licenses/mit-license.php MIT 22 | * @link http://phpdoc.org 23 | */ 24 | class CollectionTest extends \PHPUnit_Framework_TestCase 25 | { 26 | /** @var Collection */ 27 | protected $fixture = null; 28 | 29 | protected function setUp() 30 | { 31 | $this->fixture = new Collection(); 32 | } 33 | 34 | /** 35 | * Get the pathed name of the test suite's "data" directory. 36 | * 37 | * The path includes a trailing directory separator. 38 | * @return string 39 | */ 40 | protected function getNameOfDataDir() 41 | { 42 | return 43 | __DIR__ 44 | . DIRECTORY_SEPARATOR . '..' 45 | . DIRECTORY_SEPARATOR . '..' 46 | . DIRECTORY_SEPARATOR . '..' 47 | . DIRECTORY_SEPARATOR . 'data' 48 | . DIRECTORY_SEPARATOR 49 | ; 50 | } 51 | 52 | /** 53 | * Get a count of the files that exist in the test suite's "data" directory. 54 | * @return int 55 | */ 56 | protected function getCountOfDataDirFiles() 57 | { 58 | return 2; 59 | } 60 | 61 | /* __construct() ***************************************************/ 62 | 63 | /** @covers \phpDocumentor\Fileset\Collection::__construct() */ 64 | public function testConstructorSucceeds() 65 | { 66 | $this->assertInstanceOf('\phpDocumentor\Fileset\Collection', $this->fixture); 67 | } 68 | 69 | /* setIgnorePatterns() ***************************************************/ 70 | 71 | /** @covers \phpDocumentor\Fileset\Collection::setIgnorePatterns() */ 72 | public function testSetIgnorePatternsAcceptsValidIgnorePattern() 73 | { 74 | $pattern = array('./foo/*'); 75 | $this->fixture->setIgnorePatterns($pattern); 76 | $this->assertInstanceOf( 77 | '\phpDocumentor\Fileset\Collection\IgnorePatterns', 78 | $this->fixture->getIgnorePatterns() 79 | ); 80 | } 81 | 82 | /** @covers \phpDocumentor\Fileset\Collection::setIgnorePatterns() */ 83 | public function testSetIgnorePatternsAcceptsMultipleValidIgnorePatternsGivenAtOnce() 84 | { 85 | $pattern = array('./foo/*', '*/bar/*.txt'); 86 | $this->fixture->setIgnorePatterns($pattern); 87 | $this->assertInstanceOf( 88 | '\phpDocumentor\Fileset\Collection\IgnorePatterns', 89 | $this->fixture->getIgnorePatterns() 90 | ); 91 | } 92 | 93 | /** @covers \phpDocumentor\Fileset\Collection::setIgnorePatterns() */ 94 | public function testSetIgnorePatternsAcceptsMultipleValidIgnorePatternsGivenSeparately() 95 | { 96 | $pattern1 = array('./foo/*'); 97 | $pattern2 = array('*/bar/*.txt'); 98 | $this->fixture->setIgnorePatterns($pattern1); 99 | $this->fixture->setIgnorePatterns($pattern2); 100 | $this->assertInstanceOf( 101 | '\phpDocumentor\Fileset\Collection\IgnorePatterns', 102 | $this->fixture->getIgnorePatterns() 103 | ); 104 | } 105 | 106 | /* getIgnorePatterns() ***************************************************/ 107 | 108 | /** @covers \phpDocumentor\Fileset\Collection::getIgnorePatterns() */ 109 | public function testGetIgnorePatternsAcceptsValidIgnorePattern() 110 | { 111 | $pattern = array('./foo/*'); 112 | $this->fixture->setIgnorePatterns($pattern); 113 | $this->assertInstanceOf( 114 | '\phpDocumentor\Fileset\Collection\IgnorePatterns', 115 | $this->fixture->getIgnorePatterns() 116 | ); 117 | } 118 | 119 | /** @covers \phpDocumentor\Fileset\Collection::getIgnorePatterns() */ 120 | public function testGetIgnorePatternsAcceptsMultipleValidIgnorePatternsGivenAtOnce() 121 | { 122 | $pattern = array('./foo/*', '*/bar/*.txt'); 123 | $this->fixture->setIgnorePatterns($pattern); 124 | $this->assertInstanceOf( 125 | '\phpDocumentor\Fileset\Collection\IgnorePatterns', 126 | $this->fixture->getIgnorePatterns() 127 | ); 128 | } 129 | 130 | /** @covers \phpDocumentor\Fileset\Collection::getIgnorePatterns() */ 131 | public function testGetIgnorePatternsAcceptsMultipleValidIgnorePatternsGivenSeparately() 132 | { 133 | $pattern1 = array('./foo/*'); 134 | $pattern2 = array('*/bar/*.txt'); 135 | $this->fixture->setIgnorePatterns($pattern1); 136 | $this->fixture->setIgnorePatterns($pattern2); 137 | $this->assertInstanceOf( 138 | '\phpDocumentor\Fileset\Collection\IgnorePatterns', 139 | $this->fixture->getIgnorePatterns() 140 | ); 141 | } 142 | 143 | /* setAllowedExtensions() ***************************************************/ 144 | 145 | /** @covers \phpDocumentor\Fileset\Collection::setAllowedExtensions() */ 146 | public function testSetAllowedExtensionsAcceptsEmptyArray() 147 | { 148 | $this->fixture->setAllowedExtensions(array()); 149 | $this->assertTrue(true, 'Test passed'); 150 | } 151 | 152 | /** @covers \phpDocumentor\Fileset\Collection::setAllowedExtensions() */ 153 | public function testSetAllowedExtensionsAcceptsSimpleArray() 154 | { 155 | $this->fixture->setAllowedExtensions(array('php')); 156 | $this->assertTrue(true, 'Test passed'); 157 | } 158 | 159 | /** @covers \phpDocumentor\Fileset\Collection::setAllowedExtensions() */ 160 | public function testSetAllowedExtensionsAcceptsAssociativeArray() 161 | { 162 | $this->fixture->setAllowedExtensions(array('PHP executable file' => 'php')); 163 | $this->assertTrue(true, 'Test passed'); 164 | } 165 | 166 | /** @covers \phpDocumentor\Fileset\Collection::setAllowedExtensions() */ 167 | public function testSetAllowedExtensionsAllowsMultipleCalls() 168 | { 169 | $this->fixture->setAllowedExtensions(array('PHP executable file' => 'php')); 170 | $this->fixture->setAllowedExtensions(array('PHP3 old file' => 'php3')); 171 | $this->assertTrue(true, 'Test passed'); 172 | } 173 | 174 | /* addAllowedExtension() ***************************************************/ 175 | 176 | /** @covers \phpDocumentor\Fileset\Collection::addAllowedExtension() */ 177 | public function testAddAllowedExtensionAcceptsEmptyString() 178 | { 179 | $extension = ''; 180 | $this->fixture->addAllowedExtension($extension); 181 | $this->assertTrue(true, 'Test passed'); 182 | } 183 | 184 | /** @covers \phpDocumentor\Fileset\Collection::addAllowedExtension() */ 185 | public function testAddAllowedExtensionAcceptsSingleString() 186 | { 187 | $extension = 'php'; 188 | $this->fixture->addAllowedExtension($extension); 189 | $this->assertTrue(true, 'Test passed'); 190 | } 191 | 192 | /** @covers \phpDocumentor\Fileset\Collection::addAllowedExtension() */ 193 | public function testAddAllowedExtensionAllowsMultipleCalls() 194 | { 195 | $this->fixture->addAllowedExtension('php'); 196 | $this->fixture->addAllowedExtension('php3'); 197 | $this->assertTrue(true, 'Test passed'); 198 | } 199 | 200 | /* addDirectories() ***************************************************/ 201 | 202 | /** @covers \phpDocumentor\Fileset\Collection::addDirectories() */ 203 | public function testAddDirectoriesWhenGivenEmptyArrayDoesNothing() 204 | { 205 | $this->fixture->addDirectories(array()); 206 | $this->assertTrue(true, 'Test passes'); 207 | } 208 | 209 | /** @covers \phpDocumentor\Fileset\Collection::addDirectories() */ 210 | public function testAddDirectoriesWhenGivenOnePathSucceeds() 211 | { 212 | $this->fixture->addDirectories(array($this->getNameOfDataDir())); 213 | $this->assertTrue(true, 'Test passes'); 214 | } 215 | 216 | /** @covers \phpDocumentor\Fileset\Collection::addDirectories() */ 217 | public function testAddDirectoriesWhenGivenTheSamePathTwiceIsOk() 218 | { 219 | $this->fixture->addDirectories(array($this->getNameOfDataDir())); 220 | $this->fixture->addDirectories(array($this->getNameOfDataDir())); 221 | $this->assertTrue(true, 'Test passes'); 222 | } 223 | 224 | /** @covers \phpDocumentor\Fileset\Collection::addDirectories() */ 225 | public function testAddDirectoriesWhenGivenManyPathsSucceeds() 226 | { 227 | $this->fixture->addDirectories( 228 | array( 229 | $this->getNameOfDataDir(), 230 | $this->getNameOfDataDir() . '.', 231 | $this->getNameOfDataDir() . '..', 232 | )); 233 | $this->assertTrue(true, 'Test passes'); 234 | } 235 | 236 | /** @covers \phpDocumentor\Fileset\Collection::addDirectories() */ 237 | public function testAddDirectoriesWhenGivenTheSamePathTwiceDoesNotResultInDuplicatedFilenames() 238 | { 239 | $this->fixture->setAllowedExtensions(array('phar', 'txt')); 240 | 241 | $this->fixture->addDirectories(array($this->getNameOfDataDir())); 242 | $this->fixture->addDirectories(array($this->getNameOfDataDir())); 243 | 244 | $files = $this->fixture->getFilenames(); 245 | 246 | $this->assertEquals($this->getCountOfDataDirFiles(), count($files)); 247 | } 248 | 249 | /* addDirectory() ***************************************************/ 250 | 251 | /** @covers \phpDocumentor\Fileset\Collection::addDirectory() */ 252 | public function testAddDirectoryCanSeePharContents() 253 | { 254 | // read the phar test fixture 255 | $this->fixture->addDirectory( 256 | 'phar://'. $this->getNameOfDataDir() . 'test.phar' 257 | ); 258 | 259 | // we know which files are in there; test against it 260 | $this->assertEquals( 261 | array( 262 | 'phar://' . $this->getNameOfDataDir() . 'test.phar' . DIRECTORY_SEPARATOR . 'folder' . DIRECTORY_SEPARATOR . 'test.php', 263 | 'phar://' . $this->getNameOfDataDir() . 'test.phar' . DIRECTORY_SEPARATOR . 'test.php', 264 | ), 265 | $this->fixture->getFilenames() 266 | ); 267 | } 268 | 269 | /** @covers \phpDocumentor\Fileset\Collection::addDirectory() */ 270 | public function testAddDirectoryCanSeeDirectoryContents() 271 | { 272 | // load the data test folder... must add non-default extensions first 273 | $this->fixture->setAllowedExtensions(array('phar', 'txt')); 274 | $this->fixture->addDirectory($this->getNameOfDataDir()); 275 | $files = $this->fixture->getFilenames(); 276 | $count = count($files); 277 | 278 | // do a few checks to see if it has caught some cases 279 | $this->assertGreaterThan(0, $count); 280 | $this->assertContains( 281 | realpath($this->getNameOfDataDir() . 'test.phar'), 282 | $files 283 | ); 284 | } 285 | 286 | /** @covers \phpDocumentor\Fileset\Collection::addDirectory() */ 287 | public function testAddDirectoryWhenAllowedExtensionsHidesEverything() 288 | { 289 | // there are no files that match the *default* extensions 290 | $this->fixture->addDirectory($this->getNameOfDataDir()); 291 | $files = $this->fixture->getFilenames(); 292 | 293 | // this file should have been ignored 294 | $this->assertNotContains( 295 | realpath($this->getNameOfDataDir() . 'fileWithText.txt'), 296 | $files 297 | ); 298 | 299 | // this file should also have been ignored 300 | $this->assertNotContains( 301 | realpath($this->getNameOfDataDir() . 'test.phar'), 302 | $files 303 | ); 304 | } 305 | 306 | /** @covers \phpDocumentor\Fileset\Collection::addDirectory() */ 307 | public function testAddDirectoryWhenIgnorePatternHidesEverything() 308 | { 309 | // load the data test folder... must add non-default extensions first 310 | $this->fixture->setAllowedExtensions(array('phar', 'txt')); 311 | 312 | $this->fixture->getIgnorePatterns()->append('(phar|txt)'); 313 | 314 | $this->fixture->addDirectory($this->getNameOfDataDir()); 315 | $files = $this->fixture->getFilenames(); 316 | 317 | // this file should have been ignored 318 | $this->assertNotContains( 319 | realpath($this->getNameOfDataDir() . 'fileWithText.txt'), 320 | $files 321 | ); 322 | 323 | // this file should also have been ignored 324 | $this->assertNotContains( 325 | realpath($this->getNameOfDataDir() . 'test.phar'), 326 | $files 327 | ); 328 | } 329 | 330 | /** @covers \phpDocumentor\Fileset\Collection::addDirectory() */ 331 | public function testAddDirectoryWhenIgnorePatternHidesSomething() 332 | { 333 | // load the data test folder... must add non-default extensions first 334 | $this->fixture->setAllowedExtensions(array('phar', 'txt')); 335 | 336 | $this->fixture->getIgnorePatterns()->append('(phar)'); 337 | 338 | $this->fixture->addDirectory($this->getNameOfDataDir()); 339 | $files = $this->fixture->getFilenames(); 340 | 341 | // this file should have been seen 342 | $this->assertContains( 343 | realpath($this->getNameOfDataDir() . 'fileWithText.txt'), 344 | $files 345 | ); 346 | 347 | // this file should have been ignored 348 | $this->assertNotContains( 349 | realpath($this->getNameOfDataDir() . 'test.phar'), 350 | $files 351 | ); 352 | } 353 | 354 | /** @covers \phpDocumentor\Fileset\Collection::addDirectory() */ 355 | public function testAddDirectoryWhenFollowSymlinksIsSet() 356 | { 357 | /* 358 | * NOTE: 359 | * this test does not verify that any symlinks were followed... 360 | * it simply exercises the $finder->followLinks() line. 361 | */ 362 | $this->fixture->setFollowSymlinks(true); 363 | 364 | // load the data test folder... must add non-default extensions first 365 | $this->fixture->setAllowedExtensions(array('phar', 'txt')); 366 | 367 | $this->fixture->getIgnorePatterns()->append('(phar)'); 368 | 369 | $this->fixture->addDirectory($this->getNameOfDataDir()); 370 | $files = $this->fixture->getFilenames(); 371 | 372 | // this file should have been seen 373 | $this->assertContains( 374 | realpath($this->getNameOfDataDir() . 'fileWithText.txt'), 375 | $files 376 | ); 377 | 378 | // this file should have been ignored 379 | $this->assertNotContains( 380 | realpath($this->getNameOfDataDir() . 'test.phar'), 381 | $files 382 | ); 383 | } 384 | 385 | /* addFiles() ***************************************************/ 386 | 387 | /** @covers \phpDocumentor\Fileset\Collection::addFiles() */ 388 | public function testAddFilesWhenGivenEmptyArrayDoesNothing() 389 | { 390 | $this->fixture->addFiles(array()); 391 | $this->assertTrue(true, 'Test passes'); 392 | } 393 | 394 | /** @covers \phpDocumentor\Fileset\Collection::addFiles() */ 395 | public function testAddFilesWhenGivenOnePathSucceeds() 396 | { 397 | $this->fixture->addFiles(array($this->getNameOfDataDir() . 'test.phar')); 398 | $this->assertTrue(true, 'Test passes'); 399 | } 400 | 401 | /** @covers \phpDocumentor\Fileset\Collection::addFiles() */ 402 | public function testAddFilesWhenGivenTheSamePathTwiceIsOk() 403 | { 404 | $this->fixture->addFiles(array($this->getNameOfDataDir() . 'test.phar')); 405 | $this->fixture->addFiles(array($this->getNameOfDataDir() . 'test.phar')); 406 | $this->assertTrue(true, 'Test passes'); 407 | } 408 | 409 | /** @covers \phpDocumentor\Fileset\Collection::addFiles() */ 410 | public function testAddFilesWhenGivenManyPathsSucceeds() 411 | { 412 | $this->fixture->addFiles( 413 | array( 414 | $this->getNameOfDataDir() . 'test.phar', 415 | $this->getNameOfDataDir() . 'fileWithText.txt', 416 | )); 417 | $this->assertTrue(true, 'Test passes'); 418 | } 419 | 420 | /** @covers \phpDocumentor\Fileset\Collection::addFiles() */ 421 | public function testAddFilesWhenGivenTheSamePathTwiceDoesNotResultInDuplicatedFilenames() 422 | { 423 | $this->fixture->setAllowedExtensions(array('phar', 'txt')); 424 | 425 | $this->fixture->addFiles(array($this->getNameOfDataDir() . 'test.phar')); 426 | $this->fixture->addFiles(array($this->getNameOfDataDir() . 'test.phar')); 427 | 428 | $files = $this->fixture->getFilenames(); 429 | 430 | $this->assertEquals(1, count($files)); 431 | } 432 | 433 | /* addFile() ***************************************************/ 434 | 435 | /** @covers \phpDocumentor\Fileset\Collection::addFile() */ 436 | public function testAddFileWhenGivenEmptyStringThrowsException() 437 | { 438 | $this->setExpectedException( 439 | '\InvalidArgumentException', 440 | 'Expected filename or object of type SplFileInfo but received nothing at all' 441 | ); 442 | $this->fixture->addFile(''); 443 | } 444 | 445 | /** @covers \phpDocumentor\Fileset\Collection::addFile() */ 446 | public function testAddFileWhenGivenOnePathSucceeds() 447 | { 448 | $this->fixture->addFile($this->getNameOfDataDir() . 'test.phar'); 449 | $this->assertTrue(true, 'Test passes'); 450 | } 451 | 452 | /** @covers \phpDocumentor\Fileset\Collection::addFile() */ 453 | public function testAddFileWhenGivenTheSamePathTwiceIsOk() 454 | { 455 | $this->fixture->addFile($this->getNameOfDataDir() . 'test.phar'); 456 | $this->fixture->addFile($this->getNameOfDataDir() . 'test.phar'); 457 | $this->assertTrue(true, 'Test passes'); 458 | } 459 | 460 | /** @covers \phpDocumentor\Fileset\Collection::addFile() */ 461 | public function testAddFileWhenGivenTheSamePathTwiceDoesNotResultInDuplicatedFilenames() 462 | { 463 | $this->fixture->setAllowedExtensions(array('phar', 'txt')); 464 | 465 | $this->fixture->addFile($this->getNameOfDataDir() . 'test.phar'); 466 | $this->fixture->addFile($this->getNameOfDataDir() . 'test.phar'); 467 | 468 | $files = $this->fixture->getFilenames(); 469 | 470 | $this->assertEquals(1, count($files)); 471 | } 472 | 473 | /* getGlobbedPaths() ***************************************************/ 474 | 475 | /** @covers \phpDocumentor\Fileset\Collection::getGlobbedPaths() */ 476 | public function testGetGlobbedPathsWhenGivenEmptyStringThrowsException() 477 | { 478 | $this->setExpectedException( 479 | '\InvalidArgumentException', 480 | 'Expected filename or object of type SplFileInfo but received nothing at all' 481 | ); 482 | $this->fixture->addFile(''); 483 | } 484 | 485 | /** @covers \phpDocumentor\Fileset\Collection::getGlobbedPaths() */ 486 | public function testGetGlobbedPathsWhenGivenOnePathSucceeds() 487 | { 488 | $this->fixture->addFile($this->getNameOfDataDir() . 'test.phar'); 489 | $this->assertTrue(true, 'Test passes'); 490 | } 491 | 492 | /** @covers \phpDocumentor\Fileset\Collection::getGlobbedPaths() */ 493 | public function testGetGlobbedPathsWhenGivenTheSamePathTwiceIsOk() 494 | { 495 | $this->fixture->addFile($this->getNameOfDataDir() . 'test.phar'); 496 | $this->fixture->addFile($this->getNameOfDataDir() . 'test.phar'); 497 | $this->assertTrue(true, 'Test passes'); 498 | } 499 | 500 | /** @covers \phpDocumentor\Fileset\Collection::getGlobbedPaths() */ 501 | public function testGetGlobbedPathsWhenGivenTheSamePathTwiceDoesNotResultInDuplicatedFilenames() 502 | { 503 | $this->fixture->setAllowedExtensions(array('phar', 'txt')); 504 | 505 | $this->fixture->addFile($this->getNameOfDataDir() . 'test.phar'); 506 | $this->fixture->addFile($this->getNameOfDataDir() . 'test.phar'); 507 | 508 | $files = $this->fixture->getFilenames(); 509 | 510 | $this->assertEquals(1, count($files)); 511 | } 512 | 513 | /* getFilenames() ***************************************************/ 514 | 515 | /** @covers \phpDocumentor\Fileset\Collection::getFilenames() */ 516 | public function testGetFilenamesCanSeePharContents() 517 | { 518 | // read the phar test fixture 519 | $this->fixture->addDirectory( 520 | 'phar://'. $this->getNameOfDataDir() . 'test.phar' 521 | ); 522 | 523 | // we know which files are in there; test against it 524 | $this->assertEquals( 525 | array( 526 | 'phar://' . $this->getNameOfDataDir() . 'test.phar' . DIRECTORY_SEPARATOR . 'folder' . DIRECTORY_SEPARATOR . 'test.php', 527 | 'phar://' . $this->getNameOfDataDir() . 'test.phar' . DIRECTORY_SEPARATOR . 'test.php', 528 | ), 529 | $this->fixture->getFilenames() 530 | ); 531 | } 532 | 533 | /** @covers \phpDocumentor\Fileset\Collection::getFilenames() */ 534 | public function testGetFilenamesCanSeeDirectoryContents() 535 | { 536 | // load the data test folder... must add non-default extensions first 537 | $this->fixture->setAllowedExtensions(array('phar', 'txt')); 538 | $this->fixture->addDirectory($this->getNameOfDataDir()); 539 | $files = $this->fixture->getFilenames(); 540 | $count = count($files); 541 | 542 | // do a few checks to see if it has caught some cases 543 | $this->assertGreaterThan(0, $count); 544 | $this->assertContains( 545 | realpath($this->getNameOfDataDir() . 'test.phar'), 546 | $files 547 | ); 548 | } 549 | 550 | /** @covers \phpDocumentor\Fileset\Collection::getFilenames() */ 551 | public function testGetFilenamesWhenIgnorePatternHidesEverything() 552 | { 553 | // load the data test folder... must add non-default extensions first 554 | $this->fixture->setAllowedExtensions(array('phar', 'txt')); 555 | 556 | $this->fixture->getIgnorePatterns()->append('(phar|txt)'); 557 | 558 | $this->fixture->addDirectory($this->getNameOfDataDir()); 559 | $files = $this->fixture->getFilenames(); 560 | 561 | // this file should have been ignored 562 | $this->assertNotContains( 563 | realpath($this->getNameOfDataDir() . 'fileWithText.txt'), 564 | $files 565 | ); 566 | 567 | // this file should also have been ignored 568 | $this->assertNotContains( 569 | realpath($this->getNameOfDataDir() . 'test.phar'), 570 | $files 571 | ); 572 | } 573 | 574 | /** @covers \phpDocumentor\Fileset\Collection::getFilenames() */ 575 | public function testGetFilenamesWhenIgnorePatternHidesSomething() 576 | { 577 | // load the data test folder... must add non-default extensions first 578 | $this->fixture->setAllowedExtensions(array('phar', 'txt')); 579 | 580 | $this->fixture->getIgnorePatterns()->append('(phar)'); 581 | 582 | $this->fixture->addDirectory($this->getNameOfDataDir()); 583 | $files = $this->fixture->getFilenames(); 584 | 585 | // this file should have been seen 586 | $this->assertContains( 587 | realpath($this->getNameOfDataDir() . 'fileWithText.txt'), 588 | $files 589 | ); 590 | 591 | // this file should have been ignored 592 | $this->assertNotContains( 593 | realpath($this->getNameOfDataDir() . 'test.phar'), 594 | $files 595 | ); 596 | } 597 | 598 | 599 | /* getProjectRoot() ***************************************************/ 600 | 601 | /** @covers \phpDocumentor\Fileset\Collection::getProjectRoot() */ 602 | public function testGetProjectRootWhenNothingHasYetBeenAddedReturnsRootSlash() 603 | { 604 | $this->assertEquals(DIRECTORY_SEPARATOR, $this->fixture->getProjectRoot()); 605 | } 606 | 607 | /** @covers \phpDocumentor\Fileset\Collection::getProjectRoot() */ 608 | public function testGetProjectRootWhenOneDatafileWasAddedReturnsDataFolder() 609 | { 610 | $this->fixture->setAllowedExtensions(array('phar', 'txt')); 611 | $this->fixture->addFile($this->getNameOfDataDir() . 'test.phar'); 612 | 613 | // realpath() steals our trailing directory separator 614 | $expected = realpath($this->getNameOfDataDir()) . DIRECTORY_SEPARATOR; 615 | $this->assertEquals($expected, $this->fixture->getProjectRoot()); 616 | } 617 | 618 | /** @covers \phpDocumentor\Fileset\Collection::getProjectRoot() */ 619 | public function testGetProjectRootWhenTwoDatafilesWereAddedReturnsDataFolder() 620 | { 621 | $this->fixture->setAllowedExtensions(array('phar', 'txt')); 622 | $this->fixture->addFile($this->getNameOfDataDir() . 'test.phar'); 623 | $this->fixture->addFile($this->getNameOfDataDir() . 'fileWithText.txt'); 624 | 625 | // realpath() steals our trailing directory separator 626 | $expected = realpath($this->getNameOfDataDir()) . DIRECTORY_SEPARATOR; 627 | $this->assertEquals($expected, $this->fixture->getProjectRoot()); 628 | } 629 | 630 | /** @covers \phpDocumentor\Fileset\Collection::getProjectRoot() */ 631 | public function testGetProjectRootWhenTwoFilesAreApart() 632 | { 633 | $this->fixture->addFile(__FILE__); 634 | $this->fixture->addFile($this->getNameOfDataDir() . 'fileWithText.txt'); 635 | 636 | // realpath() steals our trailing directory separator 637 | $expected = 638 | realpath( 639 | $this->getNameOfDataDir() 640 | . '..' 641 | ) 642 | . DIRECTORY_SEPARATOR 643 | ; 644 | $this->assertEquals($expected, $this->fixture->getProjectRoot()); 645 | } 646 | 647 | /** @covers \phpDocumentor\Fileset\Collection::getProjectRoot() */ 648 | public function testGetProjectRootWhenTwoFilesAreVeryFarApart() 649 | { 650 | $this->fixture->addFile(__FILE__); 651 | $this->fixture->addFile( 652 | $this->getNameOfDataDir() 653 | . '..' . DIRECTORY_SEPARATOR 654 | . '..' . DIRECTORY_SEPARATOR 655 | . 'src' . DIRECTORY_SEPARATOR 656 | . 'phpDocumentor' . DIRECTORY_SEPARATOR 657 | . 'Fileset' . DIRECTORY_SEPARATOR 658 | . 'Collection.php' 659 | ); 660 | 661 | // realpath() steals our trailing directory separator 662 | $expected = 663 | realpath( 664 | $this->getNameOfDataDir() 665 | . '..' . DIRECTORY_SEPARATOR 666 | . '..' . DIRECTORY_SEPARATOR 667 | ) 668 | . DIRECTORY_SEPARATOR 669 | ; 670 | $this->assertEquals($expected, $this->fixture->getProjectRoot()); 671 | } 672 | 673 | /* setIgnoreHidden() ***************************************************/ 674 | 675 | /** @covers \phpDocumentor\Fileset\Collection::setIgnoreHidden() */ 676 | public function testSetIgnoreHiddenGivenFalse() 677 | { 678 | /* 679 | * NOTE: 680 | * this does not verify that hidden files were ignored... 681 | * it simply exercises the setIgnoreHidden() method. 682 | */ 683 | $this->fixture->setIgnoreHidden(false); 684 | $this->assertFalse($this->fixture->getIgnoreHidden()); 685 | } 686 | 687 | /** @covers \phpDocumentor\Fileset\Collection::setIgnoreHidden() */ 688 | public function testSetIgnoreHiddenGivenTrue() 689 | { 690 | /* 691 | * NOTE: 692 | * this does not verify that hidden files were not ignored... 693 | * it simply exercises the setIgnoreHidden() method. 694 | */ 695 | $this->fixture->setIgnoreHidden(true); 696 | $this->assertTrue($this->fixture->getIgnoreHidden()); 697 | } 698 | 699 | /* getIgnoreHidden() ***************************************************/ 700 | 701 | /** @covers \phpDocumentor\Fileset\Collection::getIgnoreHidden() */ 702 | public function testGetIgnoreHiddenGivenFalse() 703 | { 704 | /* 705 | * NOTE: 706 | * this does not verify that hidden files were ignored... 707 | * it simply exercises the getIgnoreHidden() method. 708 | */ 709 | $this->fixture->setIgnoreHidden(false); 710 | $this->assertFalse($this->fixture->getIgnoreHidden()); 711 | } 712 | 713 | /** @covers \phpDocumentor\Fileset\Collection::getIgnoreHidden() */ 714 | public function testGetIgnoreHiddenGivenTrue() 715 | { 716 | /* 717 | * NOTE: 718 | * this does not verify that hidden files were not ignored... 719 | * it simply exercises the getIgnoreHidden() method. 720 | */ 721 | $this->fixture->setIgnoreHidden(true); 722 | $this->assertTrue($this->fixture->getIgnoreHidden()); 723 | } 724 | 725 | /* setFollowSymlinks() ***************************************************/ 726 | 727 | /** @covers \phpDocumentor\Fileset\Collection::setFollowSymlinks() */ 728 | public function testSetFollowSymlinksGivenFalse() 729 | { 730 | /* 731 | * NOTE: 732 | * this does not verify that symlinks were not followed... 733 | * it simply exercises the setFollowSymlinks() method. 734 | */ 735 | $this->fixture->setFollowSymlinks(false); 736 | $this->assertFalse($this->fixture->getFollowSymlinks()); 737 | } 738 | 739 | /** @covers \phpDocumentor\Fileset\Collection::setFollowSymlinks() */ 740 | public function testSetFollowSymlinksGivenTrue() 741 | { 742 | /* 743 | * NOTE: 744 | * this does not verify that symlinks were followed... 745 | * it simply exercises the setFollowSymlinks() method. 746 | */ 747 | $this->fixture->setFollowSymlinks(true); 748 | $this->assertTrue($this->fixture->getFollowSymlinks()); 749 | } 750 | 751 | /* getFollowSymlinks() ***************************************************/ 752 | 753 | /** @covers \phpDocumentor\Fileset\Collection::getFollowSymlinks() */ 754 | public function testGetFollowSymlinksGivenFalse() 755 | { 756 | /* 757 | * NOTE: 758 | * this does not verify that symlinks were not followed... 759 | * it simply exercises the getFollowSymlinks() method. 760 | */ 761 | $this->fixture->setFollowSymlinks(false); 762 | $this->assertFalse($this->fixture->getFollowSymlinks()); 763 | } 764 | 765 | /** @covers \phpDocumentor\Fileset\Collection::getFollowSymlinks() */ 766 | public function testGetFollowSymlinksGivenTrue() 767 | { 768 | /* 769 | * NOTE: 770 | * this does not verify that symlinks were followed... 771 | * it simply exercises the getFollowSymlinks() method. 772 | */ 773 | $this->fixture->setFollowSymlinks(true); 774 | $this->assertTrue($this->fixture->getFollowSymlinks()); 775 | } 776 | } 777 | -------------------------------------------------------------------------------- /tests/unit/phpDocumentor/Fileset/FileTest.php: -------------------------------------------------------------------------------- 1 | 8 | * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com) 9 | * @license http://www.opensource.org/licenses/mit-license.php MIT 10 | * @link http://phpdoc.org 11 | */ 12 | 13 | namespace phpDocumentor\Fileset; 14 | 15 | /** 16 | * Test case for File class. 17 | * 18 | * @author Mike van Riel 19 | * @license http://www.opensource.org/licenses/mit-license.php MIT 20 | * @link http://phpdoc.org 21 | */ 22 | class FileTest extends \PHPUnit_Framework_TestCase 23 | { 24 | /** 25 | * Get the pathed name of the test suite's "data" directory. 26 | * 27 | * The path includes a trailing directory separator. 28 | * @return string 29 | */ 30 | protected function getNameOfDataDir() 31 | { 32 | return 33 | __DIR__ 34 | . DIRECTORY_SEPARATOR . '..' 35 | . DIRECTORY_SEPARATOR . '..' 36 | . DIRECTORY_SEPARATOR . '..' 37 | . DIRECTORY_SEPARATOR . 'data' 38 | . DIRECTORY_SEPARATOR 39 | ; 40 | } 41 | 42 | /* __construct() ******************************/ 43 | 44 | /** @covers \phpDocumentor\Fileset\File::__construct() */ 45 | public function testConstructorWithEmptyArgThrowsException() 46 | { 47 | $this->setExpectedException( 48 | '\InvalidArgumentException', 49 | 'Expected filename or object of type SplFileInfo but received nothing at all' 50 | ); 51 | $file = new File(''); 52 | } 53 | 54 | /** @covers \phpDocumentor\Fileset\File::__construct() */ 55 | public function testConstructorWithUnusableObjectThrowsException() 56 | { 57 | $this->setExpectedException( 58 | '\InvalidArgumentException', 59 | 'Expected filename or object of type SplFileInfo but received stdClass' 60 | ); 61 | $file = new File(new \stdClass()); 62 | } 63 | 64 | /** @covers \phpDocumentor\Fileset\File::__construct() */ 65 | public function testConstructorWithEmptySplFileInfoObjectThrowsException() 66 | { 67 | $this->setExpectedException( 68 | '\InvalidArgumentException', 69 | 'Expected filename or object of type SplFileInfo but received nothing at all' 70 | ); 71 | $file = new File(new \SplFileInfo('')); 72 | } 73 | 74 | /** @covers \phpDocumentor\Fileset\File::__construct() */ 75 | public function testConstructorWithValidSplFileInfoObjectSucceeds() 76 | { 77 | $file = new File(new \SplFileInfo('foo.txt')); 78 | $this->assertInstanceOf('\phpDocumentor\Fileset\File', $file); 79 | } 80 | 81 | /* getMimeType() ******************************/ 82 | 83 | /** @covers \phpDocumentor\Fileset\File::getMimeType() */ 84 | public function testGetMimeTypeWhenFileinfoNotLoadedThrowsException() 85 | { 86 | if (true === extension_loaded('fileinfo')) { 87 | $this->markTestSkipped('Does not apply if fileinfo is loaded.'); 88 | } 89 | $file = new File(new \SplFileInfo('foo.txt')); 90 | $this->setExpectedException( 91 | '\RuntimeException', 92 | 'The finfo extension or mime_content_type function are needed to determine the Mime Type for this file.' 93 | ); 94 | $file->getMimeType(); 95 | } 96 | 97 | /** @covers \phpDocumentor\Fileset\File::getMimeType() */ 98 | public function testGetMimeTypeWithEmptySplFileInfoObjectThrowsException() 99 | { 100 | if (false === extension_loaded('fileinfo')) { 101 | $this->markTestSkipped('Does not apply if fileinfo is not loaded.'); 102 | } 103 | $file = new File(new \SplFileInfo('foo.txt')); 104 | $this->setExpectedException( 105 | '\RuntimeException', 106 | 'Failed to read file info via finfo' 107 | ); 108 | $file->getMimeType(); 109 | } 110 | 111 | /** @covers \phpDocumentor\Fileset\File::getMimeType() */ 112 | public function testGetMimeTypeWithValidSplFileInfoObjectOfTestTextFile() 113 | { 114 | if (false === extension_loaded('fileinfo')) { 115 | $this->markTestSkipped('Does not apply if fileinfo is not loaded.'); 116 | } 117 | $file = new File(new \SplFileInfo($this->getNameOfDataDir() . 'fileWithText.txt')); 118 | $this->assertEquals('text/plain', $file->getMimeType()); 119 | } 120 | 121 | /* fread() ******************************/ 122 | 123 | /** @covers \phpDocumentor\Fileset\File::fread() */ 124 | public function testFreadWithEmptySplFileInfoObjectThrowsException() 125 | { 126 | $file = new File(new \SplFileInfo('foo.txt')); 127 | $this->setExpectedException( 128 | '\RuntimeException', 129 | 'Unable to open file' 130 | ); 131 | $file->fread(); 132 | } 133 | 134 | /** @covers \phpDocumentor\Fileset\File::fread() */ 135 | public function testFreadWithValidSplFileInfoObjectOfTestTextFile() 136 | { 137 | $file = new File(new \SplFileInfo($this->getNameOfDataDir() . 'fileWithText.txt')); 138 | $expected = <<fread(); 143 | $this->assertEquals($expected, $actual); 144 | } 145 | } 146 | --------------------------------------------------------------------------------