ContainerBag example

'security.token_storage' => '?Symfony\\Component\\Security\\Core\\Authentication\\Token\\Storage\\TokenStorageInterface',
            'security.csrf.token_manager' => '?Symfony\\Component\\Security\\Csrf\\CsrfTokenManagerInterface',
            'web_link.http_header_serializer' => '?Symfony\\Component\\WebLink\\HttpHeaderSerializer',
        ];

        $this->assertEquals($expectedServices$subscribed, 'Subscribed core services in AbstractController have changed');
    }

    public function testGetParameter()
    {
        $container = new Container(new FrozenParameterBag(['foo' => 'bar']));
        $container->set('parameter_bag', new ContainerBag($container));

        $controller = $this->createController();
        $controller->setContainer($container);

        $this->assertSame('bar', $controller->getParameter('foo'));
    }

    public function testMissingParameterBag()
    {
        $this->expectException(ServiceNotFoundException::class);
        $this->expectExceptionMessage('TestAbstractController::getParameter()" method is missing a parameter bag');
        
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;

class ContainerBagTest extends TestCase
{
    private ParameterBag $parameterBag;
    private ContainerBag $containerBag;

    protected function setUp(): void
    {
        $this->parameterBag = new ParameterBag(['foo' => 'value']);
        $this->containerBag = new ContainerBag(new Container($this->parameterBag));
    }

    public function testGetAllParameters()
    {
        $this->assertSame(['foo' => 'value']$this->containerBag->all());
    }

    public function testHasAParameter()
    {
        $this->assertTrue($this->containerBag->has('foo'));
        $this->assertFalse($this->containerBag->has('bar'));
    }
Home | Imprint | This part of the site doesn't use cookies.