Files
spa-api/laravel/cli/tasks/test/runner.php

146 lines
3.1 KiB
PHP
Raw Normal View History

2012-01-27 15:28:46 -06:00
<?php namespace Laravel\CLI\Tasks\Test;
use Laravel\File;
use Laravel\Bundle;
2012-02-07 20:09:17 -06:00
use Laravel\Request;
2012-01-27 15:28:46 -06:00
use Laravel\CLI\Tasks\Task;
class Runner extends Task {
/**
* The base directory where the tests will be executed.
*
* A phpunit.xml should also be stored in that directory.
*
* @var string
*/
protected $base_path;
2012-01-27 15:28:46 -06:00
/**
* Run all of the unit tests for the application.
*
2012-08-03 18:17:48 +03:00
* @param array $bundles
2012-01-27 15:28:46 -06:00
* @return void
*/
2012-02-16 10:32:39 -06:00
public function run($bundles = array())
2012-01-27 15:28:46 -06:00
{
2012-02-16 10:32:39 -06:00
if (count($bundles) == 0) $bundles = array(DEFAULT_BUNDLE);
$this->bundle($bundles);
2012-01-27 15:28:46 -06:00
}
2012-01-27 15:43:05 -06:00
/**
* Run the tests for the Laravel framework.
*
* @return void
*/
public function core()
{
$this->base_path = path('sys').'tests'.DS;
$this->stub(path('sys').'tests'.DS.'cases');
2012-01-27 16:17:43 -06:00
2012-01-27 15:43:05 -06:00
$this->test();
}
2012-01-27 15:28:46 -06:00
/**
* Run the tests for a given bundle.
*
2012-02-04 21:30:52 +00:00
* @param array $bundles
2012-01-27 15:28:46 -06:00
* @return void
*/
2012-01-27 15:43:05 -06:00
public function bundle($bundles = array())
2012-01-27 15:28:46 -06:00
{
2012-01-27 15:43:05 -06:00
if (count($bundles) == 0)
{
$bundles = Bundle::names();
}
2012-01-27 15:28:46 -06:00
$this->base_path = path('sys').'cli'.DS.'tasks'.DS.'test'.DS;
2012-01-27 15:43:05 -06:00
foreach ($bundles as $bundle)
{
// To run PHPUnit for the application, bundles, and the framework
// from one task, we'll dynamically stub PHPUnit.xml files via
// the task and point the test suite to the correct directory
// based on what was requested.
if (is_dir($path = Bundle::path($bundle).'tests'))
{
$this->stub($path);
$this->test();
}
}
2012-01-27 15:28:46 -06:00
}
/**
* Run PHPUnit with the temporary XML configuration.
*
* @return void
*/
protected function test()
{
// We'll simply fire off PHPUnit with the configuration switch
// pointing to our requested configuration file. This allows
2012-01-27 15:28:46 -06:00
// us to flexibly run tests for any setup.
$path = 'phpunit.xml';
// fix the spaced directories problem when using the command line
// strings with spaces inside should be wrapped in quotes.
$esc_path = escapeshellarg($path);
2012-01-27 15:28:46 -06:00
passthru('phpunit --configuration '.$esc_path, $status);
2012-02-07 20:09:17 -06:00
@unlink($path);
// Pass through the exit status
exit($status);
2012-01-27 15:28:46 -06:00
}
/**
* Write a stub phpunit.xml file to the base directory.
*
* @param string $directory
* @return void
*/
protected function stub($directory)
{
$path = path('sys').'cli/tasks/test/';
2012-01-27 15:28:46 -06:00
$stub = File::get($path.'stub.xml');
// The PHPUnit bootstrap file contains several items that are swapped
// at test time. This allows us to point PHPUnit at a few different
2012-02-07 20:09:17 -06:00
// locations depending on what the developer wants to test.
foreach (array('bootstrap', 'directory') as $item)
{
$stub = $this->{"swap_{$item}"}($stub, $directory);
}
2012-01-27 15:28:46 -06:00
2012-01-28 14:55:08 -06:00
File::put(path('base').'phpunit.xml', $stub);
2012-01-27 15:28:46 -06:00
}
/**
* Swap the bootstrap file in the stub.
*
* @param string $stub
* @param string $directory
* @return string
*/
protected function swap_bootstrap($stub, $directory)
{
return str_replace('{{bootstrap}}', $this->base_path.'phpunit.php', $stub);
}
/**
* Swap the directory in the stub.
*
* @param string $stub
* @param string $directory
* @return string
*/
protected function swap_directory($stub, $directory)
{
return str_replace('{{directory}}', $directory, $stub);
}
2012-01-27 15:28:46 -06:00
}