1
0
Fork 0

Expanded InstallerTest to support expecting Exceptions by supplying "EXCEPTION" as "--EXPECT--"

pull/4838/head
Niels Keurentjes 2016-01-27 10:04:45 +01:00
parent 7b6ccde97a
commit e5fe3d8a3b
1 changed files with 97 additions and 84 deletions

View File

@ -158,96 +158,109 @@ class InstallerTest extends TestCase
->method('writeError') ->method('writeError')
->will($this->returnCallback($callback)); ->will($this->returnCallback($callback));
$composer = FactoryMock::create($io, $composerConfig); // Prepare for exceptions
try {
$composer = FactoryMock::create($io, $composerConfig);
$jsonMock = $this->getMockBuilder('Composer\Json\JsonFile')->disableOriginalConstructor()->getMock(); $jsonMock = $this->getMockBuilder('Composer\Json\JsonFile')->disableOriginalConstructor()->getMock();
$jsonMock->expects($this->any()) $jsonMock->expects($this->any())
->method('read') ->method('read')
->will($this->returnValue($installed)); ->will($this->returnValue($installed));
$jsonMock->expects($this->any()) $jsonMock->expects($this->any())
->method('exists') ->method('exists')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$repositoryManager = $composer->getRepositoryManager(); $repositoryManager = $composer->getRepositoryManager();
$repositoryManager->setLocalRepository(new InstalledFilesystemRepositoryMock($jsonMock)); $repositoryManager->setLocalRepository(new InstalledFilesystemRepositoryMock($jsonMock));
$lockJsonMock = $this->getMockBuilder('Composer\Json\JsonFile')->disableOriginalConstructor()->getMock(); $lockJsonMock = $this->getMockBuilder('Composer\Json\JsonFile')->disableOriginalConstructor()->getMock();
$lockJsonMock->expects($this->any()) $lockJsonMock->expects($this->any())
->method('read') ->method('read')
->will($this->returnValue($lock)); ->will($this->returnValue($lock));
$lockJsonMock->expects($this->any()) $lockJsonMock->expects($this->any())
->method('exists') ->method('exists')
->will($this->returnValue(true)); ->will($this->returnValue(true));
if ($expectLock) { if ($expectLock) {
$actualLock = array(); $actualLock = array();
$lockJsonMock->expects($this->atLeastOnce()) $lockJsonMock->expects($this->atLeastOnce())
->method('write') ->method('write')
->will($this->returnCallback(function ($hash, $options) use (&$actualLock) { ->will($this->returnCallback(function ($hash, $options) use (&$actualLock) {
// need to do assertion outside of mock for nice phpunit output // need to do assertion outside of mock for nice phpunit output
// so store value temporarily in reference for later assetion // so store value temporarily in reference for later assetion
$actualLock = $hash; $actualLock = $hash;
})); }));
}
$contents = json_encode($composerConfig);
$locker = new Locker($io, $lockJsonMock, $repositoryManager, $composer->getInstallationManager(), $contents);
$composer->setLocker($locker);
$eventDispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock();
$autoloadGenerator = $this->getMock('Composer\Autoload\AutoloadGenerator', array(), array($eventDispatcher));
$composer->setAutoloadGenerator($autoloadGenerator);
$composer->setEventDispatcher($eventDispatcher);
$installer = Installer::create($io, $composer);
$application = new Application;
$application->get('install')->setCode(function ($input, $output) use ($installer) {
$installer
->setDevMode(!$input->getOption('no-dev'))
->setDryRun($input->getOption('dry-run'))
->setIgnorePlatformRequirements($input->getOption('ignore-platform-reqs'));
return $installer->run();
});
$application->get('update')->setCode(function ($input, $output) use ($installer) {
$installer
->setDevMode(!$input->getOption('no-dev'))
->setUpdate(true)
->setDryRun($input->getOption('dry-run'))
->setUpdateWhitelist($input->getArgument('packages'))
->setWhitelistDependencies($input->getOption('with-dependencies'))
->setPreferStable($input->getOption('prefer-stable'))
->setPreferLowest($input->getOption('prefer-lowest'))
->setIgnorePlatformRequirements($input->getOption('ignore-platform-reqs'));
return $installer->run();
});
if (!preg_match('{^(install|update)\b}', $run)) {
throw new \UnexpectedValueException('The run command only supports install and update');
}
$application->setAutoExit(false);
$appOutput = fopen('php://memory', 'w+');
$result = $application->run(new StringInput($run), new StreamOutput($appOutput));
fseek($appOutput, 0);
$this->assertEquals($expectExitCode, $result, $output . stream_get_contents($appOutput));
if ($expectLock) {
unset($actualLock['hash']);
unset($actualLock['content-hash']);
unset($actualLock['_readme']);
$this->assertEquals($expectLock, $actualLock);
}
$installationManager = $composer->getInstallationManager();
$this->assertSame(rtrim($expect), implode("\n", $installationManager->getTrace()));
if ($expectOutput) {
$this->assertEquals(rtrim($expectOutput), rtrim($output));
}
} }
catch(\Exception $e) {
$contents = json_encode($composerConfig); // Exception was thrown during execution
$locker = new Locker($io, $lockJsonMock, $repositoryManager, $composer->getInstallationManager(), $contents); if (!$expect || !$expectOutput) {
$composer->setLocker($locker); throw $e;
}
$eventDispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock(); $this->assertEquals('EXCEPTION', rtrim($expect));
$autoloadGenerator = $this->getMock('Composer\Autoload\AutoloadGenerator', array(), array($eventDispatcher)); $normalizedOutput = rtrim(str_replace("\n", PHP_EOL, $expectOutput));
$composer->setAutoloadGenerator($autoloadGenerator); $this->assertEquals($normalizedOutput, rtrim($e->getMessage()));
$composer->setEventDispatcher($eventDispatcher);
$installer = Installer::create($io, $composer);
$application = new Application;
$application->get('install')->setCode(function ($input, $output) use ($installer) {
$installer
->setDevMode(!$input->getOption('no-dev'))
->setDryRun($input->getOption('dry-run'))
->setIgnorePlatformRequirements($input->getOption('ignore-platform-reqs'));
return $installer->run();
});
$application->get('update')->setCode(function ($input, $output) use ($installer) {
$installer
->setDevMode(!$input->getOption('no-dev'))
->setUpdate(true)
->setDryRun($input->getOption('dry-run'))
->setUpdateWhitelist($input->getArgument('packages'))
->setWhitelistDependencies($input->getOption('with-dependencies'))
->setPreferStable($input->getOption('prefer-stable'))
->setPreferLowest($input->getOption('prefer-lowest'))
->setIgnorePlatformRequirements($input->getOption('ignore-platform-reqs'));
return $installer->run();
});
if (!preg_match('{^(install|update)\b}', $run)) {
throw new \UnexpectedValueException('The run command only supports install and update');
}
$application->setAutoExit(false);
$appOutput = fopen('php://memory', 'w+');
$result = $application->run(new StringInput($run), new StreamOutput($appOutput));
fseek($appOutput, 0);
$this->assertEquals($expectExitCode, $result, $output . stream_get_contents($appOutput));
if ($expectLock) {
unset($actualLock['hash']);
unset($actualLock['content-hash']);
unset($actualLock['_readme']);
$this->assertEquals($expectLock, $actualLock);
}
$installationManager = $composer->getInstallationManager();
$this->assertSame(rtrim($expect), implode("\n", $installationManager->getTrace()));
if ($expectOutput) {
$this->assertEquals(rtrim($expectOutput), rtrim($output));
} }
return;
} }
public function getIntegrationTests() public function getIntegrationTests()