16e0704a95662a4415fd4c26f06a2783e00bd577
[yaffs-website] / web / modules / contrib / ctools / tests / src / Kernel / SerializableTempstoreTest.php
1 <?php
2
3 namespace Drupal\Tests\ctools\Kernel;
4
5 use Drupal\ctools\SerializableTempstore;
6 use Drupal\KernelTests\KernelTestBase;
7 use Symfony\Component\HttpFoundation\Request;
8
9 /**
10  * Tests the serializable tempstore service.
11  *
12  * @group ctools
13  */
14 class SerializableTempstoreTest extends KernelTestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $modules = ['ctools', 'system', 'user'];
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function setUp() {
25     parent::setUp();
26     $this->installSchema('system', ['key_value_expire']);
27   }
28
29   /**
30    * Tests serializing a serializable temp store object.
31    */
32   public function testSerializableTempStore() {
33     $store = $this->container
34       ->get('ctools.serializable.tempstore.factory')
35       ->get('foobar');
36
37     // Add an unserializable request to the request stack. If the tempstore
38     // didn't use DependencySerializationTrait, the exception would be thrown
39     // when we try to serialize the tempstore.
40     $request = $this->prophesize(Request::class);
41     $request->willImplement('\Serializable');
42     $request->serialize()->willThrow(new \LogicException('Not cool, bruh!'));
43     $this->container->get('request_stack')->push($request->reveal());
44
45     $this->assertInstanceOf(SerializableTempstore::class, $store);
46     /** @var SerializableTempstore $store */
47
48     $store = serialize($store);
49     $this->assertInternalType('string', $store);
50     $this->assertNotEmpty($store, 'The tempstore was serialized.');
51
52     $store = unserialize($store);
53     $this->assertInstanceOf(SerializableTempstore::class, $store, 'The tempstore was unserialized.');
54
55     $request_stack = $this->getObjectAttribute($store, 'requestStack');
56     $this->assertSame(
57       $this->container->get('request_stack'),
58       $request_stack,
59       'The request stack was pulled from the container during unserialization.'
60     );
61     $this->assertSame($request->reveal(), $request_stack->pop());
62   }
63
64 }