Skip to content

Commit

Permalink
Merge pull request #57 from davidpeach/fix/25-duplicated-local-packag…
Browse files Browse the repository at this point in the history
…e-requrie

Fix/25 duplicated local package requrie
  • Loading branch information
davidpeach authored Jan 8, 2022
2 parents 2ede95d + 8e20485 commit 6c36e44
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 6 deletions.
Binary file modified bin/manuscript
Binary file not shown.
2 changes: 1 addition & 1 deletion bin/manuscript-entry
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if (file_exists(__DIR__.'/../../../autoload.php')) {
require __DIR__.'/../vendor/autoload.php';
}

$app = new Symfony\Component\Console\Application('Manuscript', '5.0.3');
$app = new Symfony\Component\Console\Application('Manuscript', '5.0.4');
$app->add(new DavidPeach\Manuscript\Commands\InitCommand);
$app->add(new DavidPeach\Manuscript\Commands\CreateCommand);
$app->add(new DavidPeach\Manuscript\Commands\PlayCommand);
Expand Down
40 changes: 36 additions & 4 deletions src/ComposerFileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ public function read(string $pathToFile): array
*/
public function add(string $pathToFile, array $toAdd): void
{
$composerArray = array_merge_recursive(
$this->read(pathToFile: $pathToFile),
$toAdd
);
$composerArray = $this->merge($this->read(pathToFile: $pathToFile), $toAdd);

$composerArray['require'] = (object)$composerArray['require'];

Expand All @@ -47,4 +44,39 @@ public function add(string $pathToFile, array $toAdd): void

file_put_contents(filename: $pathToFile, data: $updatedComposerJson);
}

private function merge($composer, $wantToAdd): array
{
$okayToAdd = [];

foreach ($wantToAdd as $key => $additions) {

if (! array_key_exists(key: $key, array: $composer)) {
$okayToAdd[$key] = $additions;
continue;
}

$existing = array_map(function ($item) use ($key) {
return match ($key) {
'repositories' => $item['url'],
};
}, $composer[$key]);


$adding = array_filter($additions, function ($addition) use ($existing, $key) {
return match ($key) {
'repositories' => !in_array(needle: $addition['url'], haystack: $existing),
};
});

if (! empty($adding)) {
$okayToAdd[$key] = $adding;
}
}

return array_merge_recursive(
$composer,
$okayToAdd
);
}
}
58 changes: 58 additions & 0 deletions tests/Unit/ComposerFileManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace DavidPeach\Manuscript\Tests\Unit;

use DavidPeach\Manuscript\ComposerFileManager;
use DavidPeach\Manuscript\Playgrounds;
use DavidPeach\Manuscript\Tests\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

class ComposerFileManagerTest extends TestCase
{
private string $root;

private Filesystem $fs;

/**
* Clear out the "tests/test-environments" directory before each new test
*/
public function setUp(): void
{
$this->root = realpath(__DIR__ . '/../test-environments/composer-file-manager');

file_put_contents(
$this->root . '/composer.json',
file_get_contents(__DIR__ . '/../test-environments/stubs/composer.json')
);

$this->fs = new Filesystem;

parent::setUp();
}

/** @test */
public function packages_cannot_be_added_to_a_composer_require_multiple_times()
{
$composer = new ComposerFileManager();

$composer->add(
pathToFile: $this->root . '/composer.json',
toAdd: ['repositories' => [
[
'type' => 'path',
'url' => '/some/local/path',
'options' => [
'symlink' => true,
],
]
]]
);

$composerFileData = $composer->read($this->root . '/composer.json');

$this->assertCount(1, $composerFileData['repositories']);

$this->fs->remove($this->root . '/composer.json');
}
}
Empty file.
9 changes: 8 additions & 1 deletion tests/test-environments/stubs/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,12 @@
"name": "David Peach",
"email": "[email protected]"
}
]
],
"repositories": [{
"type": "path",
"url": "/some/local/path",
"options": {
"symlink": true
}
}]
}

0 comments on commit 6c36e44

Please sign in to comment.