You are here

public function SharedTempStoreTest::testSerialization in Drupal 8

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());
}