Arbit - project tracking

PHP Depend

Browse source code

File: / tests/ PHP/ Depend/ AbstractTest.php

Type
text/plain text/plain
Last Author
mapi
Version
1184
Line Rev. Author Source
1 18 mapi <?php
2 mapi /**
3 mapi * This file is part of PHP_Depend.
4 535 mapi *
5 18 mapi * PHP Version 5
6 mapi *
7 1030 mapi * Copyright (c) 2008-2010, Manuel Pichler <mapi@pdepend.org>.
8 18 mapi * All rights reserved.
9 mapi *
10 mapi * Redistribution and use in source and binary forms, with or without
11 mapi * modification, are permitted provided that the following conditions
12 mapi * are met:
13 mapi *
14 mapi * * Redistributions of source code must retain the above copyright
15 mapi * notice, this list of conditions and the following disclaimer.
16 mapi *
17 mapi * * Redistributions in binary form must reproduce the above copyright
18 mapi * notice, this list of conditions and the following disclaimer in
19 mapi * the documentation and/or other materials provided with the
20 mapi * distribution.
21 mapi *
22 mapi * * Neither the name of Manuel Pichler nor the names of his
23 mapi * contributors may be used to endorse or promote products derived
24 mapi * from this software without specific prior written permission.
25 mapi *
26 mapi * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 mapi * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 mapi * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 mapi * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30 mapi * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 mapi * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 mapi * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 mapi * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 mapi * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 mapi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36 mapi * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 mapi * POSSIBILITY OF SUCH DAMAGE.
38 mapi *
39 mapi * @category QualityAssurance
40 mapi * @package PHP_Depend
41 269 mapi * @author Manuel Pichler <mapi@pdepend.org>
42 1030 mapi * @copyright 2008-2010 Manuel Pichler. All rights reserved.
43 18 mapi * @license http://www.opensource.org/licenses/bsd-license.php BSD License
44 mapi * @version SVN: $Id: AbstractTest.php 1184 2010-03-18 11:42:20Z mapi $
45 823 mapi * @link http://pdepend.org/
46 18 mapi */
47 mapi
48 mapi require_once 'PHPUnit/Framework/TestCase.php';
49 mapi
50 mapi /**
51 mapi * Abstract test case implementation for the PHP_Depend package.
52 mapi *
53 mapi * @category QualityAssurance
54 mapi * @package PHP_Depend
55 269 mapi * @author Manuel Pichler <mapi@pdepend.org>
56 1030 mapi * @copyright 2008-2010 Manuel Pichler. All rights reserved.
57 18 mapi * @license http://www.opensource.org/licenses/bsd-license.php BSD License
58 mapi * @version Release: @package_version@
59 823 mapi * @link http://pdepend.org/
60 18 mapi */
61 mapi class PHP_Depend_AbstractTest extends PHPUnit_Framework_TestCase
62 mapi {
63 mapi /**
64 535 mapi * Removes temporary test contents.
65 mapi *
66 mapi * @return void
67 mapi */
68 mapi protected function setUp()
69 mapi {
70 mapi parent::setUp();
71 mapi
72 1051 mapi $run = dirname(__FILE__) . '/_run';
73 mapi if (file_exists($run) === false) {
74 mapi mkdir($run, 0755);
75 mapi }
76 535 mapi
77 1109 mapi $this->_clearRunResources($run);
78 1051 mapi
79 914 mapi include_once 'PHP/Depend.php';
80 mapi include_once 'PHP/Depend/StorageRegistry.php';
81 601 mapi include_once 'PHP/Depend/Storage/MemoryEngine.php';
82 mapi
83 mapi PHP_Depend_StorageRegistry::set(
84 mapi PHP_Depend::TOKEN_STORAGE,
85 mapi new PHP_Depend_Storage_MemoryEngine()
86 mapi );
87 606 mapi PHP_Depend_StorageRegistry::set(
88 mapi PHP_Depend::PARSER_STORAGE,
89 mapi new PHP_Depend_Storage_MemoryEngine()
90 mapi );
91 1005 mapi
92 mapi if (defined('STDERR') === false) {
93 mapi define('STDERR', fopen('php://stderr', true));
94 mapi }
95 535 mapi }
96 mapi
97 mapi /**
98 219 mapi * Resets the global iterator filter.
99 mapi *
100 mapi * @return void
101 mapi */
102 mapi protected function tearDown()
103 mapi {
104 1184 mapi PHP_Depend_Code_Filter_Collection::getInstance()->setFilter();
105 535 mapi
106 1109 mapi $this->_clearRunResources();
107 mapi
108 219 mapi parent::tearDown();
109 mapi }
110 535 mapi
111 219 mapi /**
112 1109 mapi * Clears all temporary resources.
113 mapi *
114 mapi * @param string $dir The root directory.
115 mapi *
116 mapi * @return void
117 mapi */
118 mapi private function _clearRunResources($dir = null)
119 mapi {
120 mapi if ($dir === null) {
121 mapi $dir = dirname(__FILE__) . '/_run';
122 mapi }
123 mapi
124 mapi foreach (new DirectoryIterator($dir) as $file) {
125 mapi if ($file == '.' || $file == '..' || $file == '.svn') {
126 mapi continue;
127 mapi }
128 mapi $pathName = realpath($file->getPathname());
129 mapi if ($file->isDir()) {
130 mapi $this->_clearRunResources($pathName);
131 mapi rmdir($pathName);
132 mapi } else {
133 mapi unlink($pathName);
134 mapi }
135 mapi }
136 mapi }
137 mapi
138 mapi /**
139 616 mapi * Creates a temporary resource for the given file name.
140 535 mapi *
141 616 mapi * @param string $fileName The temporary file name.
142 535 mapi *
143 mapi * @return string
144 mapi */
145 616 mapi protected static function createRunResourceURI($fileName)
146 535 mapi {
147 616 mapi $uri = dirname(__FILE__) . '/_run/' . $fileName;
148 mapi if (file_exists($uri) === true) {
149 mapi throw new ErrorException("File '{$fileName}' already exists.");
150 mapi }
151 mapi return $uri;
152 535 mapi }
153 mapi
154 mapi /**
155 699 mapi * Creates a code uri for the given file name.
156 mapi *
157 mapi * @param string $fileName The code file name.
158 mapi *
159 mapi * @return string
160 mapi */
161 mapi protected static function createCodeResourceURI($fileName)
162 mapi {
163 mapi $uri = dirname(__FILE__) . '/_code/' . $fileName;
164 mapi if (file_exists($uri) === false) {
165 mapi throw new ErrorException("File '{$fileName}' does not exists.");
166 mapi }
167 mapi return $uri;
168 mapi }
169 mapi
170 mapi /**
171 18 mapi * Initializes the test environment.
172 mapi *
173 mapi * @return void
174 mapi */
175 mapi public static function init()
176 mapi {
177 630 mapi
178 18 mapi // Is it not installed?
179 214 mapi if (is_file(dirname(__FILE__) . '/../../../PHP/Depend.php')) {
180 535 mapi
181 214 mapi $path = realpath(dirname(__FILE__) . '/../../..');
182 mapi $path .= PATH_SEPARATOR . get_include_path();
183 18 mapi set_include_path($path);
184 535 mapi
185 214 mapi $whitelist = realpath(dirname(__FILE__) . '/../../../PHP') . '/';
186 19 mapi PHPUnit_Util_Filter::addDirectoryToWhitelist($whitelist);
187 18 mapi }
188 535 mapi
189 214 mapi // Set test path
190 mapi $path = realpath(dirname(__FILE__) . '/../..') ;
191 mapi $path .= PATH_SEPARATOR . get_include_path();
192 mapi set_include_path($path);
193 535 mapi
194 574 mapi include_once 'PHP/Depend/Code/Filter/Collection.php';
195 632 mapi
196 mapi self::initVersionCompatibility();
197 18 mapi }
198 535 mapi
199 211 mapi /**
200 631 mapi * There was an api change between PHP 5.3.0alpha3 and 5.3.0beta1, the new
201 mapi * extension name "Core" was introduced and interfaces like "Iterator" are
202 mapi * now part of "Core" instead of "Standard".
203 mapi *
204 mapi * @return void
205 mapi */
206 mapi private static function initVersionCompatibility()
207 mapi {
208 mapi $reflection = new ReflectionClass('Iterator');
209 mapi $extension = strtolower($reflection->getExtensionName());
210 632 mapi $extension = ($extension === '' ? 'standard' : $extension);
211 631 mapi
212 1005 mapi if (defined('CORE_PACKAGE') === false ) {
213 mapi define('CORE_PACKAGE', '+' . $extension);
214 mapi }
215 631 mapi }
216 mapi
217 mapi /**
218 211 mapi * Parses the given source file or directory with the default tokenizer
219 mapi * and node builder implementations.
220 mapi *
221 839 mapi * @param string $testCase Qualified name of the test case.
222 mapi * @param boolean $ignoreAnnotations The parser should ignore annotations.
223 mapi *
224 mapi * @return PHP_Depend_Code_NodeIterator
225 mapi */
226 mapi public static function parseTestCaseSource($testCase, $ignoreAnnotations = false)
227 mapi {
228 mapi list($class, $method) = explode('::', $testCase);
229 mapi
230 mapi $fileName = substr(strtolower($class), 11, strrpos($class, '_') - 11);
231 mapi $fileName = str_replace('_', '/', $fileName) . '/' . $method;
232 mapi
233 mapi try {
234 mapi $fileOrDirectory = self::createCodeResourceURI($fileName);
235 mapi } catch (ErrorException $e) {
236 mapi $fileOrDirectory = self::createCodeResourceURI($fileName . '.php');
237 mapi }
238 mapi
239 mapi return self::parseSource($fileOrDirectory, $ignoreAnnotations);
240 mapi }
241 mapi
242 mapi /**
243 mapi * Parses the given source file or directory with the default tokenizer
244 mapi * and node builder implementations.
245 mapi *
246 231 mapi * @param string $fileOrDirectory A source file or a source directory.
247 mapi * @param boolean $ignoreAnnotations The parser should ignore annotations.
248 535 mapi *
249 211 mapi * @return PHP_Depend_Code_NodeIterator
250 mapi */
251 231 mapi public static function parseSource($fileOrDirectory, $ignoreAnnotations = false)
252 211 mapi {
253 233 mapi include_once 'PHP/Depend/Parser.php';
254 559 mapi include_once 'PHP/Depend/Builder/Default.php';
255 574 mapi include_once 'PHP/Depend/Code/Filter/Collection.php';
256 560 mapi include_once 'PHP/Depend/Tokenizer/Internal.php';
257 609 mapi include_once 'PHP/Depend/Input/ExcludePathFilter.php';
258 mapi include_once 'PHP/Depend/Input/Iterator.php';
259 535 mapi
260 649 mapi if (file_exists($fileOrDirectory) === false) {
261 699 mapi $fileOrDirectory = self::createCodeResourceURI($fileOrDirectory);
262 649 mapi }
263 mapi
264 211 mapi if (is_dir($fileOrDirectory)) {
265 609 mapi $it = new PHP_Depend_Input_Iterator(
266 211 mapi new RecursiveIteratorIterator(
267 mapi new RecursiveDirectoryIterator($fileOrDirectory)
268 mapi ),
269 609 mapi new PHP_Depend_Input_ExcludePathFilter(array('.svn'))
270 211 mapi );
271 mapi } else {
272 mapi $it = new ArrayIterator(array($fileOrDirectory));
273 mapi }
274 535 mapi
275 1029 mapi $files = array();
276 mapi foreach ($it as $file) {
277 mapi if (is_object($file)) {
278 mapi $files[] = realpath($file->getPathname());
279 mapi } else {
280 mapi $files[] = $file;
281 mapi }
282 mapi }
283 mapi sort($files);
284 mapi
285 559 mapi $builder = new PHP_Depend_Builder_Default();
286 535 mapi
287 1029 mapi foreach ($files as $file) {
288 606 mapi $tokenizer = new PHP_Depend_Tokenizer_Internal();
289 mapi $tokenizer->setSourceFile($file);
290 535 mapi
291 231 mapi $parser = new PHP_Depend_Parser($tokenizer, $builder);
292 mapi if ($ignoreAnnotations === true) {
293 mapi $parser->setIgnoreAnnotations();
294 mapi }
295 211 mapi
296 mapi $parser->parse();
297 mapi }
298 mapi return $builder->getPackages();
299 mapi }
300 18 mapi }
301 mapi
302 1005 mapi PHP_Depend_AbstractTest::init();