Skip to content

Commit

Permalink
Enhancement: Implement NormalizePlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 13, 2018
1 parent e63c4a5 commit 26f8dd5
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 2 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
[![Latest Stable Version](https://poser.pugx.org/localheinz/composer-normalize/v/stable)](https://packagist.org/packages/localheinz/composer-normalize)
[![Total Downloads](https://poser.pugx.org/localheinz/composer-normalize/downloads)](https://packagist.org/packages/localheinz/composer-normalize)

Provides a composer plugin for normalizing `composer.json`.

## Installation

Run
Expand All @@ -15,7 +17,30 @@ $ composer global require localheinz/composer-normalize

## Usage

This package comes with the following normalizers:
Run

```
$ composer normalize
```

to normalize `composer.json` in the working directory.

The `NormalizeCommand` provided by the `NormalizePlugin` within this package will

* determine whether a `composer.json` exists
* determine whether a `composer.lock` exists, and if so, whether it is up to date
* pass the content of `composer.json` through a chain of normalizers
* write the normalized content of `composer.json` back to the file
* update the hash in `composer.lock` if it exists and if an update is necessary

## Normalizers

This package makes use of the following normalizers provided by [`localheinz/json-normalizer`](https://github.com/localheinz/json-normalizer).

* [`Localheinz\Json\Normalizer\AutoFormatNormalizer`](https://github.com/localheinz/json-normalizer#autoformatnormalizer)
* [`Localheinz\Json\Normalizer\ChainNormalizer`](https://github.com/localheinz/json-normalizer#chainnormalizer)

Additionally, it provides and makes use of the following normalizers:

* [`Localheinz\Composer\Normalize\Normalizer\ConfigHashNormalizer`](#confighashnormalizer)

Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@
"psr-4": {
"Localheinz\\Composer\\Normalize\\Test\\": "test/"
}
},
"extra": {
"class": "Localheinz\\Composer\\Normalize\\NormalizePlugin"
}
}
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions src/NormalizePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2018 Andreas Möller.
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/localheinz/composer-normalize
*/

namespace Localheinz\Composer\Normalize;

use Composer\Composer;
use Composer\IO;
use Composer\Plugin;
use Localheinz\Json\Normalizer\AutoFormatNormalizer;
use Localheinz\Json\Normalizer\ChainNormalizer;

final class NormalizePlugin implements Plugin\PluginInterface, Plugin\Capable, Plugin\Capability\CommandProvider
{
/**
* @var Composer
*/
private $composer;

/**
* @var IO\IOInterface
*/
private $io;

public function activate(Composer $composer, IO\IOInterface $io)
{
$this->composer = $composer;
$this->io = $io;
}

public function getCapabilities(): array
{
return [
Plugin\Capability\CommandProvider::class => self::class,
];
}

public function getCommands(): array
{
return [
new Command\NormalizeCommand(new AutoFormatNormalizer(new ChainNormalizer(
new Normalizer\ConfigHashNormalizer()
))),
];
}
}
86 changes: 86 additions & 0 deletions test/Unit/NormalizePluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2018 Andreas Möller.
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/localheinz/composer-normalize
*/

namespace Localheinz\Composer\Normalize\Test\Unit;

use Composer\Composer;
use Composer\IO;
use Composer\Plugin;
use Localheinz\Composer\Normalize\Command\NormalizeCommand;
use Localheinz\Composer\Normalize\NormalizePlugin;
use Localheinz\Test\Util\Helper;
use PHPUnit\Framework;

final class NormalizePluginTest extends Framework\TestCase
{
use Helper;

/**
* @dataProvider providerInterfaceName
*
* @param string $interfaceName
*/
public function testImplementsPluginInterface(string $interfaceName)
{
$this->assertClassImplementsInterface($interfaceName, NormalizePlugin::class);
}

public function providerInterfaceName(): \Generator
{
$interfaces = [
Plugin\PluginInterface::class,
Plugin\Capable::class,
Plugin\Capability\CommandProvider::class,
];

foreach ($interfaces as $interface) {
yield $interface => [
$interface,
];
}
}

public function testGetCapabilitiesReturnsCapabilities()
{
$plugin = new NormalizePlugin();

$plugin->activate(
$this->prophesize(Composer::class)->reveal(),
$this->prophesize(IO\IOInterface::class)->reveal()
);

$expected = [
Plugin\Capability\CommandProvider::class => NormalizePlugin::class,
];

$this->assertSame($expected, $plugin->getCapabilities());
}

public function testProvidesNormalizeCommand()
{
$plugin = new NormalizePlugin();

$plugin->activate(
$this->prophesize(Composer::class)->reveal(),
$this->prophesize(IO\IOInterface::class)->reveal()
);

$commands = $plugin->getCommands();

$this->assertCount(1, $commands);

$command = \array_shift($commands);

$this->assertInstanceOf(NormalizeCommand::class, $command);
}
}

0 comments on commit 26f8dd5

Please sign in to comment.