Advertisement
Guest User

NetBeansSuite.php

a guest
Feb 6th, 2013
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.44 KB | None | 0 0
  1. <?php
  2. /*
  3.  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  4.  *
  5.  * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
  6.  *
  7.  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  8.  * Other names may be trademarks of their respective owners.
  9.  *
  10.  * The contents of this file are subject to the terms of either the GNU
  11.  * General Public License Version 2 only ("GPL") or the Common
  12.  * Development and Distribution License("CDDL") (collectively, the
  13.  * "License"). You may not use this file except in compliance with the
  14.  * License. You can obtain a copy of the License at
  15.  * http://www.netbeans.org/cddl-gplv2.html
  16.  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  17.  * specific language governing permissions and limitations under the
  18.  * License.  When distributing the software, include this License Header
  19.  * Notice in each file and include the License file at
  20.  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
  21.  * particular file as subject to the "Classpath" exception as provided
  22.  * by Oracle in the GPL Version 2 section of the License file that
  23.  * accompanied this code. If applicable, add the following below the
  24.  * License Header, with the fields enclosed by brackets [] replaced by
  25.  * your own identifying information:
  26.  * "Portions Copyrighted [year] [name of copyright owner]"
  27.  *
  28.  * If you wish your version of this file to be governed by only the CDDL
  29.  * or only the GPL Version 2, indicate your decision by adding
  30.  * "[Contributor] elects to include this software in this distribution
  31.  * under the [CDDL or GPL Version 2] license." If you do not indicate a
  32.  * single choice of license, a recipient has the option to distribute
  33.  * your version of this file under either the CDDL, the GPL Version 2 or
  34.  * to extend the choice of license to its licensees as provided above.
  35.  * However, if you add GPL Version 2 code and therefore, elected the GPL
  36.  * Version 2 license, then the option applies only if the new code is
  37.  * made subject to such option by the copyright holder.
  38.  *
  39.  * Contributor(s):
  40.  *
  41.  * Portions Copyrighted 2009 Sun Microsystems, Inc.
  42.  */
  43.  
  44.  
  45. /**
  46.  * Generic test suite containing tests based on the provided CLI parameters,
  47.  * see {@link NetBeansSuite::toRun()} for more information.
  48.  *
  49.  * For directory:<br/>
  50.  * Recursively scans the test-directory and it's
  51.  * sub-directories. All found unit-tests will be
  52.  * added and executed.
  53.  *
  54.  * For file:<br/>
  55.  * Just the file is added.
  56.  *
  57.  * To run this suite from CLI: phpunit NetBeansSuite.php run=<file-or-directory>
  58.  *
  59.  * <b>WARNING: User changes to this file should be avoided.</b>
  60.  *
  61.  * @package NetBeans
  62.  */
  63. class NetBeansSuite extends PHPUnit_Framework_TestSuite {
  64.     /**
  65.      * The name of the parameter followed by equals sign ("=") of the file or directory to be run by PHPUnit.
  66.      * @see toRun()
  67.      */
  68.     const RUN = "run=";
  69.  
  70.     /**
  71.      * Suite factory.
  72.      *
  73.      * This function creates a PHPUnit test-suite,
  74.      * scans the directory for test-cases,
  75.      * adds all test-cases found and then returns
  76.      * a test-suite containing all available tests.
  77.      *
  78.      * @access public
  79.      * @static
  80.      * @return NetBeansSuite
  81.      */
  82.     public static function suite() {
  83.         $suite = new NetBeansSuite();
  84.         foreach (self::toRun() as $file) {
  85.             $suite->addTestFile($file);
  86.         }
  87.         return $suite;
  88.     }
  89.  
  90.     /**
  91.      * Tries to find {@link #RUN) in CLI parameters and returns array of files to be runj by PHPUnit
  92.      * or throws Exception if no such parameter found or directory/file does not exist.
  93.      *
  94.      * @access private
  95.      * @static
  96.      *
  97.      * @return array an array of files to be run by PHPUnit
  98.      * @see RUN
  99.      */
  100.     private static function toRun() {
  101.         $argv = isset($_SERVER['argv']) ? $_SERVER['argv'] : array();
  102.         $run = null;
  103.         foreach ($argv as $arg) {
  104.             if (preg_match("/^\"?".self::RUN."(.+?)\"?$/", $arg, $sub)) {
  105.                 $run = $sub[1];
  106.                 break;
  107.             }
  108.         }
  109.         if ($run === null) {
  110.             throw new Exception("No argument to run found.");
  111.         }
  112.         if (is_dir($run)) {
  113.             return self::rglob("*[Tt]est.php", $run.DIRECTORY_SEPARATOR);
  114.         } elseif (is_file($run)) {
  115.             return array($run);
  116.         }
  117.         throw new Exception(sprintf("Argument '%s' neither file nor directory.", $run));
  118.     }
  119.  
  120.     /**
  121.      * Recursive {@link http://php.net/manual/en/function.glob.php glob()}.
  122.      *
  123.      * @access private
  124.      * @static
  125.      *
  126.      * @param  string $pattern the pattern passed to glob(), default is "*"
  127.      * @param  string $path    the path to scan, default is
  128.      *                         {@link http://php.net/manual/en/function.getcwd.php the current working directory}
  129.      * @param  int    $flags   the flags passed to glob()
  130.      * @return array  an array of files in the given path matching the pattern.
  131.      * @link http://php.net/manual/en/function.glob.php
  132.      * @link http://php.net/manual/en/function.getcwd.php
  133.      */
  134.     private static function rglob($pattern = '*', $path = '', $flags = 0) {
  135.         $paths = glob($path.'*', GLOB_MARK | GLOB_ONLYDIR | GLOB_NOSORT) or array();
  136.         $files = glob($path.$pattern, $flags) or array();
  137.         foreach ($paths as $path) {
  138.             $files = array_merge($files, self::rglob($pattern, $path, $flags));
  139.         }
  140.         return $files;
  141.     }
  142. }
  143.  
  144. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement