/tests/testClasses/TestFiles.php

https://github.com/orchestra-io/sample-openx · PHP · 142 lines · 65 code · 8 blank · 69 comment · 16 complexity · 27fc220a6d7868445c1d319bfbe6cf70 MD5 · raw file

  1. <?php
  2. /*
  3. +---------------------------------------------------------------------------+
  4. | OpenX v${RELEASE_MAJOR_MINOR} |
  5. | =======${RELEASE_MAJOR_MINOR_DOUBLE_UNDERLINE} |
  6. | |
  7. | Copyright (c) 2003-2009 OpenX Limited |
  8. | For contact details, see: http://www.openx.org/ |
  9. | |
  10. | This program is free software; you can redistribute it and/or modify |
  11. | it under the terms of the GNU General Public License as published by |
  12. | the Free Software Foundation; either version 2 of the License, or |
  13. | (at your option) any later version. |
  14. | |
  15. | This program is distributed in the hope that it will be useful, |
  16. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  17. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  18. | GNU General Public License for more details. |
  19. | |
  20. | You should have received a copy of the GNU General Public License |
  21. | along with this program; if not, write to the Free Software |
  22. | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  23. +---------------------------------------------------------------------------+
  24. $Id: TestFiles.php 38974 2009-06-29 13:20:41Z david.keen $
  25. */
  26. /**
  27. */
  28. /**
  29. * A class for locating test files in different ways.
  30. *
  31. * @package Max
  32. * @subpackage TestSuite
  33. * @author Andrew Hill <andrew@m3.net>
  34. */
  35. class TestFiles
  36. {
  37. /**
  38. * A method to scan a directory (and, optionally, all sub-directories)
  39. * and find all OpenX tests of the appropriate type and relating to
  40. * a supplied test "layer" code.
  41. *
  42. * @param string $type The type of test being run (eg. "unit").
  43. * @param string $code The layer's code (eg. "dal").
  44. * @param string $dir Base directory name to begin search from.
  45. * @param boolean $recursive Optional. If true, searches sub-folders
  46. * recursively.
  47. * @return array An array containing all of the files found that
  48. * match the layer test code supplied.
  49. */
  50. function getTestFiles($type, $code, $dir, $recursive = true)
  51. {
  52. $aFiles = array();
  53. // Search recursively?
  54. if ($recursive) {
  55. // Open the base directory
  56. $dh = opendir($dir);
  57. if ($dh) {
  58. while (false !== ($file = readdir($dh))) {
  59. // Ignore the parent, self and subversion directories
  60. if (($file == '.') || ($file == '..') || ($file == '.svn')) {
  61. continue;
  62. }
  63. // Is the file another directory?
  64. if (is_dir($dir . '/' . $file)) {
  65. // In recursive mode, so add in all tests found in this sub-directory
  66. $aFiles = array_merge($aFiles, TestFiles::getTestFiles($type, $code, $dir . '/' . $file));
  67. }
  68. }
  69. closedir($dh);
  70. }
  71. }
  72. // Can we open a tests directory?
  73. $dh = @opendir($dir . '/' . constant($type . '_TEST_STORE'));
  74. if ($dh) {
  75. while (($file = readdir($dh)) !== false) {
  76. // Does the filename match?
  77. if (preg_match("/[^.]+\.$code\.test\.php/", $file)) {
  78. // Strip the MAX_PROJECT_PATH from the folder before storing
  79. $storeFolder = preg_replace('#' . str_replace('\\', '\\\\', MAX_PROJECT_PATH) . '/#', '', $dir);
  80. $aFiles[$storeFolder][] = $file;
  81. }
  82. }
  83. closedir($dh);
  84. if (count($aFiles[$storeFolder]) > 1) {
  85. asort($aFiles[$storeFolder]);
  86. }
  87. }
  88. return $aFiles;
  89. }
  90. /**
  91. * A method to get all test files in the OpenX project.
  92. *
  93. * @param string $type The type of test being run (eg. "unit").
  94. * @return array An array containing the details of all the test files
  95. * in the OpenX project.
  96. */
  97. function getAllTestFiles($type)
  98. {
  99. global $conf;
  100. $aDirectories = explode('|', $conf['test']['directories']);
  101. $aTests = array();
  102. foreach ($GLOBALS['_MAX']['TEST'][$type . '_layers'] as $layer => $data) {
  103. foreach ($aDirectories as $path) {
  104. if (empty($aTests[$layer])) {
  105. $aTests[$layer] = array();
  106. }
  107. $aTests[$layer] = array_merge($aTests[$layer], TestFiles::getTestFiles($type, $layer, MAX_PROJECT_PATH.'/'.$path));
  108. }
  109. }
  110. return $aTests;
  111. }
  112. /**
  113. * A method to get all test files in the OpenX project for a specified layer.
  114. *
  115. * @param string $type The type of test being run (eg. "unit").
  116. * @param $layer string The layer code.
  117. * @return mixed An array containing the details of all the test files
  118. * in the OpenX project for the specified layer.
  119. */
  120. function getLayerTestFiles($type, $layer)
  121. {
  122. global $conf;
  123. $aDirectories = explode('|', $conf['test']['directories']);
  124. $aTests = array();
  125. foreach ($aDirectories as $path) {
  126. if (empty($aTests[$layer])) {
  127. $aTests[$layer] = array();
  128. }
  129. $aTests[$layer] = array_merge($aTests[$layer], TestFiles::getTestFiles($type, $layer, MAX_PROJECT_PATH.'/'.$path));
  130. }
  131. return $aTests;
  132. }
  133. }
  134. ?>