runner.php 3.05 KB
Newer Older
Taylor Otwell committed
1 2 3 4
<?php namespace Laravel\CLI\Tasks\Test;

use Laravel\File;
use Laravel\Bundle;
5
use Laravel\Request;
Taylor Otwell committed
6 7 8 9 10 11 12 13 14
use Laravel\CLI\Tasks\Task;

class Runner extends Task {

	/**
	 * Run all of the unit tests for the application.
	 *
	 * @return void
	 */
Taylor Otwell committed
15
	public function run($bundles = array())
Taylor Otwell committed
16
	{
Taylor Otwell committed
17 18 19
		if (count($bundles) == 0) $bundles = array(DEFAULT_BUNDLE);

		$this->bundle($bundles);
Taylor Otwell committed
20 21 22
	}

	/**
23 24 25 26 27 28
	 * Run the tests for the Laravel framework.
	 *
	 * @return void
	 */
	public function core()
	{
Taylor Otwell committed
29
		if ( ! is_dir(path('bundle').'laravel-tests'))
30 31 32 33 34 35 36
		{
			throw new \Exception("The bundle [laravel-tests] has not been installed!");
		}

		// When testing the Laravel core, we will just stub the path directly
		// so the test bundle is not required to be registered in the bundle
		// configuration, as it is kind of a unique bundle.
Taylor Otwell committed
37
		$this->stub(path('bundle').'laravel-tests/cases');
38

Taylor Otwell committed
39
		$path = path('bundle').'laravel-tests/';
40

41 42 43 44
		$this->test();
	}

	/**
Taylor Otwell committed
45 46
	 * Run the tests for a given bundle.
	 *
Phill Sparks committed
47
	 * @param  array  $bundles
Taylor Otwell committed
48 49
	 * @return void
	 */
50
	public function bundle($bundles = array())
Taylor Otwell committed
51
	{
52 53 54 55
		if (count($bundles) == 0)
		{
			$bundles = Bundle::names();
		}
Taylor Otwell committed
56

57 58 59 60 61 62 63 64 65 66 67 68 69
		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();				
			}
		}
Taylor Otwell committed
70 71 72 73 74 75 76 77 78 79 80 81
	}

	/**
	 * 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 temporary configuration file. This allows
		// us to flexibly run tests for any setup.
82
		$path = path('base').'phpunit.xml';
Taylor Otwell committed
83

84 85 86
		passthru('phpunit --configuration '.$path);

		@unlink($path);
Taylor Otwell committed
87 88 89 90 91 92 93 94 95 96
	}

	/**
	 * Write a stub phpunit.xml file to the base directory.
	 *
	 * @param  string  $directory
	 * @return void
	 */
	protected function stub($directory)
	{
97
		$path = path('sys').'cli/tasks/test/';
Taylor Otwell committed
98

99 100 101 102
		$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
103
		// locations depending on what the developer wants to test.
104 105 106 107
		foreach (array('bootstrap', 'directory') as $item)
		{
			$stub = $this->{"swap_{$item}"}($stub, $path, $directory);
		}
Taylor Otwell committed
108

Taylor Otwell committed
109
		File::put(path('base').'phpunit.xml', $stub);
Taylor Otwell committed
110 111
	}

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	/**
	 * Swap the bootstrap file in the stub.
	 *
	 * @param  string  $stub
	 * @param  string  $path
	 * @param  string  $directory
	 * @return string
	 */
	protected function swap_bootstrap($stub, $path, $directory)
	{
		return str_replace('{{bootstrap}}', $path.'phpunit.php', $stub);
	}

	/**
	 * Swap the directory in the stub.
	 *
	 * @param  string  $stub
	 * @param  string  $path
	 * @param  string  $directory
	 * @return string
	 */
	protected function swap_directory($stub, $path, $directory)
	{
		return str_replace('{{directory}}', $directory, $stub);
	}

Taylor Otwell committed
138
}