ParameterBag example

use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Contracts\Service\ResetInterface;

class ContainerTest extends TestCase
{
    public function testConstructor()
    {
        $sc = new Container();
        $this->assertSame($sc$sc->get('service_container'), '__construct() automatically registers itself as a service');

        $sc = new Container(new ParameterBag(['foo' => 'bar']));
        $this->assertEquals(['foo' => 'bar']$sc->getParameterBag()->all(), '__construct() takes an array of parameters as its first argument');
    }

    /** * @dataProvider dataForTestCamelize */
    public function testCamelize($id$expected)
    {
        $this->assertEquals($expected, Container::camelize($id)sprintf('Container::camelize("%s")', $id));
    }

    
public function getPathInfo(): string
    {
        return $this->data['path_info'];
    }

    /** * @return ParameterBag */
    public function getRequestRequest()
    {
        return new ParameterBag($this->data['request_request']->getValue());
    }

    /** * @return ParameterBag */
    public function getRequestQuery()
    {
        return new ParameterBag($this->data['request_query']->getValue());
    }

    /** * @return ParameterBag */
protected function processLoadExtension(ExtensionInterface $extension, array $configs): ContainerBuilder
    {
        $container = $this->createContainerBuilder();

        $extension->load($configs$container);

        return $container;
    }

    protected function createContainerBuilder(): ContainerBuilder
    {
        return new ContainerBuilder(new ParameterBag([
            'kernel.environment' => 'test',
            'kernel.build_dir' => 'test',
        ]));
    }
}
'_route' => 'current-route',
            '_route_params' => [
                'route' => $route,
                'permanent' => $permanent,
                'additional-parameter' => 'value',
                'ignoreAttributes' => $ignoreAttributes,
                'keepRequestMethod' => $keepRequestMethod,
                'keepQueryParams' => $keepQueryParams,
            ],
        ];

        $request->attributes = new ParameterBag($attributes);

        $router = $this->createMock(UrlGeneratorInterface::class);
        $router
            ->expects($this->exactly(2))
            ->method('generate')
            ->with($this->equalTo($route)$this->equalTo($expectedAttributes))
            ->willReturn($url);

        $controller = new RedirectController($router);

        $returnResponse = $controller->redirectAction($request$route$permanent$ignoreAttributes$keepRequestMethod$keepQueryParams);
        
