comparison core/modules/system/src/Tests/Cache/CacheTestBase.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children af1871eacc83
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\system\Tests\Cache;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8 * Provides helper methods for cache tests.
9 *
10 * @deprecated Scheduled for removal in Drupal 9.0.0.
11 * Use \Drupal\Tests\system\Functional\Cache\CacheTestBase instead.
12 */
13 abstract class CacheTestBase extends WebTestBase {
14
15 protected $defaultBin = 'render';
16 protected $defaultCid = 'test_temporary';
17 protected $defaultValue = 'CacheTest';
18
19 /**
20 * Checks whether or not a cache entry exists.
21 *
22 * @param $cid
23 * The cache id.
24 * @param $var
25 * The variable the cache should contain.
26 * @param $bin
27 * The bin the cache item was stored in.
28 * @return
29 * TRUE on pass, FALSE on fail.
30 */
31 protected function checkCacheExists($cid, $var, $bin = NULL) {
32 if ($bin == NULL) {
33 $bin = $this->defaultBin;
34 }
35
36 $cached = \Drupal::cache($bin)->get($cid);
37
38 return isset($cached->data) && $cached->data == $var;
39 }
40
41 /**
42 * Asserts that a cache entry exists.
43 *
44 * @param $message
45 * Message to display.
46 * @param $var
47 * The variable the cache should contain.
48 * @param $cid
49 * The cache id.
50 * @param $bin
51 * The bin the cache item was stored in.
52 */
53 protected function assertCacheExists($message, $var = NULL, $cid = NULL, $bin = NULL) {
54 if ($bin == NULL) {
55 $bin = $this->defaultBin;
56 }
57 if ($cid == NULL) {
58 $cid = $this->defaultCid;
59 }
60 if ($var == NULL) {
61 $var = $this->defaultValue;
62 }
63
64 $this->assertTrue($this->checkCacheExists($cid, $var, $bin), $message);
65 }
66
67 /**
68 * Asserts that a cache entry has been removed.
69 *
70 * @param $message
71 * Message to display.
72 * @param $cid
73 * The cache id.
74 * @param $bin
75 * The bin the cache item was stored in.
76 */
77 public function assertCacheRemoved($message, $cid = NULL, $bin = NULL) {
78 if ($bin == NULL) {
79 $bin = $this->defaultBin;
80 }
81 if ($cid == NULL) {
82 $cid = $this->defaultCid;
83 }
84
85 $cached = \Drupal::cache($bin)->get($cid);
86 $this->assertFalse($cached, $message);
87 }
88
89 }