PageRenderTime 62ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/unit_test.php

http://68kb.googlecode.com/
PHP | 122 lines | 81 code | 21 blank | 20 comment | 10 complexity | d551c6f29f8ef0404925deefee028f45 MD5 | raw file
Possible License(s): CC0-1.0
  1. <?php
  2. /*
  3. SimpleTest + CodeIgniter
  4. test.php
  5. the test runner - loads all needed files,
  6. integrates with CodeIgniter and runs the tests
  7. by Jamie Rumbelow
  8. http://jamierumbelow.net/
  9. */
  10. //Configure and load files
  11. define('ROOT', dirname(__FILE__) . '/');
  12. $base_dir = str_replace('upload/', 'do_not_upload/', ROOT);
  13. $utility_dir = "/usr/local/Zend/apache2/htdocs/utilities/";
  14. ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.$utility_dir); // For Utilities
  15. //define('APP_DIR', ROOT.'includes/application');
  16. define('APP_DIR', $base_dir);
  17. require_once 'simpletest/unit_tester.php';
  18. require_once 'simpletest/web_tester.php';
  19. require_once 'simpletest/reporter.php';
  20. function add_test($file, &$test) {
  21. $implementation = '';
  22. if (preg_match('/_controller/', $file)) {
  23. $controller = preg_replace('#' . APP_DIR . '/tests/controllers/([a-zA-Z0-9_\-])_controller_test.php#', '$1', $file);
  24. $implementation = APP_DIR . '/controllers'.$controller.'.php';
  25. } elseif (preg_match('/_model/', $file)) {
  26. $model = preg_replace('#' . APP_DIR . '/tests/models/([a-zA-Z0-9_\-])_model_test.php#', '$1', $file);
  27. $implementation = APP_DIR . '/models/'.$model.'_model.php';
  28. } elseif (preg_match('/_view/', $file)) {
  29. $view = preg_replace('#' . APP_DIR . '/tests/views/([a-zA-Z0-9_\-])_view_test.php#', '$1', $file);
  30. $view = implode('/', explode('_', $view));
  31. $implementation = APP_DIR . 'application/views/'.$view.'.php';
  32. }
  33. if (file_exists($implementation)) {
  34. require_once($implementation);
  35. }
  36. $test->addFile($file);
  37. }
  38. class CodeIgniterUnitTestCase extends UnitTestCase {
  39. protected $ci;
  40. public function __construct() {
  41. parent::UnitTestCase();
  42. $this->ci =& get_instance();
  43. }
  44. }
  45. class CodeIgniterWebTestCase extends WebTestCase {
  46. protected $ci;
  47. public function __construct() {
  48. parent::WebTestCase();
  49. $this->ci =& get_instance();
  50. }
  51. }
  52. //Capture CodeIgniter output, discard and load system into $CI variable
  53. ob_start();
  54. include(ROOT . 'index.php');
  55. $CI =& get_instance();
  56. ob_end_clean();
  57. //Setup the test suite
  58. $test =& new TestSuite();
  59. $test->_label = '68KB Test Suite';
  60. if (!isset($_GET['test'])) {
  61. //What are we testing?
  62. $files = array();
  63. if (isset($_GET['controllers'])) {
  64. $files = @scandir(APP_DIR . '/tests/controllers');
  65. } elseif (isset($_GET['models'])) {
  66. $files = @scandir(APP_DIR . '/tests/models');
  67. } elseif (isset($_GET['models'])) {
  68. $files = @scandir(APP_DIR . '/tests/views');
  69. } elseif (isset($_GET['all'])) {
  70. $files = @scandir(APP_DIR . '/tests/controllers');
  71. $files = array_merge($files, @scandir(APP_DIR . '/tests/models'));
  72. $files = array_merge($files, @scandir(APP_DIR . '/tests/views'));
  73. } else {
  74. //Use all by default
  75. $files = @scandir(APP_DIR . '/tests/controllers');
  76. $files = array_merge($files, @scandir(APP_DIR . '/tests/models'));
  77. $files = array_merge($files, @scandir(APP_DIR . '/tests/views'));
  78. }
  79. //Remove ., .. and any .whatever files, and add the full path
  80. function prepare_array($value, $key) {
  81. global $files;
  82. if (preg_match('/^\./', $value)) { unset($files[$key]); }
  83. if (preg_match('/_model/', $value)) { $files[$key] = APP_DIR . '/tests/models/' . $value; }
  84. if (preg_match('/_controller/', $value)) { $files[$key] = APP_DIR . '/tests/controllers/' . $value; }
  85. if (preg_match('/_view/', $value)) { $files[$key] = APP_DIR . '/tests/views/' . $value; }
  86. }
  87. array_walk($files, 'prepare_array');
  88. //Add each file to the test suite
  89. foreach ($files as $file) {
  90. add_test($file, $test);
  91. }
  92. $test->run(new HtmlReporter());
  93. } else {
  94. add_test(APP_DIR . '/tests/' . $file, $test);
  95. $test->run(new HtmlReporter());
  96. }
  97. //Run tests!
  98. include(APP_DIR . '/tests/custom_test_gui.php');
  99. //$test->run(new HtmlReporter());