Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix package dependencies duplication #472

Merged
merged 1 commit into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/Entity/Organization/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,24 +170,35 @@ public function repositoryUrl(): string
}

/**
* @param string[] $encounteredVersions
* @param string[] $encounteredLinks
* @param array<string,bool> $encounteredVersions
* @param array<string,bool> $encounteredLinks
*/
public function syncSuccess(string $name, string $description, string $latestReleasedVersion, array $encounteredVersions, array $encounteredLinks, \DateTimeImmutable $latestReleaseDate): void
{
$this->setName($name);
$this->description = $description;
$this->latestReleasedVersion = $latestReleasedVersion;
$this->latestReleaseDate = $latestReleaseDate;
foreach ($this->versions as $version) {
if (!in_array($version->version(), $encounteredVersions, true)) {
$this->versions->removeElement($version);
foreach ($this->versions as $key => $version) {
if (!isset($encounteredVersions[$version->version()])) {
$this->versions->remove($key);
}
}
foreach ($this->links as $link) {
if (!in_array($link->type().'-'.$link->target(), $encounteredLinks, true)) {
$this->links->removeElement($link);

$uniqueLinks = [];
foreach ($this->links as $key => $link) {
$uniqueKey = $link->type().'-'.$link->target();
if (!isset($encounteredLinks[$uniqueKey])) {
$this->links->remove($key);
continue;
}

if (!isset($uniqueLinks[$uniqueKey])) {
$uniqueLinks[$uniqueKey] = true;
continue;
}

$this->links->remove($key);
}
$this->lastSyncAt = new \DateTimeImmutable();
$this->lastSyncError = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function synchronize(Package $package): void
$link->getPrettyConstraint(),
)
);
$encounteredLinks[] = $type.'-'.$link->getTarget();
$encounteredLinks[$type.'-'.$link->getTarget()] = true;
}
}
}
Expand All @@ -190,7 +190,7 @@ public function synchronize(Package $package): void
$linkDescription,
)
);
$encounteredLinks[] = 'suggests-'.$linkName;
$encounteredLinks['suggests-'.$linkName] = true;
}
}

Expand All @@ -205,7 +205,7 @@ public function synchronize(Package $package): void
)
);

$encounteredVersions[] = $version['prettyVersion'];
$encounteredVersions[$version['prettyVersion']] = true;
}

$package->syncSuccess(
Expand Down
2 changes: 1 addition & 1 deletion templates/component/user.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<div class="dropdown">
<a href="#" data-toggle="dropdown"
class="align-items-center ml-2 btn btn-secondary">
<span class="ml-2 d-none d-sm-block lh-1">
<span class="d-none d-sm-block lh-1">
{% if organization is defined %}
{{ icons.anonymousIcon(organization) }} {{ organization.name }}
{% else %}
Expand Down
4 changes: 2 additions & 2 deletions tests/Doubles/FakePackageSynchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public function synchronize(Package $package): void
$this->name,
$this->description,
$this->latestReleasedVersion,
$encounteredVersions,
$encounteredLinks,
array_flip($encounteredVersions),
array_flip($encounteredLinks),
$this->latestReleaseDate
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/MotherObject/PackageMother.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public static function withOrganizationAndToken(string $type, string $url, strin
}

/**
* @param string[] $unencounteredVersions
* @param string[] $unencounteredLinks
* @param array<string,bool> $unencounteredVersions
* @param array<string,bool> $unencounteredLinks
*/
public static function synchronized(string $name, string $latestVersion, string $url = '', array $unencounteredVersions = [], array $unencounteredLinks = []): Package
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Buddy\Repman\Tests\Unit\Entity;
namespace Buddy\Repman\Tests\Unit\Entity\Organization;

use Buddy\Repman\Entity\Organization\Package;
use Buddy\Repman\Entity\Organization\Package\Link;
Expand Down Expand Up @@ -33,7 +33,7 @@ public function testSyncSuccessRemovesUnencounteredVersions(): void
$this->package->addOrUpdateVersion($version2 = new Version(Uuid::uuid4(), '1.0.1', 'anotherref', 5678, new \DateTimeImmutable(), Version::STABILITY_STABLE));
$this->package->addOrUpdateVersion($version3 = new Version(Uuid::uuid4(), '1.1.0', 'lastref', 6543, new \DateTimeImmutable(), Version::STABILITY_STABLE));

$this->package->syncSuccess('some/package', 'desc', '1.1.0', ['1.0.0', '1.1.0'], [], new \DateTimeImmutable());
$this->package->syncSuccess('some/package', 'desc', '1.1.0', array_flip(['1.0.0', '1.1.0']), [], new \DateTimeImmutable());

self::assertCount(2, $this->package->versions());
self::assertContains($version1, $this->package->versions());
Expand All @@ -52,7 +52,7 @@ public function testSyncSuccessRemovesUnencounteredLinks(): void
'desc',
'1.1.0',
[],
['replaces-buddy-works/testone', 'replaces-buddy-works/testthree'],
array_flip(['replaces-buddy-works/testone', 'replaces-buddy-works/testthree']),
new \DateTimeImmutable()
);

Expand All @@ -62,6 +62,15 @@ public function testSyncSuccessRemovesUnencounteredLinks(): void
self::assertContains($link3, $this->package->links());
}

public function testSyncSuccessRemovesDuplicatedLinks(): void
{
$this->package->addLink(new Link(Uuid::uuid4(), 'requires', 'phpunit/phpunit', '^1.0'));
$this->package->addLink(new Link(Uuid::uuid4(), 'requires', 'phpunit/phpunit', '^1.0'));
$this->package->syncSuccess('some/package', 'desc', '1.1.0', [], array_flip(['requires-phpunit/phpunit']), new \DateTimeImmutable());

self::assertCount(1, $this->package->links());
}

public function testOuathTokenNotFound(): void
{
$this->expectException(\RuntimeException::class);
Expand Down