Skip to content

Commit

Permalink
Merge pull request #556 from Landerstraeten/composer-dependency
Browse files Browse the repository at this point in the history
Move composer dependency to dev
  • Loading branch information
Landerstraeten authored May 31, 2019
2 parents 361c2df + 677eda2 commit c675400
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 206 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"require": {
"php": "^7.1",
"composer-plugin-api": "~1.0",
"composer/composer": "^1.8.5",
"doctrine/collections": "~1.2",
"gitonomy/gitlib": "^1.0.3",
"monolog/monolog": "~1.16",
Expand All @@ -23,6 +22,7 @@
},
"require-dev": {
"brianium/paratest": "^2.2.0",
"composer/composer": "^1.8.5",
"friendsofphp/php-cs-fixer": "~2",
"jakub-onderka/php-parallel-lint": "^0.9.2",
"nikic/php-parser": "^3.0",
Expand Down
24 changes: 7 additions & 17 deletions spec/Console/Helper/ComposerHelperSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
namespace spec\GrumPHP\Console\Helper;

use Composer\Config;
use Composer\Package\RootPackage;
use GrumPHP\Console\Helper\ComposerHelper;
use GrumPHP\Util\ComposerFile;
use PhpSpec\ObjectBehavior;
use Symfony\Component\Console\Helper\Helper;

class ComposerHelperSpec extends ObjectBehavior
{
function let(Config $config, RootPackage $rootPackage)
function let(ComposerFile $composerFile)
{
$this->beConstructedWith($config, $rootPackage);
$this->beConstructedWith($composerFile);
}

function it_is_initializable()
Expand All @@ -25,23 +25,13 @@ function it_is_a_console_helper()
$this->shouldHaveType(Helper::class);
}

function it_knows_if_the_composer_configuration_is_available()
function it_has_a_composer_file(ComposerFile $composerFile)
{
$this->hasConfiguration()->shouldBe(true);
$this->getComposerFile()->shouldBe($composerFile);
}

function it_has_composer_configuration(Config $config)
function it_has_a_name()
{
$this->getConfiguration()->shouldBe($config);
}

function it_knows_if_the_composer_root_package_is_available()
{
$this->hasRootPackage()->shouldBe(true);
}

function it_has_composer_root_package(RootPackage $rootPackage)
{
$this->getRootPackage()->shouldBe($rootPackage);
$this->getName()->shouldBe(ComposerHelper::HELPER_NAME);
}
}
46 changes: 17 additions & 29 deletions spec/Locator/ConfigurationFileSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace spec\GrumPHP\Locator;

use Composer\Package\PackageInterface;
use GrumPHP\Locator\ConfigurationFile;
use GrumPHP\Util\ComposerFile;
use GrumPHP\Util\Filesystem;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
Expand All @@ -20,77 +20,65 @@ function it_is_initializable()
$this->shouldHaveType(ConfigurationFile::class);
}

function it_should_locate_config_file(Filesystem $filesystem)
function it_should_locate_config_file(Filesystem $filesystem, ComposerFile $composerFile)
{
$filesystem->exists($this->pathArgument('/composer/grumphp.yml'))->willReturn(true);
$filesystem->isAbsolutePath($this->pathArgument('/composer/grumphp.yml'))->willReturn(true);

$this->locate('/composer', null)->shouldMatch($this->pathRegex('/composer/grumphp.yml'));
$this->locate('/composer', $composerFile)->shouldMatch($this->pathRegex('/composer/grumphp.yml'));
}

function it_should_fall_back_on_dist_file(Filesystem $filesystem)
function it_should_fall_back_on_dist_file(Filesystem $filesystem, ComposerFile $composerFile)
{
$filesystem->exists($this->pathArgument('/composer/grumphp.yml'))->willReturn(false);
$filesystem->exists($this->pathArgument('/composer/grumphp.yml.dist'))->willReturn(true);
$filesystem->isAbsolutePath($this->pathArgument('/composer/grumphp.yml.dist'))->willReturn(true);

$this->locate('/composer', null)->shouldMatch($this->pathRegex('/composer/grumphp.yml.dist'));
$this->locate('/composer', $composerFile)->shouldMatch($this->pathRegex('/composer/grumphp.yml.dist'));
}

