runner.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php namespace Laravel\CLI\Tasks\Test;
  2. use Laravel\File;
  3. use Laravel\Bundle;
  4. use Laravel\CLI\Tasks\Task;
  5. class Runner extends Task {
  6. /**
  7. * Run all of the unit tests for the application.
  8. *
  9. * @return void
  10. */
  11. public function run()
  12. {
  13. $this->bundle(array(DEFAULT_BUNDLE));
  14. }
  15. /**
  16. * Run the tests for the Laravel framework.
  17. *
  18. * @return void
  19. */
  20. public function core()
  21. {
  22. if ( ! is_dir($GLOBALS['BUNDLE_PATH'].'laravel-tests'))
  23. {
  24. throw new \Exception("The bundle [laravel-tests] has not been installed!");
  25. }
  26. // When testing the Laravel core, we will just stub the path directly
  27. // so the test bundle is not required to be registered in the bundle
  28. // configuration, as it is kind of a unique bundle.
  29. $this->stub($GLOBALS['BUNDLE_PATH'].'laravel-tests/cases');
  30. $path = $GLOBALS['BUNDLE_PATH'].'laravel-tests/';
  31. // When testing the framework, we will swap out the application
  32. // and other core paths to stub out the application portion of
  33. // the framework for testing. This allows us to create dummy
  34. // controllers, views, models, etc.
  35. $GLOBALS['APP_PATH'] = $path.'application'.DS;
  36. $GLOBALS['BUNDLE_PATH'] = $path.'bundles'.DS;
  37. $GLOBALS['STORAGE_PATH'] = $path.'storage'.DS;
  38. $this->test();
  39. }
  40. /**
  41. * Run the tests for a given bundle.
  42. *
  43. * @param array $arguments
  44. * @return void
  45. */
  46. public function bundle($bundles = array())
  47. {
  48. if (count($bundles) == 0)
  49. {
  50. $bundles = Bundle::names();
  51. }
  52. foreach ($bundles as $bundle)
  53. {
  54. // To run PHPUnit for the application, bundles, and the framework
  55. // from one task, we'll dynamically stub PHPUnit.xml files via
  56. // the task and point the test suite to the correct directory
  57. // based on what was requested.
  58. if (is_dir($path = Bundle::path($bundle).'tests'))
  59. {
  60. $this->stub($path);
  61. $this->test();
  62. }
  63. }
  64. }
  65. /**
  66. * Run PHPUnit with the temporary XML configuration.
  67. *
  68. * @return void
  69. */
  70. protected function test()
  71. {
  72. // We'll simply fire off PHPUnit with the configuration switch
  73. // pointing to our temporary configuration file. This allows
  74. // us to flexibly run tests for any setup.
  75. passthru('phpunit -c '.$GLOBALS['BASE_PATH'].'phpunit.xml');
  76. @unlink($GLOBALS['BASE_PATH'].'phpunit.xml');
  77. }
  78. /**
  79. * Write a stub phpunit.xml file to the base directory.
  80. *
  81. * @param string $directory
  82. * @return void
  83. */
  84. protected function stub($directory)
  85. {
  86. $stub = File::get($GLOBALS['SYS_PATH'].'cli/tasks/test/stub.xml');
  87. $stub = str_replace('{{directory}}', $directory, $stub);
  88. File::put($GLOBALS['BASE_PATH'].'phpunit.xml', $stub);
  89. }
  90. }