PageRenderTime 42ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/phpunit/NetBeansSuite.php

https://github.com/jkonieczny/netbeans-test
PHP | 144 lines | 39 code | 6 blank | 99 comment | 3 complexity | 2bbc84fef022788bf2a0b74b20e2932e MD5 | raw file
  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. * Generic test suite containing tests based on the provided CLI parameters,
  45. * see {@link NetBeansSuite::toRun()} for more information.
  46. *
  47. * For directory:<br/>
  48. * Recursively scans the test-directory and it's
  49. * sub-directories. All found unit-tests will be
  50. * added and executed.
  51. *
  52. * For file:<br/>
  53. * Just the file is added.
  54. *
  55. * To run this suite from CLI: phpunit NetBeansSuite.php run=<file-or-directory>
  56. *
  57. * <b>WARNING: User changes to this file should be avoided.</b>
  58. *
  59. * @package NetBeans
  60. */
  61. class NetBeansSuite extends PHPUnit_Framework_TestSuite {
  62. /**
  63. * The name of the parameter followed by equals sign ("=") of the file or directory to be run by PHPUnit.
  64. * @see toRun()
  65. */
  66. const RUN = "run=";
  67. /**
  68. * Suite factory.
  69. *
  70. * This function creates a PHPUnit test-suite,
  71. * scans the directory for test-cases,
  72. * adds all test-cases found and then returns
  73. * a test-suite containing all available tests.
  74. *
  75. * @access public
  76. * @static
  77. * @return NetBeansSuite
  78. */
  79. public static function suite() {
  80. $suite = new NetBeansSuite();
  81. foreach (self::toRun() as $file) {
  82. $suite->addTestFile($file);
  83. }
  84. return $suite;
  85. }
  86. /**
  87. * Tries to find {@link #RUN) in CLI parameters and returns array of files to be runj by PHPUnit
  88. * or throws Exception if no such parameter found or directory/file does not exist.
  89. *
  90. * @access private
  91. * @static
  92. *
  93. * @return array an array of files to be run by PHPUnit
  94. * @see RUN
  95. */
  96. private static function toRun() {
  97. $argv = isset($_SERVER['argv']) ? $_SERVER['argv'] : array();
  98. $run = null;
  99. foreach ($argv as $arg) {
  100. if (preg_match("/^".self::RUN."(.+)/", $arg, $sub)) {
  101. $run = $sub[1];
  102. break;
  103. }
  104. }
  105. if ($run === null) {
  106. throw new Exception("No argument to run found.");
  107. }
  108. if (is_dir($run)) {
  109. return self::rglob("*[Tt]est.php", $run.DIRECTORY_SEPARATOR);
  110. } elseif (is_file($run)) {
  111. return array($run);
  112. }
  113. throw new Exception(sprintf("Argument '%s' neither file nor directory.", $run));
  114. }
  115. /**
  116. * Recursive {@link http://php.net/manual/en/function.glob.php glob()}.
  117. *
  118. * @access private
  119. * @static
  120. *
  121. * @param string $pattern the pattern passed to glob(), default is "*"
  122. * @param string $path the path to scan, default is
  123. * {@link http://php.net/manual/en/function.getcwd.php the current working directory}
  124. * @param int $flags the flags passed to glob()
  125. * @return array an array of files in the given path matching the pattern.
  126. * @link http://php.net/manual/en/function.glob.php
  127. * @link http://php.net/manual/en/function.getcwd.php
  128. */
  129. private static function rglob($pattern = '*', $path = '', $flags = 0) {
  130. $paths = glob($path.'*', GLOB_MARK | GLOB_ONLYDIR | GLOB_NOSORT) or array();
  131. $files = glob($path.$pattern, $flags) or array();
  132. foreach ($paths as $path) {
  133. $files = array_merge($files, self::rglob($pattern, $path, $flags));
  134. }
  135. return $files;
  136. }
  137. }
  138. ?>