Skip to content

Commit 17258cb

Browse files
committed
Add redirect for packages without any published versions
Signed-off-by: Tim Goudriaan <tim@codedmonkey.com>
1 parent 4ea3bbc commit 17258cb

7 files changed

Lines changed: 85 additions & 15 deletions

File tree

src/Controller/Dashboard/DashboardPackagesInfoController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public function info(string $packageName): Response
3232
$package = $this->packageRepository->findOneBy(['name' => $packageName]);
3333
$version = $package->getLatestVersion();
3434

35+
if (!$version) {
36+
return $this->redirectToRoute('dashboard_packages_versions', ['packageName' => $packageName]);
37+
}
38+
3539
return $this->versionInfo($packageName, $version->getNormalizedVersion());
3640
}
3741

templates/dashboard/packages/_package_header.html.twig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
<ul class="nav nav-pills nav-justified d-grid d-md-flex gap-1 pb-3 mb-3 border-bottom">
66
<li class="nav-item">
7-
{% set packageInfoUrl = path('dashboard_packages_info', {packageName: package.name}) %}
8-
<a {{ (currentPage == 'info' ? attrActive : attr)|raw }} href="{{ packageInfoUrl }}">{{ 'Info'|trans }}</a>
7+
{% if package.versions|length > 0 %}
8+
{% set packageInfoUrl = path('dashboard_packages_info', {packageName: package.name}) %}
9+
<a {{ (currentPage == 'info' ? attrActive : attr)|raw }} href="{{ packageInfoUrl }}">{{ 'Info'|trans }}</a>
10+
{% else %}
11+
<span class="nav-link disabled">{{ 'Info'|trans }}</span>
12+
{% endif %}
913
</li>
1014
<li class="nav-item">
1115
{% set packageVersionsUrl = path('dashboard_packages_versions', {packageName: package.name}) %}

templates/dashboard/packages/package_versions.html.twig

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,35 @@
55
{% block page_content %}
66
{{ include('dashboard/packages/_package_header.html.twig', {currentPage: 'versions'}) }}
77

8-
{{ _self.versionList('Latest versions', package.activeVersions) }}
9-
{{ _self.versionList('Development versions', package.devVersions) }}
10-
{{ _self.versionList('Historical versions', package.historicalVersions) }}
11-
{{ _self.versionList('Development branch versions', package.devBranchVersions) }}
8+
{% if package.versions|length > 0 %}
9+
{{ _self.versionList('Latest versions', package.activeVersions) }}
10+
{{ _self.versionList('Development versions', package.devVersions) }}
11+
{{ _self.versionList('Historical versions', package.historicalVersions) }}
12+
{{ _self.versionList('Development branch versions', package.devBranchVersions) }}
13+
{% else %}
14+
<table class="table datagrid datagrid-empty">
15+
<tbody>
16+
{% for i in 1..14 %}
17+
<tr class="empty-row">
18+
<td><span></span></td>
19+
<td><span></span></td>
20+
<td><span></span></td>
21+
<td><span></span></td>
22+
<td><span></span></td>
23+
<td><span></span></td>
24+
</tr>
25+
26+
{% if 3 == loop.index %}
27+
<tr class="no-results">
28+
<td colspan="100">
29+
{{ 'No published versions found'|trans }}
30+
</td>
31+
</tr>
32+
{% endif %}
33+
{% endfor %}
34+
</tbody>
35+
</table>
36+
{% endif %}
1237
{% endblock %}
1338

1439
{% macro versionList(title, versions) %}

tests/FunctionalTests/Controller/Dashboard/DashboardPackagesControllerTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
use CodedMonkey\Dirigent\Doctrine\Entity\Package;
66
use CodedMonkey\Dirigent\Doctrine\Repository\PackageRepository;
77
use CodedMonkey\Dirigent\Doctrine\Repository\RegistryRepository;
8-
use CodedMonkey\Dirigent\Tests\Helper\TestEntityFactoryTrait;
8+
use CodedMonkey\Dirigent\Tests\Helper\MockEntityFactoryTrait;
99
use CodedMonkey\Dirigent\Tests\Helper\WebTestCaseTrait;
1010
use Doctrine\ORM\EntityManagerInterface;
1111
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1212
use Symfony\Component\HttpFoundation\Response;
1313

