5
5
*/
6
6
namespace Magento \Config \App \Config \Type ;
7
7
8
+ use Magento \Config \Model \Config \Factory ;
9
+ use Magento \Framework \Lock \Backend \Cache ;
10
+ use Magento \Framework \App \Cache \Type \Config ;
11
+ use Magento \Framework \Cache \FrontendInterface ;
12
+ use Magento \Framework \Lock \LockManagerInterface ;
8
13
use Magento \Framework \ObjectManagerInterface ;
14
+ use Magento \Store \Model \ScopeInterface ;
9
15
use Magento \TestFramework \Helper \Bootstrap ;
10
16
11
17
/**
12
18
* @magentoDataFixture Magento/Config/_files/config_data.php
13
19
* @magentoAppIsolation enabled
20
+ * @magentoCache config enabled
14
21
*/
15
22
class SystemTest extends \PHPUnit \Framework \TestCase
16
23
{
@@ -27,24 +34,145 @@ class SystemTest extends \PHPUnit\Framework\TestCase
27
34
protected function setUp ()
28
35
{
29
36
$ this ->objectManager = Bootstrap::getObjectManager ();
30
- $ this ->system = $ this ->objectManager -> create (System::class );
37
+ $ this ->system = $ this ->createSystemConfig ( );
31
38
}
32
39
33
- public function testGetValueDefaultScope ()
40
+ public function testGetValueForDefaultScope ()
34
41
{
35
42
$ this ->assertEquals (
36
43
'value1.db.default.test ' ,
37
44
$ this ->system ->get ('default/web/test/test_value_1 ' )
38
45
);
46
+ }
39
47
48
+ public function testGetValueForWebsiteScope ()
49
+ {
40
50
$ this ->assertEquals (
41
51
'value1.db.website_base.test ' ,
42
52
$ this ->system ->get ('websites/base/web/test/test_value_1 ' )
43
53
);
54
+ }
44
55
56
+ public function testGetValueForStoreScope ()
57
+ {
45
58
$ this ->assertEquals (
46
59
'value1.db.store_default.test ' ,
47
60
$ this ->system ->get ('stores/default/web/test/test_value_1 ' )
48
61
);
49
62
}
63
+
64
+ public function testCachingDoesNotBreakValueRetrievalForStoreScope ()
65
+ {
66
+ // First uncached call to configuration
67
+ $ this ->createSystemConfig ()->get ('stores/default/web/test/test_value_1 ' );
68
+
69
+ // Second call after cache data is stored
70
+ $ this ->assertEquals (
71
+ 'value1.db.store_default.test ' ,
72
+ $ this ->createSystemConfig ()->get ('stores/default/web/test/test_value_1 ' )
73
+ );
74
+ }
75
+
76
+ public function testCachingDoesNotBreakValueRetrievalForWebsiteScope ()
77
+ {
78
+ // First uncached call to configuration
79
+ $ this ->createSystemConfig ()->get ('websites/base/web/test/test_value_1 ' );
80
+
81
+ // Second call after cache data is stored
82
+ $ this ->assertEquals (
83
+ 'value1.db.website_base.test ' ,
84
+ $ this ->createSystemConfig ()->get ('websites/base/web/test/test_value_1 ' )
85
+ );
86
+ }
87
+
88
+ public function testCachingDoesNotBreakValueRetrievalForDefaultScope ()
89
+ {
90
+ // First uncached call to configuration
91
+ $ this ->createSystemConfig ()->get ('default/web/test/test_value_1 ' );
92
+
93
+ // Second call after cache data is stored
94
+ $ this ->assertEquals (
95
+ 'value1.db.default.test ' ,
96
+ $ this ->createSystemConfig ()->get ('default/web/test/test_value_1 ' )
97
+ );
98
+ }
99
+
100
+ public function testClearingCachePrefixAndLockingItReturnsStaleCachedValue ()
101
+ {
102
+ // First uncached call to configuration
103
+ $ this ->createSystemConfig ()->get ('default/web/test/test_value_1 ' );
104
+
105
+ $ this ->clearConfigCachePrefix ();
106
+ $ this ->accessLock ()->lock ('SYSTEM_CONFIG ' );
107
+ // Second call after cache data is stored
108
+ $ configValue = $ this ->createSystemConfig ()->get ('default/web/test/test_value_1 ' );
109
+ $ this ->accessLock ()->unlock ('SYSTEM_CONFIG ' );
110
+
111
+ $ this ->assertEquals (
112
+ 'value1.db.default.test ' ,
113
+ $ configValue
114
+ );
115
+ }
116
+
117
+ public function testClearingConfigurationCacheAndLockingFallsBackToStaleCache ()
118
+ {
119
+ // First uncached call to configuration
120
+ $ this ->createSystemConfig ()->get ('stores/default/web/test/test_value_1 ' );
121
+
122
+ $ this ->accessCacheFrontend ()->clean (\Zend_Cache::CLEANING_MODE_MATCHING_TAG , [System::CACHE_TAG ]);
123
+
124
+ $ this ->accessLock ()->lock ('SYSTEM_CONFIG ' );
125
+ // Second call after cache data is stored
126
+ $ configValue = $ this ->createSystemConfig ()->get ('stores/default/web/test/test_value_1 ' );
127
+ $ this ->accessLock ()->unlock ('SYSTEM_CONFIG ' );
128
+
129
+ $ this ->assertEquals (
130
+ 'value1.db.store_default.test ' ,
131
+ $ configValue
132
+ );
133
+ }
134
+
135
+ public function testChangingConfigurationValueRefreshesACache ()
136
+ {
137
+ $ systemConfig = $ this ->objectManager ->get (System::class);
138
+
139
+ // First uncached call to configuration
140
+ $ systemConfig ->get ('stores/default/web/test/test_value_1 ' );
141
+
142
+ /** @var Factory $configFactory */
143
+ $ configFactory = $ this ->objectManager ->create (Factory::class);
144
+ $ config = $ configFactory ->create ();
145
+ $ config ->setScope (ScopeInterface::SCOPE_STORES );
146
+ $ config ->setStore ('default ' );
147
+
148
+ $ config ->setDataByPath ('web/test/test_value_1 ' , 'new_uncached_value ' );
149
+ $ config ->save ();
150
+
151
+ $ this ->assertEquals (
152
+ 'new_uncached_value ' ,
153
+ $ systemConfig ->get ('stores/default/web/test/test_value_1 ' )
154
+ );
155
+ }
156
+
157
+ private function clearConfigCachePrefix ()
158
+ {
159
+ $ cache = $ this ->accessCacheFrontend ();
160
+
161
+ $ cache ->remove (System::STALE_CACHE_KEY_FOR_PREFIX );
162
+ }
163
+
164
+ private function accessCacheFrontend (): FrontendInterface
165
+ {
166
+ return $ this ->objectManager ->get (Config::class);
167
+ }
168
+
169
+ private function createSystemConfig (): System
170
+ {
171
+ return $ this ->objectManager ->create (System::class);
172
+ }
173
+
174
+ private function accessLock (): LockManagerInterface
175
+ {
176
+ return $ this ->objectManager ->get (Cache::class);
177
+ }
50
178
}
0 commit comments