runner.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php namespace Laravel\CLI\Tasks\Test;
  2. use Laravel\File;
  3. use Laravel\Bundle;
  4. use Laravel\Request;
  5. use Laravel\CLI\Tasks\Task;
  6. class Runner extends Task {
  7. /**
  8. * The base directory where the tests will be executed.
  9. *
  10. * A phpunit.xml should also be stored in that directory.
  11. *
  12. * @var string
  13. */
  14. protected $base_path;
  15. /**
  16. * Run all of the unit tests for the application.
  17. *
  18. * @param array $bundles
  19. * @return void
  20. */
  21. public function run($bundles = array())
  22. {
  23. if (count($bundles) == 0) $bundles = array(DEFAULT_BUNDLE);
  24. $this->bundle($bundles);
  25. }
  26. /**
  27. * Run the tests for the Laravel framework.
  28. *
  29. * @return void
  30. */
  31. public function core()
  32. {
  33. $this->base_path = path('sys').'tests'.DS;
  34. $this->stub(path('sys').'tests'.DS.'cases');
  35. $this->test();
  36. }
  37. /**
  38. * Run the tests for a given bundle.
  39. *
  40. * @param array $bundles
  41. * @return void
  42. */
  43. public function bundle($bundles = array())
  44. {
  45. if (count($bundles) == 0)
  46. {
  47. $bundles = Bundle::names();
  48. }
  49. $this->base_path = path('sys').'cli'.DS.'tasks'.DS.'test'.DS;
  50. foreach ($bundles as $bundle)
  51. {
  52. // To run PHPUnit for the application, bundles, and the framework
  53. // from one task, we'll dynamically stub PHPUnit.xml files via
  54. // the task and point the test suite to the correct directory
  55. // based on what was requested.
  56. if (is_dir($path = Bundle::path($bundle).'tests'))
  57. {
  58. $this->stub($path);
  59. $this->test();
  60. }
  61. }
  62. }
  63. /**
  64. * Run PHPUnit with the temporary XML configuration.
  65. *
  66. * @return void
  67. */
  68. protected function test()
  69. {
  70. // We'll simply fire off PHPUnit with the configuration switch
  71. // pointing to our requested configuration file. This allows
  72. // us to flexibly run tests for any setup.
  73. $path = 'phpunit.xml';
  74. // fix the spaced directories problem when using the command line
  75. // strings with spaces inside should be wrapped in quotes.
  76. $esc_path = escapeshellarg($path);
  77. passthru('LARAVEL_ENV='.Request::env().' phpunit --configuration '.$esc_path, $status);
  78. @unlink($path);
  79. // Pass through the exit status
  80. exit($status);
  81. }
  82. /**
  83. * Write a stub phpunit.xml file to the base directory.
  84. *
  85. * @param string $directory
  86. * @return void
  87. */
  88. protected function stub($directory)
  89. {
  90. $path = path('sys').'cli/tasks/test/';
  91. $stub = File::get($path.'stub.xml');
  92. // The PHPUnit bootstrap file contains several items that are swapped
  93. // at test time. This allows us to point PHPUnit at a few different
  94. // locations depending on what the developer wants to test.
  95. foreach (array('bootstrap', 'directory') as $item)
  96. {
  97. $stub = $this->{"swap_{$item}"}($stub, $directory);
  98. }
  99. File::put(path('base').'phpunit.xml', $stub);
  100. }
  101. /**
  102. * Swap the bootstrap file in the stub.
  103. *
  104. * @param string $stub
  105. * @param string $directory
  106. * @return string
  107. */
  108. protected function swap_bootstrap($stub, $directory)
  109. {
  110. return str_replace('{{bootstrap}}', $this->base_path.'phpunit.php', $stub);
  111. }
  112. /**
  113. * Swap the directory in the stub.
  114. *
  115. * @param string $stub
  116. * @param string $directory
  117. * @return string
  118. */
  119. protected function swap_directory($stub, $directory)
  120. {
  121. return str_replace('{{directory}}', $directory, $stub);
  122. }
  123. }