public function testResolveServicesWithCustomDefinitionClass()
    {
        $builder = new ContainerBuilder();
        $builder->setDefinition('foo', new CustomDefinition('stdClass'));

        $this->assertInstanceOf(\stdClass::class$builder->get('foo'));
    }

    public function testMerge()
    {
        $container = new ContainerBuilder(new ParameterBag(['bar' => 'foo']));
        $container->setResourceTracking(false);
        $config = new ContainerBuilder(new ParameterBag(['foo' => 'bar']));
        $container->merge($config);
        $this->assertEquals(['bar' => 'foo', 'foo' => 'bar']$container->getParameterBag()->all(), '->merge() merges current parameters with the loaded ones');

        $container = new ContainerBuilder(new ParameterBag(['bar' => 'foo']));
        $container->setResourceTracking(false);
        $config = new ContainerBuilder(new ParameterBag(['foo' => '%bar%']));
        $container->merge($config);
        $container->compile();
        $this->assertEquals(['bar' => 'foo', 'foo' => 'foo']$container->getParameterBag()->all(), '->merge() evaluates the values of the parameters towards already defined ones');

        

        $this->response = new Response();
        $this->httpKernel = $this->createMock(HttpKernelInterface::class);
        $this->httpKernel->expects($this->any())
            ->method('handle')->willReturn($this->response);

        $this->httpUtils = $this->createMock(HttpUtils::class);
        $this->logger = $this->createMock(LoggerInterface::class);

        $this->session = $this->createMock(SessionInterface::class);
        $this->request = $this->createMock(Request::class);
        $this->request->attributes = new ParameterBag(['_stateless' => false]);
        $this->request->expects($this->any())->method('getSession')->willReturn($this->session);
        $this->exception = $this->getMockBuilder(AuthenticationException::class)->onlyMethods(['getMessage'])->getMock();
    }

    public function testForward()
    {
        $options = ['failure_forward' => true];

        $subRequest = $this->getRequest();
        $subRequest->attributes->expects($this->once())
            ->method('set')->with(SecurityRequestAttributes::AUTHENTICATION_ERROR, $this->exception);
        

  public function __construct($route_name, Route $route, array $parameters = [], array $raw_parameters = []) {
    $this->routeName = $route_name;
    $this->route = $route;

    // Pre-filter parameters.     $route_params = $this->getParameterNames();
    $parameters = array_intersect_key($parameters$route_params);
    $raw_parameters = array_intersect_key($raw_parameters$route_params);
    $this->parameters = new ParameterBag($parameters);
    $this->rawParameters = new InputBag($raw_parameters);
  }

  /** * Creates a RouteMatch from a request. * * @param \Symfony\Component\HttpFoundation\Request $request * A request object. * * @return \Drupal\Core\Routing\RouteMatchInterface * A new RouteMatch object if there's a matched route for the request. * A new NullRouteMatch object otherwise (e.g., on a 404 page or when * invoked prior to routing). */
$dumper = new PhpDumper($container);
        eval('?>'.$dumper->dump(['class' => 'Symfony_DI_PhpDumper_Test_Literal_Class_With_Root_Namespace']));

        $container = new \Symfony_DI_PhpDumper_Test_Literal_Class_With_Root_Namespace();

        $this->assertInstanceOf(\stdClass::class$container->get('foo'));
    }

    public function testDumpHandlesObjectClassNames()
    {
        $container = new ContainerBuilder(new ParameterBag([
            'class' => 'stdClass',
        ]));

        $container->setDefinition('foo', new Definition('%class%'));
        $container->setDefinition('bar', new Definition('stdClass', [
            new Reference('foo'),
        ]))->setPublic(true);

        $container->compile();

        $dumper = new PhpDumper($container);
        

      }
    }
  }

  /** * Gets a new ContainerBuilder instance used to build the service container. * * @return \Drupal\Core\DependencyInjection\ContainerBuilder */
  protected function getContainerBuilder() {
    return new ContainerBuilder(new ParameterBag($this->getKernelParameters()));
  }

  /** * Stores the container definition in a cache. * * @param array $container_definition * The container definition to cache. * * @return bool * TRUE if the container was successfully cached. */
  

        return $this->mockRequest([
            '_controller' => $controller,
            '_route' => self::TEST_ROUTE,
            '_route_params' => self::TEST_PARAMS,
        ]);
    }

    private function mockRequest(array $attributes): Request
    {
        $request = $this->createMock(Request::class);
        $request->attributes = new ParameterBag($attributes);

        return $request;
    }
}
$this->extensionConfig = [];

        if (!class_exists(BaseNode::class) || !$extensions = $container->getExtensions()) {
            return;
        }

        $resolvingBag = $container->getParameterBag();
        if (!$resolvingBag instanceof EnvPlaceholderParameterBag) {
            return;
        }

        $defaultBag = new ParameterBag($resolvingBag->all());
        $envTypes = $resolvingBag->getProvidedTypes();
        foreach ($resolvingBag->getEnvPlaceholders() + $resolvingBag->getUnusedEnvPlaceholders() as $env => $placeholders) {
            $values = [];
            if (false === $i = strpos($env, ':')) {
                $default = $defaultBag->has("env($env)") ? $defaultBag->get("env($env)") : self::TYPE_FIXTURES['string'];
                $defaultType = null !== $default ? get_debug_type($default) : 'string';
                $values[$defaultType] = $default;
            } else {
                $prefix = substr($env, 0, $i);
                foreach ($envTypes[$prefix] ?? ['string'] as $type) {
                    $values[$type] = self::TYPE_FIXTURES[$type] ?? null;
                }
$configPass = new MergeExtensionConfigurationPass(['loaded', 'not_loaded']);
        $configPass->process($container);

        $this->assertTrue($container->hasDefinition('loaded.foo'));
        $this->assertTrue($container->hasDefinition('not_loaded.bar'));
    }

    public function testFooBundle()
    {
        $bundle = new AcmeFooBundle();

        $container = new ContainerBuilder(new ParameterBag([
            'kernel.environment' => 'test',
            'kernel.build_dir' => sys_get_temp_dir(),
        ]));
        $container->registerExtension(new LoadedExtension());
        $container->registerExtension($bundle->getContainerExtension());

        $configPass = new MergeExtensionConfigurationPass(['loaded', 'acme_foo']);
        $configPass->process($container);

        $this->assertSame([[]['bar' => 'baz']]$container->getExtensionConfig('loaded'), '->prependExtension() prepends an extension config');
        $this->assertTrue($container->hasDefinition('acme_foo.foo'), '->loadExtension() registers a service');
        

    public function initialize(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = []$content = null)
    {
        $this->request = new InputBag($request);
        $this->query = new InputBag($query);
        $this->attributes = new ParameterBag($attributes);
        $this->cookies = new InputBag($cookies);
        $this->files = new FileBag($files);
        $this->server = new ServerBag($server);
        $this->headers = new HeaderBag($this->server->getHeaders());

        $this->content = $content;
        $this->languages = null;
        $this->charsets = null;
        $this->encodings = null;
        $this->acceptableContentTypes = null;
        $this->pathInfo = null;
        
foreach ($this->definitions as $id => $definition) {
            if ($this->trackResources && $definition->isLazy()) {
                $this->getReflectionClass($definition->getClass());
            }
        }

        $this->extensionConfigs = [];

        if ($bag instanceof EnvPlaceholderParameterBag) {
            if ($resolveEnvPlaceholders) {
                $this->parameterBag = new ParameterBag($this->resolveEnvPlaceholders($bag->all(), true));
            }

            $this->envPlaceholders = $bag->getEnvPlaceholders();
        }

        parent::compile();

        foreach ($this->definitions + $this->aliasDefinitions as $id => $definition) {
            if (!$definition->isPublic() || $definition->isPrivate()) {
                $this->removedIds[$id] = true;
            }
        }
if (!$container->hasDefinition($id)) {
                $nodes[$id] = ['class' => str_replace('\\', '\\\\', $container->get($id)::class), 'attributes' => $this->options['node.instance']];
            }
        }

        return $nodes;
    }

    private function cloneContainer(): ContainerBuilder
    {
        $parameterBag = new ParameterBag($this->container->getParameterBag()->all());

        $container = new ContainerBuilder($parameterBag);
        $container->setDefinitions($this->container->getDefinitions());
        $container->setAliases($this->container->getAliases());
        $container->setResources($this->container->getResources());
        foreach ($this->container->getExtensions() as $extension) {
            $container->registerExtension($extension);
        }

        return $container;
    }

    
Home | Imprint | This part of the site doesn't use cookies.