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

main
Niels Keurentjes 9 years ago
parent 7b6ccde97a
commit e5fe3d8a3b

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

Loading…
Cancel
Save