runner.php 2.83 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()
	{
29
		$this->stub(path('sys').'tests/cases');
30

31 32 33 34
		$this->test();
	}

	/**
Taylor Otwell committed
35 36
	 * Run the tests for a given bundle.
	 *
Phill Sparks committed
37
	 * @param  array  $bundles
Taylor Otwell committed
38 39
	 * @return void
	 */
40
	public function bundle($bundles = array())
Taylor Otwell committed
41
	{
42 43 44 45
		if (count($bundles) == 0)
		{
			$bundles = Bundle::names();
		}
Taylor Otwell committed
46

47 48 49 50 51 52 53 54 55 56 57 58 59
		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
60 61 62 63 64 65 66 67 68 69 70 71
	}

	/**
	 * 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.
72
		$path = path('base').'phpunit.xml';
73 74
		
		// fix the spaced directories problem when using the command line
75
		// strings with spaces inside should be wrapped in quotes.
76
		$path = escapeshellarg($path);
Taylor Otwell committed
77

78 79 80
		passthru('phpunit --configuration '.$path);

		@unlink($path);
Taylor Otwell committed
81 82 83 84 85 86 87 88 89 90
	}

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

93 94 95 96
		$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
97
		// locations depending on what the developer wants to test.
98 99 100 101
		foreach (array('bootstrap', 'directory') as $item)
		{
			$stub = $this->{"swap_{$item}"}($stub, $path, $directory);
		}
Taylor Otwell committed
102

Taylor Otwell committed
103
		File::put(path('base').'phpunit.xml', $stub);
Taylor Otwell committed
104 105
	}

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	/**
	 * 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
132
}