Skip to content

Commit 8406ad1

Browse files
committed
Merge branch 'mirror-on-request-fix' into 0.6
2 parents 97d6c9a + d048940 commit 8406ad1

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

src/Controller/ApiController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function packageMetadata(Request $request): Response
8888
$packageName = $request->attributes->getString('package');
8989
$basePackageName = u($packageName)->trimSuffix('~dev')->toString();
9090

91-
if (null === $this->findPackage($basePackageName)) {
91+
if (null === $this->findPackage($basePackageName, create: true)) {
9292
throw $this->createNotFoundException();
9393
}
9494

tests/FunctionalTests/Controller/ApiControllerPublicTest.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22

33
namespace CodedMonkey\Dirigent\Tests\FunctionalTests\Controller;
44

5+
use CodedMonkey\Dirigent\Doctrine\Entity\Package;
6+
use CodedMonkey\Dirigent\Doctrine\Entity\Registry;
7+
use CodedMonkey\Dirigent\Doctrine\Entity\RegistryPackageMirroring;
58
use CodedMonkey\Dirigent\Tests\FunctionalTests\PublicKernel;
9+
use CodedMonkey\Dirigent\Tests\Helper\KernelTestCaseTrait;
10+
use CodedMonkey\Dirigent\Tests\Helper\MockEntityFactoryTrait;
611
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
712
use Symfony\Component\HttpFoundation\Request;
813
use Symfony\Component\HttpFoundation\Response;
914

1015
class ApiControllerPublicTest extends KernelTestCase
1116
{
17+
use KernelTestCaseTrait;
18+
use MockEntityFactoryTrait;
19+
1220
#[\Override]
1321
protected static function getKernelClass(): string
1422
{
@@ -28,7 +36,7 @@ public function testRoot(): void
2836
], $rootData);
2937
}
3038

31-
public function testPackage(): void
39+
public function testPackagMetadata(): void
3240
{
3341
self::bootKernel();
3442

@@ -37,7 +45,7 @@ public function testPackage(): void
3745
$this->assertNotSame([], $packageData);
3846
}
3947

40-
public function testPackageDev(): void
48+
public function testPackageMetadataDev(): void
4149
{
4250
self::bootKernel();
4351

@@ -46,6 +54,37 @@ public function testPackageDev(): void
4654
$this->assertNotSame([], $packageData);
4755
}
4856

57+
public function testPackageMetadataIsNotFound(): void
58+
{
59+
self::bootKernel();
60+
61+
$request = Request::create('/p2/psr/container.json', 'GET');
62+
$response = self::$kernel->handle($request);
63+
64+
$this->assertSame(Response::HTTP_NOT_FOUND, $response->getStatusCode());
65+
}
66+
67+
public function testPackageMetadataIsMirroredOnRequest(): void
68+
{
69+
self::bootKernel();
70+
71+
// Verify the package doesn't already exist
72+
$package = $this->findEntity(Package::class, ['name' => 'psr/container']);
73+
74+
$this->assertNull($package);
75+
76+
// Update the registry so that it allows dynamically adding packages on request from the API
77+
$registry = $this->findEntity(Registry::class, ['name' => 'Packagist']);
78+
$registry->setPackageMirroring(RegistryPackageMirroring::Automatic);
79+
80+
$this->persistEntities($registry);
81+
82+
// Execute the API endpoint
83+
$packageData = $this->requestJson('/p2/psr/container.json', 'GET');
84+
85+
$this->assertNotSame([], $packageData);
86+
}
87+
4988
private function requestJson(...$requestArguments)
5089
{
5190
$request = Request::create(...$requestArguments);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace CodedMonkey\Dirigent\Tests\Helper;
4+
5+
trait KernelTestCaseTrait
6+
{
7+
/**
8+
* @template TServiceClass of object
9+
*
10+
* @param class-string<TServiceClass> $class
11+
*
12+
* @return TServiceClass
13+
*/
14+
protected function getService(string $class, ?string $name = null): object
15+
{
16+
return self::getContainer()->get($name ?: $class);
17+
}
18+
}

0 commit comments

Comments
 (0)