Skip to content

Commit

Permalink
Merge pull request #58 from weierophinney/fix/extra-zf-migration
Browse files Browse the repository at this point in the history
Ensure extra.zf configuration is renamed to extra.laminas
  • Loading branch information
weierophinney committed Jan 5, 2021
2 parents 4f55d2f + fd68291 commit a9f7212
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ matrix:
env:
- COMPAT_VERSION=7.4

before_install:
- composer self-update --1

install:
- composer update $COMPOSER_ARGS
- if [[ $RUN_TESTS != '' ]]; then composer require $RUN_TESTS ; fi
Expand Down
2 changes: 2 additions & 0 deletions src/Command/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Laminas\Migration\DependencyPlugin;
use Laminas\Migration\FileFilter;
use Laminas\Migration\MigrateProject;
use Laminas\Migration\SpecialCase\ComposerJsonExtraZFSpecialCase;
use Laminas\Migration\SpecialCase\ComposerJsonZendFrameworkPackageSpecialCase;
use Laminas\Migration\VendorDirectory;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -247,6 +248,7 @@ private function injectDependencyPlugin($path, $noPluginOption, SymfonyStyle $io
private function migrateProjectFiles($path, callable $filter, SymfonyStyle $io)
{
$migration = new MigrateProject([
new ComposerJsonExtraZFSpecialCase(),
new ComposerJsonZendFrameworkPackageSpecialCase(),
]);
$migration($path, $filter, $io);
Expand Down
44 changes: 44 additions & 0 deletions src/SpecialCase/ComposerJsonExtraZFSpecialCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* @see https://github.com/laminas/laminas-migration for the canonical source repository
* @copyright https://github.com/laminas/laminas-migration/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-migration/blob/master/LICENSE.md New BSD License
*/

namespace Laminas\Migration\SpecialCase;

class ComposerJsonExtraZFSpecialCase implements SpecialCaseInterface
{
public function matches($filename, $content)
{
if (! preg_match('#\bcomposer\.json$#', $filename)) {
return false;
}

$composer = json_decode($content, true);
return isset($composer['extra']['zf']);
}

public function replace($content)
{
$composer = json_decode($content, true);
$composer = $this->updateComposer($composer);

return json_encode($composer, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}

/**
* @return array The updated Composer array.
*/
private function updateComposer(array $composer)
{
$extraZf = $composer['extra']['zf'];
$extraLaminas = isset($composer['extra']['laminas']) ? $composer['extra']['laminas'] : [];

unset($composer['extra']['zf']);
$composer['extra']['laminas'] = array_replace_recursive($extraZf, $extraLaminas);

return $composer;
}
}
32 changes: 32 additions & 0 deletions test/SpecialCase/ComposerJsonExtraZFSpecialCaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* @see https://github.com/laminas/laminas-migration for the canonical source repository
* @copyright https://github.com/laminas/laminas-migration/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-migration/blob/master/LICENSE.md New BSD License
*/

namespace LaminasTest\Migration\SpecialCase;

use Laminas\Migration\SpecialCase\ComposerJsonExtraZFSpecialCase;
use PHPUnit\Framework\TestCase;

class ComposerJsonExtraZFSpecialCaseTest extends TestCase
{
public function testReplacesZendFrameworkPackageWithAppropriatePackagesAtAppropriateConstraint()
{
$specialCase = new ComposerJsonExtraZFSpecialCase();
$file = sprintf('%s/fixtures/extra/composer.json', __DIR__);
$expectedFile = sprintf('%s/fixtures/extra/composer.updated.json', __DIR__);
$contents = file_get_contents($file);
$expectedContents = file_get_contents($expectedFile);
$expected = json_decode($expectedContents, true);

$this->assertTrue($specialCase->matches($file, $contents));

$result = $specialCase->replace($contents);
$replacements = json_decode($result, true);

$this->assertEquals($expected, $replacements);
}
}
5 changes: 5 additions & 0 deletions test/SpecialCase/fixtures/extra/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extra": {
"zf": {}
}
}
5 changes: 5 additions & 0 deletions test/SpecialCase/fixtures/extra/composer.updated.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extra": {
"laminas": {}
}
}

0 comments on commit a9f7212

Please sign in to comment.