2012-08-13 23:16:40 +00:00
|
|
|
<?php
|
|
|
|
|
2013-05-27 08:41:50 +00:00
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2012-08-13 23:16:40 +00:00
|
|
|
namespace Composer\Test;
|
|
|
|
|
2016-01-21 12:01:55 +00:00
|
|
|
use Composer\TestCase;
|
2012-08-13 23:16:40 +00:00
|
|
|
use Composer\Util\Filesystem;
|
|
|
|
use Symfony\Component\Finder\Finder;
|
2016-01-21 12:01:55 +00:00
|
|
|
use Symfony\Component\Process\Process;
|
2012-08-13 23:16:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group slow
|
|
|
|
*/
|
2016-01-21 12:01:55 +00:00
|
|
|
class AllFunctionalTest extends TestCase
|
2012-08-13 23:16:40 +00:00
|
|
|
{
|
2012-11-10 18:42:29 +00:00
|
|
|
protected $oldcwd;
|
2012-11-11 14:52:37 +00:00
|
|
|
protected $oldenv;
|
2012-11-10 20:54:23 +00:00
|
|
|
protected $testDir;
|
2012-11-11 16:09:57 +00:00
|
|
|
private static $pharPath;
|
2012-11-10 20:54:23 +00:00
|
|
|
|
2012-11-10 18:42:29 +00:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->oldcwd = getcwd();
|
2016-01-21 12:01:55 +00:00
|
|
|
|
2012-11-10 18:42:29 +00:00
|
|
|
chdir(__DIR__.'/Fixtures/functional');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
chdir($this->oldcwd);
|
2016-01-21 12:01:55 +00:00
|
|
|
|
2012-11-11 14:52:37 +00:00
|
|
|
$fs = new Filesystem;
|
2016-01-21 12:01:55 +00:00
|
|
|
|
2012-11-10 20:54:23 +00:00
|
|
|
if ($this->testDir) {
|
|
|
|
$fs->removeDirectory($this->testDir);
|
|
|
|
$this->testDir = null;
|
|
|
|
}
|
2016-01-21 12:01:55 +00:00
|
|
|
|
2012-11-11 14:52:37 +00:00
|
|
|
if ($this->oldenv) {
|
|
|
|
$fs->removeDirectory(getenv('COMPOSER_HOME'));
|
2015-03-06 16:20:27 +00:00
|
|
|
$_SERVER['COMPOSER_HOME'] = $this->oldenv;
|
|
|
|
putenv('COMPOSER_HOME='.$_SERVER['COMPOSER_HOME']);
|
2012-11-11 14:52:37 +00:00
|
|
|
$this->oldenv = null;
|
|
|
|
}
|
2012-11-11 16:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
2016-01-21 12:01:55 +00:00
|
|
|
self::$pharPath = self::getUniqueTmpDirectory() . '/composer.phar';
|
2012-11-11 16:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function tearDownAfterClass()
|
|
|
|
{
|
|
|
|
$fs = new Filesystem;
|
|
|
|
$fs->removeDirectory(dirname(self::$pharPath));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBuildPhar()
|
|
|
|
{
|
2014-12-02 13:31:37 +00:00
|
|
|
if (defined('HHVM_VERSION')) {
|
|
|
|
$this->markTestSkipped('Building the phar does not work on HHVM.');
|
|
|
|
}
|
2014-12-07 13:54:29 +00:00
|
|
|
|
2015-05-01 13:41:15 +00:00
|
|
|
$target = dirname(self::$pharPath);
|
2016-01-21 12:01:55 +00:00
|
|
|
$fs = new Filesystem();
|
2015-05-01 13:41:15 +00:00
|
|
|
chdir($target);
|
|
|
|
|
|
|
|
$it = new \RecursiveDirectoryIterator(__DIR__.'/../../../', \RecursiveDirectoryIterator::SKIP_DOTS);
|
|
|
|
$ri = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::SELF_FIRST);
|
|
|
|
|
|
|
|
foreach ($ri as $file) {
|
|
|
|
$targetPath = $target . DIRECTORY_SEPARATOR . $ri->getSubPathName();
|
|
|
|
if ($file->isDir()) {
|
|
|
|
$fs->ensureDirectoryExists($targetPath);
|
|
|
|
} else {
|
|
|
|
copy($file->getPathname(), $targetPath);
|
|
|
|
}
|
|
|
|
}
|
2012-11-11 16:09:57 +00:00
|
|
|
|
2015-05-01 13:41:15 +00:00
|
|
|
$proc = new Process('php '.escapeshellarg('./bin/compile'), $target);
|
2012-11-11 16:09:57 +00:00
|
|
|
$exitcode = $proc->run();
|
2016-01-21 12:01:55 +00:00
|
|
|
|
2013-03-10 12:58:49 +00:00
|
|
|
if ($exitcode !== 0 || trim($proc->getOutput())) {
|
|
|
|
$this->fail($proc->getOutput());
|
|
|
|
}
|
2016-01-21 12:01:55 +00:00
|
|
|
|
2012-11-11 16:09:57 +00:00
|
|
|
$this->assertTrue(file_exists(self::$pharPath));
|
2012-11-10 18:42:29 +00:00
|
|
|
}
|
|
|
|
|
2012-08-13 23:16:40 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider getTestFiles
|
2012-11-11 16:09:57 +00:00
|
|
|
* @depends testBuildPhar
|
2012-08-13 23:16:40 +00:00
|
|
|
*/
|
|
|
|
public function testIntegration(\SplFileInfo $testFile)
|
|
|
|
{
|
|
|
|
$testData = $this->parseTestFile($testFile);
|
|
|
|
|
2012-11-11 14:52:37 +00:00
|
|
|
$this->oldenv = getenv('COMPOSER_HOME');
|
2015-03-06 16:20:27 +00:00
|
|
|
$_SERVER['COMPOSER_HOME'] = $this->testDir.'home';
|
|
|
|
putenv('COMPOSER_HOME='.$_SERVER['COMPOSER_HOME']);
|
2012-11-11 14:52:37 +00:00
|
|
|
|
2012-11-11 16:09:57 +00:00
|
|
|
$cmd = 'php '.escapeshellarg(self::$pharPath).' --no-ansi '.$testData['RUN'];
|
2015-02-23 08:57:54 +00:00
|
|
|
$proc = new Process($cmd, __DIR__.'/Fixtures/functional', null, null, 300);
|
2012-08-13 23:16:40 +00:00
|
|
|
$exitcode = $proc->run();
|
|
|
|
|
|
|
|
if (isset($testData['EXPECT'])) {
|
2012-09-07 22:43:51 +00:00
|
|
|
$this->assertEquals($testData['EXPECT'], $this->cleanOutput($proc->getOutput()), 'Error Output: '.$proc->getErrorOutput());
|
2012-08-13 23:16:40 +00:00
|
|
|
}
|
|
|
|
if (isset($testData['EXPECT-REGEX'])) {
|
2012-09-07 22:43:51 +00:00
|
|
|
$this->assertRegExp($testData['EXPECT-REGEX'], $this->cleanOutput($proc->getOutput()), 'Error Output: '.$proc->getErrorOutput());
|
2012-08-13 23:16:40 +00:00
|
|
|
}
|
|
|
|
if (isset($testData['EXPECT-ERROR'])) {
|
|
|
|
$this->assertEquals($testData['EXPECT-ERROR'], $this->cleanOutput($proc->getErrorOutput()));
|
|
|
|
}
|
|
|
|
if (isset($testData['EXPECT-ERROR-REGEX'])) {
|
|
|
|
$this->assertRegExp($testData['EXPECT-ERROR-REGEX'], $this->cleanOutput($proc->getErrorOutput()));
|
|
|
|
}
|
|
|
|
if (isset($testData['EXPECT-EXIT-CODE'])) {
|
|
|
|
$this->assertSame($testData['EXPECT-EXIT-CODE'], $exitcode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTestFiles()
|
|
|
|
{
|
|
|
|
$tests = array();
|
|
|
|
foreach (Finder::create()->in(__DIR__.'/Fixtures/functional')->name('*.test')->files() as $file) {
|
|
|
|
$tests[] = array($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tests;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function parseTestFile(\SplFileInfo $file)
|
|
|
|
{
|
|
|
|
$tokens = preg_split('#(?:^|\n*)--([A-Z-]+)--\n#', file_get_contents($file->getRealPath()), null, PREG_SPLIT_DELIM_CAPTURE);
|
|
|
|
$data = array();
|
|
|
|
$section = null;
|
|
|
|
|
2016-01-21 12:01:55 +00:00
|
|
|
$testDir = self::getUniqueTmpDirectory();
|
2012-11-10 20:54:23 +00:00
|
|
|
$this->testDir = $testDir;
|
2012-08-13 23:16:40 +00:00
|
|
|
$varRegex = '#%([a-zA-Z_-]+)%#';
|
2014-06-10 14:02:44 +00:00
|
|
|
$variableReplacer = function ($match) use (&$data, $testDir) {
|
2012-08-13 23:16:40 +00:00
|
|
|
list(, $var) = $match;
|
|
|
|
|
|
|
|
switch ($var) {
|
|
|
|
case 'testDir':
|
|
|
|
$data['test_dir'] = $testDir;
|
|
|
|
|
|
|
|
return $testDir;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown variable "%s". Supported variables: "testDir"', $var));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for ($i = 0, $c = count($tokens); $i < $c; $i++) {
|
|
|
|
if ('' === $tokens[$i] && null === $section) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle section headers.
|
|
|
|
if (null === $section) {
|
|
|
|
$section = $tokens[$i];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sectionData = $tokens[$i];
|
|
|
|
|
|
|
|
// Allow sections to validate, or modify their section data.
|
|
|
|
switch ($section) {
|
|
|
|
case 'RUN':
|
|
|
|
$sectionData = preg_replace_callback($varRegex, $variableReplacer, $sectionData);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'EXPECT-EXIT-CODE':
|
2017-03-08 14:07:29 +00:00
|
|
|
$sectionData = (int) $sectionData;
|
2016-08-15 04:30:20 +00:00
|
|
|
break;
|
2012-08-13 23:16:40 +00:00
|
|
|
|
|
|
|
case 'EXPECT':
|
|
|
|
case 'EXPECT-REGEX':
|
|
|
|
case 'EXPECT-ERROR':
|
|
|
|
case 'EXPECT-ERROR-REGEX':
|
|
|
|
$sectionData = preg_replace_callback($varRegex, $variableReplacer, $sectionData);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new \RuntimeException(sprintf(
|
|
|
|
'Unknown section "%s". Allowed sections: "RUN", "EXPECT", "EXPECT-ERROR", "EXPECT-EXIT-CODE", "EXPECT-REGEX", "EXPECT-ERROR-REGEX". '
|
|
|
|
.'Section headers must be written as "--HEADER_NAME--".',
|
|
|
|
$section
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
$data[$section] = $sectionData;
|
|
|
|
$section = $sectionData = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate data
|
|
|
|
if (!isset($data['RUN'])) {
|
|
|
|
throw new \RuntimeException('The test file must have a section named "RUN".');
|
|
|
|
}
|
|
|
|
if (!isset($data['EXPECT']) && !isset($data['EXPECT-ERROR']) && !isset($data['EXPECT-REGEX']) && !isset($data['EXPECT-ERROR-REGEX'])) {
|
|
|
|
throw new \RuntimeException('The test file must have a section named "EXPECT", "EXPECT-ERROR", "EXPECT-REGEX", or "EXPECT-ERROR-REGEX".');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function cleanOutput($output)
|
|
|
|
{
|
|
|
|
$processed = '';
|
|
|
|
|
|
|
|
for ($i = 0; $i < strlen($output); $i++) {
|
|
|
|
if ($output[$i] === "\x08") {
|
|
|
|
$processed = substr($processed, 0, -1);
|
|
|
|
} elseif ($output[$i] !== "\r") {
|
|
|
|
$processed .= $output[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $processed;
|
|
|
|
}
|
2012-08-18 14:22:15 +00:00
|
|
|
}
|