You are here

public function SerializableTempstoreTest::testSerializableTempStore in Chaos Tool Suite (ctools) 8.3

Tests serializing a serializable temp store object.

File

tests/src/Kernel/SerializableTempstoreTest.php, line 32

Class

SerializableTempstoreTest
Tests the serializable tempstore service.

Namespace

Drupal\Tests\ctools\Kernel

Code

public function testSerializableTempStore() {
  $store = $this->container
    ->get('ctools.serializable.tempstore.factory')
    ->get('foobar');

  // 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('Not cool, bruh!'));
  $this->container
    ->get('request_stack')
    ->push($request
    ->reveal());
  $this
    ->assertInstanceOf(SerializableTempstore::class, $store);

  /** @var \Drupal\ctools\SerializableTempstore $store */
  $store = serialize($store);
  $this
    ->assertSame('string', gettype($store));
  $this
    ->assertNotEmpty($store, 'The tempstore was serialized.');
  $store = unserialize($store);
  $this
    ->assertInstanceOf(SerializableTempstore::class, $store, 'The tempstore was unserialized.');
  $reflector = new \ReflectionClass($store);
  $property = $reflector
    ->getProperty('requestStack');
  $property
    ->setAccessible(TRUE);
  $request_stack = $property
    ->getValue($store);
  $this
    ->assertSame($this->container
    ->get('request_stack'), $request_stack, 'The request stack was pulled from the container during unserialization.');
  $this
    ->assertSame($request
    ->reveal(), $request_stack
    ->pop());
}