1414
class DashboardPackagesControllerTest extends WebTestCase
1515
{
16-
use TestEntityFactoryTrait;
16+
use MockEntityFactoryTrait;
1717
use WebTestCaseTrait;
1818

1919
public function testAddMirroring(): void
@@ -95,11 +95,10 @@ public function testDelete(): void
9595

9696
$entityManager = $this->getService(EntityManagerInterface::class);
9797

98-
$package = $this->createPackageEntity();
99-
100-
$entityManager->persist($package);
101-
$entityManager->flush();
98+
$package = $this->createMockPackage();
99+
$this->persistEntities($package);
102100

101+
// Fetch package id prior to deleting it
103102
$packageId = $package->getId();
104103

105104
$client->request('GET', "/packages/{$package->getName()}/delete");

tests/FunctionalTests/Controller/Dashboard/DashboardPackagesInfoControllerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
namespace CodedMonkey\Dirigent\Tests\FunctionalTests\Controller\Dashboard;
44

55
use CodedMonkey\Dirigent\Doctrine\Repository\PackageRepository;
6+
use CodedMonkey\Dirigent\Tests\Helper\MockEntityFactoryTrait;
67
use CodedMonkey\Dirigent\Tests\Helper\WebTestCaseTrait;
78
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
89
use Symfony\Component\HttpFoundation\Response;
910

1011
class DashboardPackagesInfoControllerTest extends WebTestCase
1112
{
13+
use MockEntityFactoryTrait;
1214
use WebTestCaseTrait;
1315

1416
public function testInfo(): void
@@ -21,6 +23,23 @@ public function testInfo(): void
2123
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
2224
}
2325

26+
public function testInfoRedirectsWithNoVersions(): void
27+
{
28+
$client = static::createClient();
29+
$this->loginUser();
30+
31+
$package = $this->createMockPackage();
32+
$this->persistEntities($package);
33+
34+
$client->request('GET', "/packages/{$package->getName()}");
35+
36+
$this->assertResponseStatusCodeSame(Response::HTTP_FOUND);
37+
38+
$client->followRedirect();
39+
40+
$this->assertSame(sprintf('/packages/%s/versions', $package->getName()), $client->getRequest()->getRequestUri());
41+
}
42+
2443
public function testPackageInfo(): void
2544
{
2645
$client = static::createClient();
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
use CodedMonkey\Dirigent\Doctrine\Entity\Package;
66
use CodedMonkey\Dirigent\Doctrine\Entity\Version;
77
use Composer\Semver\VersionParser;
8+
use Doctrine\ORM\EntityManagerInterface;
89

9-
trait TestEntityFactoryTrait
10+
trait MockEntityFactoryTrait
1011
{
11-
protected function createPackageEntity(): Package
12+
protected function createMockPackage(): Package
1213
{
1314
$package = new Package();
1415
$package->setName(sprintf('%s/%s', uniqid(), uniqid()));
1516

1617
return $package;
1718
}
1819

19-
protected function createVersionEntity(Package $package, string $versionName = '1.0.0'): Version
20+
protected function createMockVersion(Package $package, string $versionName = '1.0.0'): Version
2021
{
2122
$version = new Version();
2223

@@ -29,4 +30,20 @@ protected function createVersionEntity(Package $package, string $versionName = '
2930

3031
return $version;
3132
}
33+
34+
/**
35+
* Persist and flush all given entities.
36+
*
37+
* @param object ...$entities
38+
*/
39+
protected function persistEntities(...$entities): void
40+
{
41+
$entityManager = $this->getService(EntityManagerInterface::class);
42+
43+
foreach ($entities as $entity) {
44+
$entityManager->persist($entity);
45+
}
46+
47+
$entityManager->flush();
48+
}
3249
}

translations/messages.en.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ Providers: Providers
5252
Statistics: Statistics
5353
Suggesters: Suggesters
5454

55+
No published versions found: No published versions found.
56+
5557
# Package labels (Statistics)
5658
Daily installations: Daily installations
5759
Daily installations per version: Daily installations per version

0 commit comments

Comments
 (0)