Ejemplo n.º 1
0
 public function testSearchWithType()
 {
     $repoConfig = array('url' => 'http://example.org');
     $result = array('results' => array(array('name' => 'foo', 'description' => null)));
     $rfs = $this->getMockBuilder('Composer\\Util\\RemoteFilesystem')->disableOriginalConstructor()->getMock();
     $rfs->expects($this->at(0))->method('getContents')->with('example.org', 'http://example.org/packages.json', false)->willReturn(json_encode(array('search' => '/search.json?q=%query%&type=%type%')));
     $rfs->expects($this->at(1))->method('getContents')->with('example.org', 'http://example.org/search.json?q=foo&type=composer-plugin', false)->willReturn(json_encode($result));
     $repository = new ComposerRepository($repoConfig, new NullIO(), FactoryMock::createConfig(), null, $rfs);
     $this->assertSame(array(array('name' => 'foo', 'description' => null)), $repository->search('foo', RepositoryInterface::SEARCH_FULLTEXT, 'composer-plugin'));
     $this->assertEmpty($repository->search('foo', RepositoryInterface::SEARCH_FULLTEXT, 'library'));
 }
Ejemplo n.º 2
0
 /**
  * @dataProvider loadDataProvider
  */
 public function testLoadData(array $expected, array $repoPackages)
 {
     $repoConfig = array('url' => 'http://example.org');
     $repository = $this->getMock('Composer\\Repository\\ComposerRepository', array('loadRootServerFile', 'createPackage'), array($repoConfig, new NullIO(), FactoryMock::createConfig()));
     $repository->expects($this->exactly(2))->method('loadRootServerFile')->will($this->returnValue($repoPackages));
     foreach ($expected as $at => $arg) {
         $stubPackage = $this->getPackage('stub/stub', '1.0.0');
         $repository->expects($this->at($at + 2))->method('createPackage')->with($this->identicalTo($arg), $this->equalTo('Composer\\Package\\CompletePackage'))->will($this->returnValue($stubPackage));
     }
     // Triggers initialization
     $packages = $repository->getPackages();
     // Final sanity check, ensure the correct number of packages were added.
     $this->assertCount(count($expected), $packages);
 }
Ejemplo n.º 3
0
    /**
     * @dataProvider getIntegrationTests
     */
    public function testIntegration($file, $message, $condition, $composerConfig, $lock, $installed, $run, $expectLock, $expectOutput, $expect)
    {
        if ($condition) {
            eval('$res = '.$condition.';');
            if (!$res) {
                $this->markTestSkipped($condition);
            }
        }

        $output = null;
        $io = $this->getMock('Composer\IO\IOInterface');
        $io->expects($this->any())
            ->method('write')
            ->will($this->returnCallback(function ($text, $newline) use (&$output) {
                $output .= $text . ($newline ? "\n":"");
            }));

        $composer = FactoryMock::create($io, $composerConfig);

        $jsonMock = $this->getMockBuilder('Composer\Json\JsonFile')->disableOriginalConstructor()->getMock();
        $jsonMock->expects($this->any())
            ->method('read')
            ->will($this->returnValue($installed));
        $jsonMock->expects($this->any())
            ->method('exists')
            ->will($this->returnValue(true));

        $repositoryManager = $composer->getRepositoryManager();
        $repositoryManager->setLocalRepository(new InstalledFilesystemRepositoryMock($jsonMock));

        $lockJsonMock = $this->getMockBuilder('Composer\Json\JsonFile')->disableOriginalConstructor()->getMock();
        $lockJsonMock->expects($this->any())
            ->method('read')
            ->will($this->returnValue($lock));
        $lockJsonMock->expects($this->any())
            ->method('exists')
            ->will($this->returnValue(true));

        if ($expectLock) {
            $actualLock = array();
            $lockJsonMock->expects($this->atLeastOnce())
                ->method('write')
                ->will($this->returnCallback(function ($hash, $options) use (&$actualLock) {
                    // need to do assertion outside of mock for nice phpunit output
                    // so store value temporarily in reference for later assetion
                    $actualLock = $hash;
                }));
        }

        $locker = new Locker($lockJsonMock, $repositoryManager, $composer->getInstallationManager(), md5(json_encode($composerConfig)));
        $composer->setLocker($locker);

        $eventDispatcher = $this->getMockBuilder('Composer\Script\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('dev'));

            return $installer->run() ? 0 : 1;
        });

        $application->get('update')->setCode(function ($input, $output) use ($installer) {
            $installer
                ->setDevMode($input->getOption('dev'))
                ->setUpdate(true)
                ->setUpdateWhitelist($input->getArgument('packages'));

            return $installer->run() ? 0 : 1;
        });

        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(0, $result, $output . stream_get_contents($appOutput));

        if ($expectLock) {
            unset($actualLock['hash']);
            unset($actualLock['_readme']);
            $this->assertEquals($expectLock, $actualLock);
        }

        $installationManager = $composer->getInstallationManager();
        $this->assertSame($expect, implode("\n", $installationManager->getTrace()));

        if ($expectOutput) {
            $this->assertEquals($expectOutput, $output);
        }
    }
