1+ <?php
2+ /**
3+ * @author ihipop@gmail.com @ 19-2-27 下午6:22 For 1688-sdk.
4+ */
5+
6+ use PHPUnit \Framework \TestCase ;
7+
8+ class SimpleCacheNullCacheTest extends TestCase
9+ {
10+
11+ protected $ nullCache ;
12+ protected $ invalidKey = [];
13+
14+ public function __construct (?string $ name = null , array $ data = [], string $ dataName = '' )
15+ {
16+ parent ::__construct ($ name , $ data , $ dataName );
17+ $ this ->nullCache = new \ihipop \PsrNullCache \SimpleCache \NullCache (false );
18+ $ this ->invalidKey = str_split ('{}()/\@: ' );
19+ }
20+
21+ public function testInvalidKeySet ()
22+ {
23+ $ invalidKeyChr = array_merge ([new stdClass (), '' ], $ this ->invalidKey );
24+ $ totalException = count ($ invalidKeyChr );
25+ $ exceptionContainer = [];
26+ foreach ($ invalidKeyChr as $ key ) {
27+ try {
28+ $ this ->nullCache ->set ($ key , 'foobar ' );
29+ } catch (\Psr \SimpleCache \InvalidArgumentException $ e ) {
30+ $ exceptionContainer [] = $ e ;
31+ }
32+ }
33+
34+ $ this ->assertEquals ($ totalException , count ($ exceptionContainer ));
35+ $ this ->assertContainsOnlyInstancesOf (\Psr \SimpleCache \InvalidArgumentException::class, $ exceptionContainer );
36+ }
37+
38+ public function testInvalidKeyGet ()
39+ {
40+ $ invalidKeyChr = array_merge ([new stdClass (), '' ], $ this ->invalidKey );
41+ $ totalException = count ($ invalidKeyChr );
42+ $ exceptionContainer = [];
43+ foreach ($ invalidKeyChr as $ key ) {
44+ try {
45+ $ this ->nullCache ->get ($ key );
46+ } catch (\Psr \SimpleCache \InvalidArgumentException $ e ) {
47+ $ exceptionContainer [] = $ e ;
48+ }
49+ }
50+
51+ $ this ->assertEquals ($ totalException , count ($ exceptionContainer ));
52+ $ this ->assertContainsOnlyInstancesOf (\Psr \SimpleCache \InvalidArgumentException::class, $ exceptionContainer );
53+ }
54+
55+ public function testInvalidKeyDelete ()
56+ {
57+ $ invalidKeyChr = array_merge ([new stdClass (), '' ], $ this ->invalidKey );
58+ $ totalException = count ($ invalidKeyChr );
59+ foreach ($ invalidKeyChr as $ key ) {
60+ try {
61+ $ this ->nullCache ->delete ($ key );
62+ } catch (\Psr \SimpleCache \InvalidArgumentException $ e ) {
63+ $ exceptionContainer [] = $ e ;
64+ }
65+ }
66+
67+ $ this ->assertEquals ($ totalException , count ($ exceptionContainer ));
68+ $ this ->assertContainsOnlyInstancesOf (\Psr \SimpleCache \InvalidArgumentException::class, $ exceptionContainer );
69+ }
70+
71+ public function testInvalidKeyHas ()
72+ {
73+ $ invalidKeyChr = array_merge ([new stdClass (), '' ], $ this ->invalidKey );
74+ $ totalException = count ($ invalidKeyChr );
75+ $ exceptionContainer = [];
76+ foreach ($ invalidKeyChr as $ key ) {
77+ try {
78+ $ this ->nullCache ->has ($ key );
79+ } catch (\Psr \SimpleCache \InvalidArgumentException $ e ) {
80+ $ exceptionContainer [] = $ e ;
81+ }
82+ }
83+
84+ $ this ->assertEquals ($ totalException , count ($ exceptionContainer ));
85+ $ this ->assertContainsOnlyInstancesOf (\Psr \SimpleCache \InvalidArgumentException::class, $ exceptionContainer );
86+ }
87+
88+ public function testInvalidKeySetMultiple ()
89+ {
90+ $ invalidKeyChr = array_merge (['' ], $ this ->invalidKey );
91+ $ totalException = count ($ invalidKeyChr );
92+ $ exceptionContainer = [];
93+ foreach ($ invalidKeyChr as $ key ) {
94+ try {
95+ $ this ->nullCache ->setMultiple (['foo ' => 'bar ' , $ key => 'bar ' ]);
96+ } catch (\Psr \SimpleCache \InvalidArgumentException $ e ) {
97+ $ exceptionContainer [] = $ e ;
98+ }
99+ }
100+
101+ $ this ->assertEquals ($ totalException , count ($ exceptionContainer ));
102+ $ this ->assertContainsOnlyInstancesOf (\Psr \SimpleCache \InvalidArgumentException::class, $ exceptionContainer );
103+ }
104+
105+ public function testInvalidKeyGetMultiple ()
106+ {
107+ $ invalidKeyChr = array_merge (['' ], $ this ->invalidKey );
108+ $ totalException = count ($ invalidKeyChr );
109+ $ exceptionContainer = [];
110+ foreach ($ invalidKeyChr as $ key ) {
111+ try {
112+ foreach ($ this ->nullCache ->getMultiple (['foo ' , $ key ]) as $ iter ) {
113+ // fo nothing
114+ }
115+ } catch (\Psr \SimpleCache \InvalidArgumentException $ e ) {
116+ $ exceptionContainer [] = $ e ;
117+ }
118+ }
119+
120+ $ this ->assertEquals ($ totalException , count ($ exceptionContainer ));
121+ $ this ->assertContainsOnlyInstancesOf (\Psr \SimpleCache \InvalidArgumentException::class, $ exceptionContainer );
122+ }
123+
124+ public function testInvalidKeyDeleteMultiple ()
125+ {
126+ $ invalidKeyChr = array_merge (['' ], $ this ->invalidKey );
127+ $ totalException = count ($ invalidKeyChr );
128+ $ exceptionContainer = [];
129+ foreach ($ invalidKeyChr as $ key ) {
130+ try {
131+ $ this ->nullCache ->deleteMultiple (['foo ' , $ key ]);
132+ } catch (\Psr \SimpleCache \InvalidArgumentException $ e ) {
133+ $ exceptionContainer [] = $ e ;
134+ }
135+ }
136+
137+ $ this ->assertEquals ($ totalException , count ($ exceptionContainer ));
138+ $ this ->assertContainsOnlyInstancesOf (\Psr \SimpleCache \InvalidArgumentException::class, $ exceptionContainer );
139+ }
140+ }
0 commit comments