function SharedTempStoreTest::testSerialization

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/TempStore/SharedTempStoreTest.php \Drupal\Tests\Core\TempStore\SharedTempStoreTest::testSerialization()
  2. 10 core/tests/Drupal/Tests/Core/TempStore/SharedTempStoreTest.php \Drupal\Tests\Core\TempStore\SharedTempStoreTest::testSerialization()
  3. 11.x core/tests/Drupal/Tests/Core/TempStore/SharedTempStoreTest.php \Drupal\Tests\Core\TempStore\SharedTempStoreTest::testSerialization()

Tests the serialization of a shared temp store.

File

core/tests/Drupal/Tests/Core/TempStore/SharedTempStoreTest.php, line 389

Class

SharedTempStoreTest
@coversDefaultClass \Drupal\Core\TempStore\SharedTempStore @group TempStore

Namespace

Drupal\Tests\Core\TempStore

Code

public function testSerialization() {
    // Add an unserializable request to the request stack. If the tempstore
    // didn't use DependencySerializationTrait, the exception would be thrown
    // when we try to serialize the tempstore.
    $request = $this->prophesize(Request::class);
    $request->willImplement('\\Serializable');
    $request->serialize()
        ->willThrow(new \LogicException('Oops!'));
    $unserializable_request = $request->reveal();
    $this->requestStack
        ->push($unserializable_request);
    $this->requestStack->_serviceId = 'request_stack';
    $container = $this->prophesize(ContainerInterface::class);
    $container->get('request_stack')
        ->willReturn($this->requestStack);
    $container->has('request_stack')
        ->willReturn(TRUE);
    \Drupal::setContainer($container->reveal());
    $store = unserialize(serialize($this->tempStore));
    $this->assertInstanceOf(SharedTempStore::class, $store);
    $request_stack = $this->getObjectAttribute($store, 'requestStack');
    $this->assertEquals($this->requestStack, $request_stack);
    $this->assertSame($unserializable_request, $request_stack->pop());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.