Ejemplo n.º 4
0
 /**
  * @dataProvider getIntegrationTests
  */
 public function testIntegration($file, $message, $condition, $composerConfig, $lock, $installed, $run, $expectLock, $expectOutput, $expect, $expectResult)
 {
     if ($condition) {
         eval('$res = ' . $condition . ';');
         if (!$res) {
             $this->markTestSkipped($condition);
         }
     }
     $io = new BufferIO('', OutputInterface::VERBOSITY_NORMAL, new OutputFormatter(false));
     // Prepare for exceptions
     if (!is_int($expectResult)) {
         $normalizedOutput = rtrim(str_replace("\n", PHP_EOL, $expect));
         $this->setExpectedException($expectResult, $normalizedOutput);
     }
     // Create Composer mock object according to configuration
     $composer = FactoryMock::create($io, $composerConfig);
     $this->tempComposerHome = $composer->getConfig()->get('home');
     $jsonMock = $this->getMockBuilder('Composer\\Json\\JsonFile')->disableOriginalConstructor()->getMock();
     $jsonMock->expects($this->any())->method('read')->will($this->returnValue($installed));
     $jsonMock->expects($this->any())->method('exists')->will($this->returnValue(true));
     $repositoryManager = $composer->getRepositoryManager();
     $repositoryManager->setLocalRepository(new InstalledFilesystemRepositoryMock($jsonMock));
     $lockJsonMock = $this->getMockBuilder('Composer\\Json\\JsonFile')->disableOriginalConstructor()->getMock();
     $lockJsonMock->expects($this->any())->method('read')->will($this->returnValue($lock));
     $lockJsonMock->expects($this->any())->method('exists')->will($this->returnValue(true));
     if ($expectLock) {
         $actualLock = array();
         $lockJsonMock->expects($this->atLeastOnce())->method('write')->will($this->returnCallback(function ($hash, $options) use(&$actualLock) {
             // 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();
     $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);
     // Shouldn't check output and results if an exception was expected by this point
     if (!is_int($expectResult)) {
         return;
     }
     $output = str_replace("\r", '', $io->getOutput());
     $this->assertEquals($expectResult, $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->assertStringMatchesFormat(rtrim($expectOutput), rtrim($output));
     }
 }
Ejemplo n.º 5
0
 /**
  * @dataProvider getIntegrationTests
  */
 public function testIntegration($file, $message, $condition, $composerConfig, $lock, $installed, $installedDev, $run, $expect)
 {
     if ($condition) {
         eval('$res = ' . $condition . ';');
         if (!$res) {
             $this->markTestSkipped($condition);
         }
     }
     $output = null;
     $io = $this->getMock('Composer\\IO\\IOInterface');
     $io->expects($this->any())->method('write')->will($this->returnCallback(function ($text, $newline) use(&$output) {
         $output .= $text . ($newline ? "\n" : "");
     }));
     $composer = FactoryMock::create($io, $composerConfig);
     $jsonMock = $this->getMockBuilder('Composer\\Json\\JsonFile')->disableOriginalConstructor()->getMock();
     $jsonMock->expects($this->any())->method('read')->will($this->returnValue($installed));
     $jsonMock->expects($this->any())->method('exists')->will($this->returnValue(true));
     $devJsonMock = $this->getMockBuilder('Composer\\Json\\JsonFile')->disableOriginalConstructor()->getMock();
     $devJsonMock->expects($this->any())->method('read')->will($this->returnValue($installedDev));
     $devJsonMock->expects($this->any())->method('exists')->will($this->returnValue(true));
     $repositoryManager = $composer->getRepositoryManager();
     $repositoryManager->setLocalRepository(new InstalledFilesystemRepositoryMock($jsonMock));
     $repositoryManager->setLocalDevRepository(new InstalledFilesystemRepositoryMock($devJsonMock));
     $lockJsonMock = $this->getMockBuilder('Composer\\Json\\JsonFile')->disableOriginalConstructor()->getMock();
     $lockJsonMock->expects($this->any())->method('read')->will($this->returnValue($lock));
     $lockJsonMock->expects($this->any())->method('exists')->will($this->returnValue(true));
     $locker = new Locker($lockJsonMock, $repositoryManager, $composer->getInstallationManager(), md5(json_encode($composerConfig)));
     $composer->setLocker($locker);
     $autoloadGenerator = $this->getMock('Composer\\Autoload\\AutoloadGenerator');
     $installer = Installer::create($io, $composer, null, $autoloadGenerator);
     $application = new Application();
     $application->get('install')->setCode(function ($input, $output) use($installer) {
         $installer->setDevMode($input->getOption('dev'));
         return $installer->run();
     });
     $application->get('update')->setCode(function ($input, $output) use($installer) {
         $installer->setDevMode($input->getOption('dev'))->setUpdate(true)->setUpdateWhitelist($input->getArgument('packages'));
         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(0, $result, $output . stream_get_contents($appOutput));
     $installationManager = $composer->getInstallationManager();
     $this->assertSame($expect, implode("\n", $installationManager->getTrace()));
 }