Skip to content

Commit

Permalink
Move composer dependency to dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Lander Vanderstraeten committed Feb 27, 2019
1 parent a23e8db commit 4a45a25
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 229 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"license": "MIT",
"require": {
"php": ">=7.0",
"composer-plugin-api": "~1.0",
"composer/composer": "^1.0",
"composer-plugin-api": "^1.0",
"doctrine/collections": "~1.2",
"gitonomy/gitlib": "^1.0.3",
"monolog/monolog": "~1.16",
Expand All @@ -22,6 +21,7 @@
"symfony/yaml": "~3.0|~4.0"
},
"require-dev": {
"composer/composer": "^1.0",
"friendsofphp/php-cs-fixer": "~2",
"jakub-onderka/php-parallel-lint": "^0.9.2",
"nikic/php-parser": "~2.1",
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');
}
}
19 changes: 6 additions & 13 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
namespace GrumPHP\Console;

use GrumPHP\Configuration\ContainerFactory;
use GrumPHP\Exception\RuntimeException;
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 +170,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 @@ -183,17 +182,11 @@ protected function initializeComposerHelper(): Helper\ComposerHelper
return $this->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);
$composerFile->ensureProjectBinDirInSystemPath();

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

/**
Expand Down
18 changes: 2 additions & 16 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 All @@ -213,7 +199,7 @@ protected function guessGitDir(): string
return rtrim($this->paths()->getRelativePath($topLevel), '/');
}

public function pathValidator($path): bool
public function pathValidator(string $path): string
{
if (!$this->filesystem->exists($path)) {
throw new RuntimeException(sprintf('The path %s could not be found!', $path));
Expand Down
Loading

0 comments on commit 4a45a25

Please sign in to comment.