-
-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[LiveComponent] Allow configuring secret for fingerprints and checksums
Allow to configure a dedicated secret (used in FingerprintCalculator and LiveComonentHydrator) Suggested by #2453 Inspired by #56840
- Loading branch information
Showing
4 changed files
with
145 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/LiveComponent/tests/Unit/DependencyInjection/LiveComponentConfigurationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Symfony\UX\LiveComponent\Tests\Unit\DependencyInjection; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; | ||
use Symfony\Component\Config\Definition\Processor; | ||
use Symfony\UX\LiveComponent\DependencyInjection\LiveComponentExtension; | ||
|
||
class LiveComponentConfigurationTest extends TestCase | ||
{ | ||
public function testDefaultSecret() | ||
{ | ||
$processor = new Processor(); | ||
$config = $processor->processConfiguration(new LiveComponentExtension(), []); | ||
|
||
$this->assertEquals('%kernel.secret%', $config['secret']); | ||
} | ||
|
||
public function testEmptySecretThrows() | ||
{ | ||
$this->expectException(InvalidConfigurationException::class); | ||
$this->expectExceptionMessage('The path "live_component.secret" cannot contain an empty value, but got null.'); | ||
|
||
$processor = new Processor(); | ||
$config = $processor->processConfiguration(new LiveComponentExtension(), [ | ||
'live_component' => [ | ||
'secret' => null, | ||
], | ||
]); | ||
} | ||
|
||
public function testCustomSecret() | ||
{ | ||
$processor = new Processor(); | ||
$config = $processor->processConfiguration(new LiveComponentExtension(), [ | ||
'live_component' => [ | ||
'secret' => 'my_secret', | ||
], | ||
]); | ||
|
||
$this->assertEquals('my_secret', $config['secret']); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/LiveComponent/tests/Unit/DependencyInjection/LiveComponentExtensionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Symfony\UX\LiveComponent\Tests\Unit\DependencyInjection; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; | ||
use Symfony\UX\LiveComponent\DependencyInjection\LiveComponentExtension; | ||
|
||
class LiveComponentExtensionTest extends TestCase | ||
{ | ||
public function testKernelSecretIsUsedByDefault(): void | ||
{ | ||
$container = $this->createContainer(); | ||
$container->registerExtension(new LiveComponentExtension()); | ||
$container->loadFromExtension('live_component', []); | ||
$this->compileContainer($container); | ||
|
||
$this->assertSame('%kernel.secret%', $container->getDefinition('ux.live_component.component_hydrator')->getArgument(4)); | ||
$this->assertSame('%kernel.secret%', $container->getDefinition('ux.live_component.fingerprint_calculator')->getArgument(0)); | ||
} | ||
|
||
public function testCustomSecretIsUsedInDefinition(): void | ||
{ | ||
$container = $this->createContainer(); | ||
$container->registerExtension(new LiveComponentExtension()); | ||
$container->loadFromExtension('live_component', [ | ||
'secret' => 'custom_secret', | ||
]); | ||
$this->compileContainer($container); | ||
|
||
$this->assertSame('custom_secret', $container->getDefinition('ux.live_component.component_hydrator')->getArgument(4)); | ||
$this->assertSame('custom_secret', $container->getDefinition('ux.live_component.fingerprint_calculator')->getArgument(0)); | ||
} | ||
|
||
private function createContainer(): ContainerBuilder | ||
{ | ||
$container = new ContainerBuilder(new ParameterBag([ | ||
'kernel.cache_dir' => __DIR__, | ||
'kernel.project_dir' => __DIR__, | ||
'kernel.charset' => 'UTF-8', | ||
'kernel.debug' => false, | ||
'kernel.bundles' => [], | ||
'kernel.bundles_metadata' => [], | ||
])); | ||
|
||
return $container; | ||
} | ||
|
||
private function compileContainer(ContainerBuilder $container): void | ||
{ | ||
$container->getCompilerPassConfig()->setOptimizationPasses([]); | ||
$container->getCompilerPassConfig()->setRemovingPasses([]); | ||
$container->getCompilerPassConfig()->setAfterRemovingPasses([]); | ||
$container->compile(); | ||
} | ||
} |