Changeset View
Changeset View
Standalone View
Standalone View
src/unit/engine/PhpunitTestEngine.php
| <?php | <?php | ||||
| /** | /** | ||||
| * PHPUnit wrapper | * PHPUnit wrapper. | ||||
| * | |||||
| * To use, set unit.engine in .arcconfig, or use --engine flag | |||||
| * with arc unit. Currently supports only class & test files | |||||
| * (no directory support). | |||||
| * To use custom phpunit configuration, set phpunit_config in | |||||
| * .arcconfig (e.g. app/phpunit.xml.dist). | |||||
| * | |||||
| * @group unitrun | |||||
| */ | */ | ||||
| final class PhpunitTestEngine extends ArcanistBaseUnitTestEngine { | final class PhpunitTestEngine extends ArcanistBaseUnitTestEngine { | ||||
| private $configFile; | private $configFile; | ||||
| private $phpunitBinary = 'phpunit'; | private $phpunitBinary = 'phpunit'; | ||||
| private $affectedTests; | private $affectedTests; | ||||
| private $projectRoot; | private $projectRoot; | ||||
| public function run() { | public function run() { | ||||
| $this->projectRoot = $this->getWorkingCopy()->getProjectRoot(); | $this->projectRoot = $this->getWorkingCopy()->getProjectRoot(); | ||||
| $this->affectedTests = array(); | $this->affectedTests = array(); | ||||
| foreach ($this->getPaths() as $path) { | foreach ($this->getPaths() as $path) { | ||||
| $path = Filesystem::resolvePath($path, $this->projectRoot); | $path = Filesystem::resolvePath($path, $this->projectRoot); | ||||
| // TODO: add support for directories | // TODO: add support for directories | ||||
| // Users can call phpunit on the directory themselves | // Users can call phpunit on the directory themselves | ||||
| ▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | foreach ($this->affectedTests as $class_path => $test_path) { | ||||
| $stderr = '-d display_errors=stderr'; | $stderr = '-d display_errors=stderr'; | ||||
| $futures[$test_path] = new ExecFuture('%C %C %C --log-json %s %C %s', | $futures[$test_path] = new ExecFuture('%C %C %C --log-json %s %C %s', | ||||
| $this->phpunitBinary, $config, $stderr, $json_tmp, $clover, $test_path); | $this->phpunitBinary, $config, $stderr, $json_tmp, $clover, $test_path); | ||||
| $tmpfiles[$test_path] = array( | $tmpfiles[$test_path] = array( | ||||
| 'json' => $json_tmp, | 'json' => $json_tmp, | ||||
| 'clover' => $clover_tmp, | 'clover' => $clover_tmp, | ||||
| ); | ); | ||||
| } | } | ||||
| $results = array(); | $results = array(); | ||||
| foreach (Futures($futures)->limit(4) as $test => $future) { | foreach (Futures($futures)->limit(4) as $test => $future) { | ||||
| list($err, $stdout, $stderr) = $future->resolve(); | list($err, $stdout, $stderr) = $future->resolve(); | ||||
| $results[] = $this->parseTestResults( | $results[] = $this->parseTestResults( | ||||
| $test, | $test, | ||||
| $tmpfiles[$test]['json'], | $tmpfiles[$test]['json'], | ||||
| $tmpfiles[$test]['clover'], | $tmpfiles[$test]['clover'], | ||||
| $stderr); | $stderr); | ||||
| } | } | ||||
| return array_mergev($results); | return array_mergev($results); | ||||
| } | } | ||||
| /** | /** | ||||
| * Parse test results from phpunit json report | * Parse test results from phpunit json report. | ||||
| * | * | ||||
| * @param string $path Path to test | * @param string $path Path to test | ||||
| * @param string $json_tmp Path to phpunit json report | * @param string $json_tmp Path to phpunit json report | ||||
| * @param string $clover_tmp Path to phpunit clover report | * @param string $clover_tmp Path to phpunit clover report | ||||
| * @param string $stderr Data written to stderr | * @param string $stderr Data written to stderr | ||||
| * | * | ||||
| * @return array | * @return array | ||||
| */ | */ | ||||
| ▲ Show 20 Lines • Show All 141 Lines • ▼ Show 20 Lines | public static function getSearchLocationsForTests($path) { | ||||
| foreach ($try_directories as $parts) { | foreach ($try_directories as $parts) { | ||||
| $results[implode(DIRECTORY_SEPARATOR, $parts).DIRECTORY_SEPARATOR] = true; | $results[implode(DIRECTORY_SEPARATOR, $parts).DIRECTORY_SEPARATOR] = true; | ||||
| } | } | ||||
| return array_keys($results); | return array_keys($results); | ||||
| } | } | ||||
| /** | /** | ||||
| * Tries to find and update phpunit configuration file | * Tries to find and update phpunit configuration file based on | ||||
| * based on phpunit_config option in .arcconfig | * `phpunit_config` option in `.arcconfig`. | ||||
| */ | */ | ||||
| private function prepareConfigFile() { | private function prepareConfigFile() { | ||||
| $project_root = $this->projectRoot.DIRECTORY_SEPARATOR; | $project_root = $this->projectRoot.DIRECTORY_SEPARATOR; | ||||
| $config = $this->getConfigurationManager()->getConfigFromAnySource( | $config = $this->getConfigurationManager()->getConfigFromAnySource( | ||||
| 'phpunit_config'); | 'phpunit_config'); | ||||
| if ($config) { | if ($config) { | ||||
| if (Filesystem::pathExists($project_root.$config)) { | if (Filesystem::pathExists($project_root.$config)) { | ||||
| Show All 9 Lines | if ($bin) { | ||||
| if (Filesystem::binaryExists($bin)) { | if (Filesystem::binaryExists($bin)) { | ||||
| $this->phpunitBinary = $bin; | $this->phpunitBinary = $bin; | ||||
| } | } | ||||
| else { | else { | ||||
| $this->phpunitBinary = Filesystem::resolvePath($bin, $project_root); | $this->phpunitBinary = Filesystem::resolvePath($bin, $project_root); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||