Last active
October 13, 2015 20:18
-
-
Save neoascetic/4250799 to your computer and use it in GitHub Desktop.
[Laravel] Better PHPUnit integration for Laravel 3.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* To get Code Coverage support, install `xdebug` module. | |
* | |
* Usage: | |
* | |
* php artisan btests directory with/suffix.test.php or/without --env=test | |
* | |
*/ | |
class Btests_Task 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; | |
/** | |
* Run all of the unit tests for the application. | |
* | |
* @param array $tests | |
* @return void | |
*/ | |
public function run($tests) | |
{ | |
exit($this->safe($tests)); | |
} | |
/** | |
* Run all of the unit tests for the application without calling 'exit' at the end. | |
* | |
* @param array $tests | |
* @return integer PHPUnit exit status | |
*/ | |
public function safe($tests = array()) | |
{ | |
$this->base_path = path('sys').'cli'.DS.'tasks'.DS.'test'.DS; | |
if (is_dir($path = Bundle::path(DEFAULT_BUNDLE).'tests')) | |
{ | |
$tests = array_map(function ($p) use ($path) { | |
$p = $path.DS.$p; | |
if (File::exists($p) || File::exists($p = "{$p}.test.php")) return $p; | |
}, $tests); | |
$this->stub(empty($tests) ? array($path) : $tests); | |
return $this->test(); | |
} | |
} | |
/** | |
* Run the tests for a given bundle. | |
* | |
* @param array $bundles | |
* @return void | |
*/ | |
public function bundle($bundles = array()) | |
{ | |
if (count($bundles) == 0) | |
{ | |
$bundles = Bundle::names(); | |
} | |
$this->base_path = path('sys').'cli'.DS.'tasks'.DS.'test'.DS; | |
$tests = array(); | |
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')) | |
{ | |
$tests[] = $path; | |
} | |
} | |
$this->stub($tests); | |
exit($this->test()); | |
} | |
/** | |
* Run PHPUnit with the temporary XML configuration. | |
* | |
* @return integer PHPUnit exit status | |
*/ | |
protected function test() | |
{ | |
// We'll simply fire off PHPUnit with the configuration switch | |
// pointing to our requested configuration file. This allows | |
// 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); | |
// Preserve environment always | |
if ($env = Request::env()) putenv("LARAVEL_ENV={$env}"); | |
passthru('phpunit --coverage-text --configuration '.$esc_path, $status); | |
@unlink($path); | |
// Pass through the exit status | |
return $status; | |
} | |
/** | |
* Write a stub phpunit.xml file to the base directory. | |
* | |
* @param array $tests | |
* @return void | |
*/ | |
protected function stub(array $tests) | |
{ | |
$stub_path = __DIR__.DS.'stub.blade.php'; | |
$stub = View::make("path: {$stub_path}", array( | |
'tests' => $tests, | |
'bootstrap' => $this->base_path.'phpunit.php' | |
)); | |
File::put(path('base').'phpunit.xml', $stub); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<phpunit colors="true" | |
bootstrap="{{ $bootstrap }}" | |
backupGlobals="false"> | |
<testsuites> | |
<testsuite name="Test Suite"> | |
@foreach ($tests as $test) | |
@if (is_dir($test)) | |
<directory suffix=".test.php">{{ $test }}</directory> | |
@else | |
<file>{{ $test }}</file> | |
@endif | |
@endforeach | |
</testsuite> | |
</testsuites> | |
<filter> | |
<whitelist> | |
<directory suffix=".php">{{ path('app') }}</directory> | |
<exclude> | |
<directory suffix=".test.php">{{ path('app') }}</directory> | |
<directory suffix=".view.php">{{ path('app') }}</directory> | |
</exclude> | |
</whitelist> | |
</filter> | |
</phpunit> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment