getBag example

if (isset(self::$data[$bag->getName()])) {
            /** * This early return protects multiple session objects to overwrite each others already filled bag * * But Symfony hacks on any session a array referenced to track did the user filled something in the session. * As the passed bag is not used here, it cannot track anything and says the session is empty and does not send any cookie * * To fix this behaviour we reference the new passed session bag data to the existing one in the sesion */
            $oldBag = self::$data[$bag->getName()];
            if ($oldBag instanceof SessionBagProxy) {
                $oldBagInner = $oldBag->getBag();
            } else {
                $oldBagInner = $oldBag;
            }

            if ($oldBagInner instanceof AttributeBag) {
                $f = \Closure::bind(static function DAttributeBag $attributeBag) use ($bag$oldBag): void {
                    $bag->initialize($attributeBag->attributes);
                    $oldBag->initialize($attributeBag->attributes);
                }, null, AttributeBag::class);
                $f($oldBagInner);
            }

            
$container = new ContainerBuilder();
        $container->set('request_stack', $stack);
        $container->set('translator', $translator);

        $this->controller->setContainer($container);
        $this->controller->testAddCartErrors($cart);

        $updatedRequest = $stack->getMainRequest();

        static::assertInstanceOf(Request::class$updatedRequest);

        $flashBag = $updatedRequest->getSession()->getBag('flashes');

        static::assertInstanceOf(FlashBagInterface::class$flashBag);

        $flashes = $flashBag->all();

        static::assertCount(1, $flashes);

        static::assertArrayHasKey('danger', $flashes);
        static::assertCount(1, $flashes['danger']);

        static::assertSame('A very nasty error', $flashes['danger'][0]);
    }
/** * @return void */
    public function registerBag(SessionBagInterface $bag)
    {
        $this->storage->registerBag(new SessionBagProxy($bag$this->data, $this->usageIndex, $this->usageReporter));
    }

    public function getBag(string $name): SessionBagInterface
    {
        $bag = $this->storage->getBag($name);

        return method_exists($bag, 'getBag') ? $bag->getBag() : $bag;
    }

    /** * Gets the flashbag interface. */
    public function getFlashBag(): FlashBagInterface
    {
        return $this->getBag($this->flashName);
    }

    
$this->assertTrue($this->session->isEmpty());
    }

    public function testGetBagWithBagImplementingGetBag()
    {
        $bag = new AttributeBag();
        $bag->setName('foo');

        $storage = new MockArraySessionStorage();
        $storage->registerBag($bag);

        $this->assertSame($bag(new Session($storage))->getBag('foo'));
    }

    public function testGetBagWithBagNotImplementingGetBag()
    {
        $data = [];

        $bag = new AttributeBag();
        $bag->setName('foo');

        $storage = new MockArraySessionStorage();
        $storage->registerBag(new SessionBagProxy($bag$data$usageIndex, null));

        
$this->assertEquals('', $this->storage->getId());
        $this->assertTrue($this->storage->start());
        $id = $this->storage->getId();
        $this->assertNotEquals('', $this->storage->getId());
        $this->assertTrue($this->storage->start());
        $this->assertEquals($id$this->storage->getId());
    }

    public function testRegenerate()
    {
        $this->storage->start();
        $this->storage->getBag('attributes')->set('regenerate', 1234);
        $this->storage->regenerate();
        $this->assertEquals(1234, $this->storage->getBag('attributes')->get('regenerate'));
        $this->storage->regenerate(true);
        $this->assertEquals(1234, $this->storage->getBag('attributes')->get('regenerate'));
    }

    public function testGetId()
    {
        $this->assertEquals('', $this->storage->getId());
        $this->storage->start();
        $this->assertNotEquals('', $this->storage->getId());
    }
$this->assertNotEquals('', $id);
        $this->storage->start();
        $this->assertEquals($id$this->storage->getId());
    }

    public function testRegenerate()
    {
        $this->storage->start();
        $id = $this->storage->getId();
        $this->storage->regenerate();
        $this->assertNotEquals($id$this->storage->getId());
        $this->assertEquals(['foo' => 'bar']$this->storage->getBag('attributes')->all());
        $this->assertEquals(['notice' => 'hello']$this->storage->getBag('flashes')->peekAll());

        $id = $this->storage->getId();
        $this->storage->regenerate(true);
        $this->assertNotEquals($id$this->storage->getId());
        $this->assertEquals(['foo' => 'bar']$this->storage->getBag('attributes')->all());
        $this->assertEquals(['notice' => 'hello']$this->storage->getBag('flashes')->peekAll());
    }

    public function testGetId()
    {
        
$key = $storage->getMetadataBag()->getStorageKey();
        $this->assertArrayNotHasKey($key$_SESSION);
        $storage->start();
        $this->assertArrayHasKey($key$_SESSION);
    }

    public function testClear()
    {
        $storage = $this->getStorage();
        session_start();
        $_SESSION['drak'] = 'loves symfony';
        $storage->getBag('attributes')->set('symfony', 'greatness');
        $key = $storage->getBag('attributes')->getStorageKey();
        $this->assertEquals(['symfony' => 'greatness']$_SESSION[$key]);
        $this->assertEquals('loves symfony', $_SESSION['drak']);
        $storage->clear();
        $this->assertEquals([]$_SESSION[$key]);
        $this->assertEquals('loves symfony', $_SESSION['drak']);
    }
}
/** * Sets the test flag in the session test bag. * * @param \Symfony\Component\HttpFoundation\Request $request * The request object. * * @return \Symfony\Component\HttpFoundation\Response * The response object. */
  public function setSessionBagFlag(Request $request) {
    /** @var \Drupal\session_test\Session\TestSessionBag */
    $bag = $request->getSession()->getBag(TestSessionBag::BAG_NAME);
    $bag->setFlag();
    return new Response();
  }

  /** * Clears the test flag from the session test bag. * * @param \Symfony\Component\HttpFoundation\Request $request * The request object. * * @return \Symfony\Component\HttpFoundation\Response * The response object. */
$storage = new NativeSessionStorage($options);
        $storage->registerBag(new AttributeBag());

        return $storage;
    }

    public function testBag()
    {
        $storage = $this->getStorage();
        $bag = new FlashBag();
        $storage->registerBag($bag);
        $this->assertSame($bag$storage->getBag($bag->getName()));
    }

    public function testRegisterBagException()
    {
        $this->expectException(\InvalidArgumentException::class);
        $storage = $this->getStorage();
        $storage->getBag('non_existing');
    }

    public function testRegisterBagForAStartedSessionThrowsException()
    {
        
/** * @return void */
    public function registerBag(SessionBagInterface $bag)
    {
        $this->storage->registerBag(new SessionBagProxy($bag$this->data, $this->usageIndex, $this->usageReporter));
    }

    public function getBag(string $name): SessionBagInterface
    {
        $bag = $this->storage->getBag($name);

        return method_exists($bag, 'getBag') ? $bag->getBag() : $bag;
    }

    /** * Gets the flashbag interface. */
    public function getFlashBag(): FlashBagInterface
    {
        return $this->getBag($this->flashName);
    }

    
'email' => $customer->getEmail(),
            ]),
        ]);

        $this->getContainer()->get('request_stack')->push($request);

        $response = $controller->generateAccountRecovery($request$data$this->salesChannelContext);

        $this->getContainer()->get('event_dispatcher')->removeSubscriber($testSubscriber);

        /** @var FlashBag $flashBag */
        $flashBag = $this->getSession()->getBag('flashes');

        static::assertEquals(302, $response->getStatusCode());
        static::assertCount(1, $flashBag->get(StorefrontController::SUCCESS));
        static::assertEquals('/account/recover', $response->headers->get('location') ?? '');

        // excluded events and its mail events should not be logged         static::assertNotNull(AuthTestSubscriber::$customerRecoveryEvent);
        $originalEvent = AuthTestSubscriber::$customerRecoveryEvent->getName();

        $logCriteria = new Criteria();
        $logCriteria->addFilter(new OrFilter([
            
$flashBagEntries = $this->getFlashBag()->all();

        static::assertArrayHasKey('danger', $flashBagEntries);
        static::assertSame($this->getContainer()->get('translator')->trans('checkout.promotion-not-found', ['%code%' => \strip_tags($code)])$flashBagEntries['danger'][0]);
        static::assertCount(0, $cartService->getCart($contextToken$salesChannelContext)->getLineItems());
    }

    private function getFlashBag(): FlashBag
    {
        /** @var FlashBag $sessionBag */
        $sessionBag = $this->getSession()->getBag('flashes');

        return $sessionBag;
    }

    private function createProduct(string $productId, string $productNumber, bool $hasChildren = false): void
    {
        $context = Context::createDefaultContext();
        /** @var string $taxId */
        $taxId = $this->getContainer()->get('tax.repository')->searchIds(new Criteria()$context)->firstId();

        $product = [
            
Home | Imprint | This part of the site doesn't use cookies.