Skip to content

Commit

Permalink
Uses config data from Composer object whenever possible on ArchiveCom…
Browse files Browse the repository at this point in the history
…mand

It was previously blindly getting a new instance from the factory thus ignoring what is on Composer object config data.
  • Loading branch information
adrianosferreira committed Feb 10, 2020
1 parent dbcf592 commit 97d077c
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 1 deletion.
1 change: 1 addition & 0 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ return PhpCsFixer\Config::create()
'standardize_not_equals' => true,
'ternary_operator_spaces' => true,
'trailing_comma_in_multiline_array' => true,
'unary_operator_spaces' => true,
))
->setFinder($finder)
;
8 changes: 7 additions & 1 deletion src/Composer/Command/ArchiveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,21 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$config = Factory::createConfig();
$composer = $this->getComposer(false);
$config = null;

if ($composer) {
$config = $composer->getConfig();
$commandEvent = new CommandEvent(PluginEvents::COMMAND, 'archive', $input, $output);
$eventDispatcher = $composer->getEventDispatcher();
$eventDispatcher->dispatch($commandEvent->getName(), $commandEvent);
$eventDispatcher->dispatchScript(ScriptEvents::PRE_ARCHIVE_CMD);
}

if (!$config) {
$config = Factory::createConfig();
}

if (null === $input->getOption('format')) {
$input->setOption('format', $config->get('archive-format'));
}
Expand Down
121 changes: 121 additions & 0 deletions tests/Composer/Test/Command/ArchiveCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

/*
* This file is part of Composer.
*
* (c) Nils Adermann <[email protected]>
* Jordi Boggiano <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Composer\Test\Command;

use Composer\Composer;
use Composer\Config;
use Composer\Factory;
use Composer\Test\TestCase;
use Symfony\Component\Console\Input\ArrayInput;

class ArchiveCommandTest extends TestCase
{
public function testUsesConfigFromComposerObject()
{
$input = new ArrayInput(array());

$output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')
->getMock();

$ed = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
->disableOriginalConstructor()->getMock();

$composer = new Composer;
$config = new Config;
$config->merge(array('config' => array('archive-format' => 'zip')));
$composer->setConfig($config);

$manager = $this->getMockBuilder('Composer\Package\Archiver\ArchiveManager')
->disableOriginalConstructor()->getMock();

$package = $this->getMockBuilder('Composer\Package\RootPackageInterface')
->getMock();

$manager->expects($this->once())->method('archive')
->with($package, 'zip', '.', null, false)->willReturn(getcwd());

$composer->setArchiveManager($manager);
$composer->setEventDispatcher($ed);
$composer->setPackage($package);

$command = $this->getMockBuilder('Composer\Command\ArchiveCommand')
->setMethods(array(
'mergeApplicationDefinition',
'bind',
'getSynopsis',
'initialize',
'isInteractive',
'getComposer',
))->getMock();
$command->expects($this->any())->method('getComposer')
->willReturn($composer);
$command->method('isInteractive')->willReturn(false);

$command->run($input, $output);
}

public function testUsesConfigFromFactoryWhenComposerIsNotDefined()
{
$input = new ArrayInput(array());

$output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')
->getMock();

$ed = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
->disableOriginalConstructor()->getMock();

$composer = new Composer;
$config = Factory::createConfig();

$manager = $this->getMockBuilder('Composer\Package\Archiver\ArchiveManager')
->disableOriginalConstructor()->getMock();

$factory = $this->getMockBuilder('Composer\Factory')->getMock();
$factory->method('createArchiveManager')->willReturn($manager);

$package = $this->getMockBuilder('Composer\Package\RootPackageInterface')
->getMock();

$composer->setArchiveManager($manager);
$composer->setEventDispatcher($ed);
$composer->setPackage($package);

$command = $this->getMockBuilder('Composer\Command\ArchiveCommand')
->setMethods(array(
'mergeApplicationDefinition',
'bind',
'getSynopsis',
'initialize',
'isInteractive',
'getComposer',
'archive',
))->getMock();
$command->expects($this->any())->method('getComposer')
->willReturnOnConsecutiveCalls(null, $composer);
$command->expects($this->any())->method('archive')
->with(
$this->isType('object'),
$config,
null,
null,
'tar',
'.',
null,
false,
null
);
$command->method('isInteractive')->willReturn(false);

$command->run($input, $output);
}
}

0 comments on commit 97d077c

Please sign in to comment.