function it_should_use_the_config_file_configured_in_the_composer_file(Filesystem $filesystem, PackageInterface $package)
function it_should_use_the_config_default_path_configured_in_the_composer_file(Filesystem $filesystem, ComposerFile $composerFile)
{
$package->getExtra()->willReturn([
'grumphp' => [
'config-default-path' => '/composer/exotic/path/grumphp.yml'
]
]);
$composerFile->getConfigDefaultPath()->willReturn('/composer/exotic/path/grumphp.yml');

$filesystem->exists($this->pathArgument('/composer/grumphp.yml'))->willReturn(true);
$filesystem->exists('/composer/exotic/path/grumphp.yml')->willReturn(true);
$filesystem->isAbsolutePath('/composer/exotic/path/grumphp.yml')->willReturn(true);

$this->locate('/composer', $package)->shouldBe('/composer/exotic/path/grumphp.yml');
$this->locate('/composer', $composerFile)->shouldBe('/composer/exotic/path/grumphp.yml');
}

function it_should_use_the_config_file_configured_in_the_composer_file_and_fall_back_on_dist(Filesystem $filesystem, PackageInterface $package)
function it_should_use_the_config_file_configured_in_the_composer_file_and_fall_back_on_dist(Filesystem $filesystem, ComposerFile $composerFile)
{
$package->getExtra()->willReturn([
'grumphp' => [
'config-default-path' => '/composer/exotic/path/grumphp.yml'
]
]);
$composerFile->getConfigDefaultPath()->willReturn('/composer/exotic/path/grumphp.yml');

$filesystem->exists($this->pathArgument('/composer/grumphp.yml'))->willReturn(true);
$filesystem->exists('/composer/exotic/path/grumphp.yml')->willReturn(false);
$filesystem->exists('/composer/exotic/path/grumphp.yml.dist')->willReturn(true);
$filesystem->isAbsolutePath('/composer/exotic/path/grumphp.yml.dist')->willReturn(true);

$this->locate('/composer', $package)->shouldBe('/composer/exotic/path/grumphp.yml.dist');
$this->locate('/composer', $composerFile)->shouldBe('/composer/exotic/path/grumphp.yml.dist');
}

function it_should_always_return_absolute_paths(Filesystem $filesystem, PackageInterface $package)
function it_should_always_return_absolute_paths(Filesystem $filesystem, ComposerFile $composerFile)
{
$package->getExtra()->willReturn([
'grumphp' => [
'config-default-path' => 'exotic/path/grumphp.yml'
]
]);
$composerFile->getConfigDefaultPath()->willReturn('exotic/path/grumphp.yml');

$filesystem->exists($this->pathArgument('/composer/grumphp.yml'))->willReturn(true);
$filesystem->exists($this->pathArgument('exotic/path/grumphp.yml'))->willReturn(true);
$filesystem->isAbsolutePath($this->pathArgument('exotic/path/grumphp.yml'))->willReturn(false);

$this->locate('/composer', $package)->shouldMatch($this->pathRegex('/composer/exotic/path/grumphp.yml'));
$this->locate('/composer', $composerFile)->shouldMatch($this->pathRegex('/composer/exotic/path/grumphp.yml'));
}

function it_should_locate_config_file_on_empty_composer_configuration(Filesystem $filesystem, PackageInterface $package)
function it_should_locate_config_file_on_empty_composer_configuration(Filesystem $filesystem, ComposerFile $composerFile)
{
$package->getExtra()->willReturn([]);
$composerFile->getConfigDefaultPath()->willReturn(null);

$filesystem->exists($this->pathArgument('/composer/grumphp.yml'))->willReturn(true);
$filesystem->isAbsolutePath($this->pathArgument('/composer/grumphp.yml'))->willReturn(true);

$this->locate('/composer', $package)->shouldMatch($this->pathRegex('/composer/grumphp.yml'));
$this->locate('/composer', $composerFile)->shouldMatch($this->pathRegex('/composer/grumphp.yml'));
}

private function pathRegex($expected)
Expand Down
75 changes: 75 additions & 0 deletions spec/Util/ComposerFileSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace spec\GrumPHP\Util;

use GrumPHP\Util\ComposerFile;
use PhpSpec\ObjectBehavior;

class ComposerFileSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->beConstructedWith([
'config' => [
'sort-packages' => true,
]
]);
$this->shouldHaveType(ComposerFile::class);
}

function it_should_create_itself_from_a_composer_file_location()
{
$this->beConstructedThrough('createFrom', ['composer.json']);
$this->ensureProjectBinDirInSystemPath()->shouldBe(true);
}

function it_should_have_a_default_bin_dir()
{
$this->beConstructedWith([
'config' => [
'sort-packages' => true,
]
]);

$this->getBinDir()->shouldBe('vendor/bin');
}

function it_should_have_a_custom_bin_dir()
{
$this->beConstructedWith([
'config' => [
'sort-packages' => true,
'bin-dir' => 'bin'
]
]);

$this->getBinDir()->shouldBe('bin');
}

