Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ public function load(array $configs, ContainerBuilder $container): void
$definition->addTag($id, $attributes);
}
$container->setDefinition($service_id, $definition);
$container->registerAliasForArgument($service_id, self::class, $name . 'NestedTokenBuilder');
$container->registerAliasForArgument(
$service_id,
NestedTokenBuilderService::class,
$name . 'NestedTokenBuilder'
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public function load(array $configs, ContainerBuilder $container): void
$definition->addTag($id, $attributes);
}
$container->setDefinition($service_id, $definition);
$container->registerAliasForArgument($service_id, self::class, $name . 'NestedTokenLoader');
$container->registerAliasForArgument(
$service_id,
NestedTokenLoaderService::class,
$name . 'NestedTokenLoader'
);
}
}

Expand Down
83 changes: 83 additions & 0 deletions tests/Bundle/JoseFramework/Functional/AutowiringAliasesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Jose\Tests\Bundle\JoseFramework\Functional;

use Jose\Bundle\JoseFramework\JoseFrameworkBundle;
use Jose\Component\NestedToken\NestedTokenBuilder;
use Jose\Component\NestedToken\NestedTokenLoader;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Yaml\Yaml;
use function explode;
use function is_a;
use function iterator_to_array;
use function sprintf;
use function str_contains;
use function str_starts_with;

/**
* The bundle registers an autowiring alias for every service it creates from the configuration. The nested token
* sources used to alias their own class - the class of the configuration source - instead of the class of the service
* they create, so nothing could ever be autowired through those aliases.
*
* @internal
*/
final class AutowiringAliasesTest extends TestCase
{
#[Test]
#[DataProvider('autowiringAliases')]
public function theAliasedServiceIsAnInstanceOfTheAliasedType(
string $type,
string $serviceId,
string $class
): void {
static::assertTrue(
is_a($class, $type, true),
sprintf('The service "%s" is a "%s" and cannot be autowired as a "%s".', $serviceId, $class, $type)
);
}

#[Test]
public function theNestedTokenServicesAreAliasedWithTheClassOfTheServiceTheyCreate(): void
{
$aliases = iterator_to_array(self::autowiringAliases());

static::assertArrayHasKey(
NestedTokenLoader::class . ' $nestedTokenLoader1NestedTokenLoader',
$aliases
);
static::assertArrayHasKey(
NestedTokenBuilder::class . ' $nestedTokenBuilder1NestedTokenBuilder',
$aliases
);
}

/**
* @return iterable<string, array{string, string, string}>
*/
public static function autowiringAliases(): iterable
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
(new JoseFrameworkBundle())->getContainerExtension()
->load([Yaml::parseFile(__DIR__ . '/../config/config_test.yml')['jose']], $container);

foreach ($container->getAliases() as $id => $alias) {
if (str_starts_with($id, '.') || ! str_contains($id, ' $')) {
continue;
}
$serviceId = (string) $alias;

yield $id => [
explode(' $', $id, 2)[0],
$serviceId,
$container->findDefinition($serviceId)
->getClass() ?? '',
];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Jose\Bundle\JoseFramework\Services\NestedTokenBuilderFactory;
use Jose\Component\Core\JWK;
use Jose\Component\NestedToken\NestedTokenBuilder;
use Jose\Tests\Bundle\JoseFramework\TestBundle\Service\NestedTokenServiceConsumer;
use Jose\Tests\Bundle\JoseFramework\WebTestCase;
use PHPUnit\Framework\Attributes\Test;

Expand Down Expand Up @@ -45,6 +46,23 @@ public static function theNestedTokenBuilderFromTheConfigurationHelperIsAvailabl
static::assertTrue($container->has('jose.nested_token_builder.nested_token_builder_2'));
}

#[Test]
public static function theNestedTokenBuilderFromTheConfigurationCanBeAutowired(): void
{
static::ensureKernelShutdown();
$client = static::createClient();
$container = $client->getContainer();
static::assertNotNull($container);

/** @var NestedTokenServiceConsumer $consumer */
$consumer = $container->get(NestedTokenServiceConsumer::class);

static::assertSame(
$container->get('jose.nested_token_builder.nested_token_builder_1'),
$consumer->getBuilder()
);
}

#[Test]
public static function aNestedTokenCanBeSignedAndEncryptedUsingTheServiceCreatedFromTheConfiguration(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Jose\Component\Core\JWK;
use Jose\Component\Core\JWKSet;
use Jose\Component\NestedToken\NestedTokenLoader;
use Jose\Tests\Bundle\JoseFramework\TestBundle\Service\NestedTokenServiceConsumer;
use Jose\Tests\Bundle\JoseFramework\WebTestCase;
use PHPUnit\Framework\Attributes\Test;

Expand Down Expand Up @@ -46,6 +47,23 @@ public static function theNestedTokenLoaderFromTheConfigurationHelperIsAvailable
static::assertTrue($container->has('jose.nested_token_loader.nested_token_loader_2'));
}

#[Test]
public static function theNestedTokenLoaderFromTheConfigurationCanBeAutowired(): void
{
static::ensureKernelShutdown();
$client = static::createClient();
$container = $client->getContainer();
static::assertNotNull($container);

/** @var NestedTokenServiceConsumer $consumer */
$consumer = $container->get(NestedTokenServiceConsumer::class);

static::assertSame(
$container->get('jose.nested_token_loader.nested_token_loader_1'),
$consumer->getLoader()
);
}

#[Test]
public static function aNestedTokenCanBeDecryptedAndVerifiedUsingTheServiceCreatedFromTheConfiguration(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Jose\Tests\Bundle\JoseFramework\TestBundle\Checker\CustomChecker;
use Jose\Tests\Bundle\JoseFramework\TestBundle\Service\NestedTokenServiceConsumer;
use Psr\Clock\ClockInterface;
use Symfony\Component\Clock\NativeClock;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
Expand All @@ -25,4 +26,8 @@
$container->set(ClockInterface::class)
->class(NativeClock::class)
;

$container->set(NestedTokenServiceConsumer::class)
->public()
;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Jose\Tests\Bundle\JoseFramework\TestBundle\Service;

use Jose\Component\NestedToken\NestedTokenBuilder;
use Jose\Component\NestedToken\NestedTokenLoader;

/**
* Receives the nested token services of the configuration through the autowiring aliases registered by the bundle. The
* container cannot be compiled unless those aliases refer to the classes of the services.
*/
final readonly class NestedTokenServiceConsumer
{
public function __construct(
private NestedTokenLoader $nestedTokenLoader1NestedTokenLoader,
private NestedTokenBuilder $nestedTokenBuilder1NestedTokenBuilder
) {
}

public function getLoader(): NestedTokenLoader
{
return $this->nestedTokenLoader1NestedTokenLoader;
}

public function getBuilder(): NestedTokenBuilder
{
return $this->nestedTokenBuilder1NestedTokenBuilder;
}
}