Debian Codegraph @

tests/phpunit/unit/includes/objectcache/MemcachedBagOStuffTest.php

raw symbols SCIP
1 <?php
2
3 use Wikimedia\ObjectCache\MemcachedBagOStuff;
4 use Wikimedia\ObjectCache\MemcachedPhpBagOStuff;
5
6 /**
7 * @covers \Wikimedia\ObjectCache\MemcachedBagOStuff
8 * @group BagOStuff
9 */
10 class MemcachedBagOStuffTest extends \MediaWikiUnitTestCase {
11 /** @var MemcachedBagOStuff */
12 private $cache;
13
14 protected function setUp(): void {
15 parent::setUp();
16 $this->cache = new MemcachedPhpBagOStuff( [ 'keyspace' => 'test', 'servers' => [] ] );
17 }
18
19 public function testKeyNormalization() {
20 $this->assertEquals(
21 'test:vanilla',
22 $this->cache->makeKey( 'vanilla' )
23 );
24
25 $this->assertEquals(
26 'test:punctuation_marks_are_ok:!@$^&*()',
27 $this->cache->makeKey( 'punctuation_marks_are_ok', '!@$^&*()' )
28 );
29
30 $this->assertEquals(
31 'test:but_spaces:hashes%23:and%0Anewlines:are_not',
32 $this->cache->makeKey( 'but spaces', 'hashes#', "and\nnewlines", 'are_not' )
33 );
34
35 $this->assertEquals(
36 'test:this:key:contains:%F0%9D%95%9E%F0%9D%95%A6%F0%9D%95%9D%F0%9D%95%A5%F0%9' .
37 'D%95%9A%F0%9D%95%93%F0%9D%95%AA%F0%9D%95%A5%F0%9D%95%96:characters',
38 $this->cache->makeKey( 'this', 'key', 'contains', '𝕞𝕦𝕝𝕥𝕚𝕓𝕪𝕥𝕖', 'characters' )
39 );
40
41 $this->assertEquals(
42 'test:this:key:contains:#c118f92685a635cb843039de50014c9c',
43 $this->cache->makeKey( 'this', 'key', 'contains', '𝕥𝕠𝕠 𝕞𝕒𝕟𝕪 𝕞𝕦𝕝𝕥𝕚𝕓𝕪𝕥𝕖 𝕔𝕙𝕒𝕣𝕒𝕔𝕥𝕖𝕣𝕤' )
44 );
45
46 $this->assertEquals(
47 'test:BagOStuff-long-key:##dc89dcb43b28614da27660240af478b5',
48 $this->cache->makeKey( '𝕖𝕧𝕖𝕟', '𝕚𝕗', '𝕨𝕖', '𝕄𝔻𝟝', '𝕖𝕒𝕔𝕙',
49 '𝕒𝕣𝕘𝕦𝕞𝕖𝕟𝕥', '𝕥𝕙𝕚𝕤', '𝕜𝕖𝕪', '𝕨𝕠𝕦𝕝𝕕', '𝕤𝕥𝕚𝕝𝕝', '𝕓𝕖', '𝕥𝕠𝕠', '𝕝𝕠𝕟𝕘' )
50 );
51
52 $this->assertEquals(
53 'test:%23%235820ad1d105aa4dc698585c39df73e19',
54 $this->cache->makeKey( '##5820ad1d105aa4dc698585c39df73e19' )
55 );
56
57 $this->assertEquals(
58 'test:percent_is_escaped:!@$%25^&*()',
59 $this->cache->makeKey( 'percent_is_escaped', '!@$%^&*()' )
60 );
61
62 $this->assertEquals(
63 'test:colon_is_escaped:!@$%3A^&*()',
64 $this->cache->makeKey( 'colon_is_escaped', '!@$:^&*()' )
65 );
66
67 $this->assertEquals(
68 'test:long_key_part_hashed:#0244f7b1811d982dd932dd7de01465ac',
69 $this->cache->makeKey( 'long_key_part_hashed', str_repeat( 'y', 500 ) )
70 );
71 }
72
73 /**
74 * @dataProvider validKeyProvider
75 */
76 public function testValidateKeyEncoding( $key ) {
77 $this->assertSame( $key, $this->cache->validateKeyEncoding( $key ) );
78 }
79
80 public static function validKeyProvider() {
81 return [
82 'empty' => [ '' ],
83 'digits' => [ '09' ],
84 'letters' => [ 'AZaz' ],
85 'ASCII special characters' => [ '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' ],
86 ];
87 }
88
89 /**
90 * @dataProvider invalidKeyProvider
91 */
92 public function testValidateKeyEncodingThrowsException( $key ) {
93 $this->expectException( Exception::class );
94 $this->cache->validateKeyEncoding( $key );
95 }
96
97 public static function invalidKeyProvider() {
98 return [
99 [ "\x00" ],
100 [ ' ' ],
101 [ "\x1F" ],
102 [ "\x7F" ],
103 [ "\x80" ],
104 [ "\xFF" ],
105 ];
106 }
107 }