function it_should_have_a_default_config_path()
{
$this->beConstructedWith([
'config' => [
'sort-packages' => true,
]
]);

$this->getConfigDefaultPath()->shouldBe(null);
}

function it_should_have_a_custom_config_path()
{
$this->beConstructedWith([
'config' => [
'sort-packages' => true,
],
'extra' => [
'grumphp' => [
'config-default-path' => 'some/folder'
]
]
]);

$this->getConfigDefaultPath()->shouldBe('some/folder');
}
}
20 changes: 9 additions & 11 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace GrumPHP\Console;

use GrumPHP\Configuration\ContainerFactory;
use GrumPHP\Exception\RuntimeException;
use GrumPHP\Exception\FileNotFoundException;
use GrumPHP\IO\ConsoleIO;
use GrumPHP\Locator\ConfigurationFile;
use GrumPHP\Util\Composer;
use GrumPHP\Util\ComposerFile;
use GrumPHP\Util\Filesystem;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
Expand Down Expand Up @@ -171,7 +171,7 @@ protected function getDefaultConfigPath(): string
$locator = new ConfigurationFile($this->filesystem);
$this->configDefaultPath = $locator->locate(
getcwd(),
$this->initializeComposerHelper()->getRootPackage()
$this->initializeComposerHelper()->getComposerFile()
);

return $this->configDefaultPath;
Expand All @@ -184,16 +184,14 @@ protected function initializeComposerHelper(): Helper\ComposerHelper
}

try {
$composerFile = getcwd().DIRECTORY_SEPARATOR.'composer.json';
$configuration = Composer::loadConfiguration();
Composer::ensureProjectBinDirInSystemPath($configuration->get('bin-dir'));
$rootPackage = Composer::loadRootPackageFromJson($composerFile, $configuration);
} catch (RuntimeException $e) {
$configuration = null;
$rootPackage = null;
$composerFileLocation = getcwd().DIRECTORY_SEPARATOR.'composer.json';
$composerFile = ComposerFile::createFrom($composerFileLocation);
} catch (FileNotFoundException $exception) {
$composerFile = ComposerFile::createEmpty();
}
$composerFile->ensureProjectBinDirInSystemPath();

return $this->composerHelper = new Helper\ComposerHelper($configuration, $rootPackage);
return $this->composerHelper = new Helper\ComposerHelper($composerFile);
}

/**
Expand Down
16 changes: 1 addition & 15 deletions src/Console/Command/ConfigureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace GrumPHP\Console\Command;

use Composer\Config;
use Exception;
use Gitonomy\Git\Repository;
use GrumPHP\Configuration\GrumPHP;
Expand Down Expand Up @@ -183,22 +182,9 @@ protected function writeConfiguration(array $configuration): bool
return false;
}

/**
* Make a guess to the bin dir.
*/
protected function guessBinDir(): string
{
$defaultBinDir = $this->config->getBinDir();
if (!$this->composer()->hasConfiguration()) {
return $defaultBinDir;
}

$config = $this->composer()->getConfiguration();
if (!$config->has('bin-dir')) {
return $defaultBinDir;
}

return $config->get('bin-dir', Config::RELATIVE_PATHS);
return $this->composer()->getComposerFile()->getBinDir();
}

protected function guessGitDir(): string
Expand Down
42 changes: 6 additions & 36 deletions src/Console/Helper/ComposerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,23 @@

namespace GrumPHP\Console\Helper;

use Composer\Config;
use Composer\Package\RootPackageInterface;
use GrumPHP\Util\ComposerFile;
use Symfony\Component\Console\Helper\Helper;

/**
* This class will make the composer configurationa available for the commands.
*
* Class ComposerHelper
*/
class ComposerHelper extends Helper
{
const HELPER_NAME = 'composer';

/**
* @var RootPackageInterface
*/
private $rootPackage;
private $composerFile;

/**
* @var Config
*/
private $configuration;

public function __construct(?Config $configuration = null, ?RootPackageInterface $rootPackage = null)
{
$this->rootPackage = $rootPackage;
$this->configuration = $configuration;
}

public function getRootPackage(): ?RootPackageInterface
{
return $this->rootPackage;
}

public function hasRootPackage(): bool
{
return null !== $this->rootPackage;
}

public function getConfiguration(): ?Config
public function __construct(ComposerFile $composerFile)
{
return $this->configuration;
$this->composerFile = $composerFile;
}

public function hasConfiguration(): bool
public function getComposerFile(): ComposerFile
{
return null !== $this->configuration;
return $this->composerFile;
}

public function getName(): string
Expand Down
Loading

0 comments on commit c675400

Please sign in to comment.