@@ -30,25 +30,40 @@ class LiteSpeedTest extends TestCase
30
30
/**
31
31
* @var string
32
32
*/
33
- private $ targetDir ;
33
+ private $ documentRoot ;
34
34
35
35
protected function setUp ()
36
36
{
37
37
$ this ->httpDispatcher = \Mockery::mock (HttpDispatcher::class);
38
38
39
- $ targetDir = sys_get_temp_dir ().'/fos_ls_tests ' ;
40
- if (is_dir ($ targetDir )) {
41
- array_map ('unlink ' , glob ($ targetDir .'/* ' ));
42
- rmdir ($ targetDir );
39
+ $ deleteFilesAndFolders = function ($ path ) use (&$ deleteFilesAndFolders ) {
40
+ $ files = glob ($ path .'/* ' );
41
+ foreach ($ files as $ file ) {
42
+ is_dir ($ file ) ? $ deleteFilesAndFolders ($ file ) : unlink ($ file );
43
+ }
44
+ rmdir ($ path );
45
+
46
+ return ;
47
+ };
48
+
49
+ $ documentRoot = sys_get_temp_dir ().'/fos_ls_tests ' ;
50
+ if (is_dir ($ documentRoot )) {
51
+ $ deleteFilesAndFolders ($ documentRoot );
43
52
}
44
- mkdir ($ targetDir );
53
+ mkdir ($ documentRoot );
45
54
46
- $ this ->targetDir = $ targetDir ;
55
+ $ this ->documentRoot = $ documentRoot ;
47
56
}
48
57
49
58
public function testPurge ()
50
59
{
51
- $ ls = new LiteSpeed ($ this ->httpDispatcher , ['target_dir ' => $ this ->targetDir ]);
60
+ $ ls = new LiteSpeed ($ this ->httpDispatcher , [
61
+ 'document_root ' => $ this ->documentRoot ,
62
+ 'target_dir ' => 'subfolder ' ,
63
+ ]);
64
+
65
+ // We're also testing target_dir here so we have to create the subfolder
66
+ mkdir ($ this ->documentRoot .'/subfolder ' );
52
67
53
68
$ expectedContent = <<<'EOT'
54
69
<?php
@@ -59,7 +74,7 @@ public function testPurge()
59
74
header('X-LiteSpeed-Purge: foo\'); exec(\\\'rm -rf /\\\');//');
60
75
61
76
EOT;
62
- $ this ->assertLiteSpeedPurger ($ expectedContent );
77
+ $ this ->assertLiteSpeedPurger ([ $ expectedContent], ' subfolder ' );
63
78
64
79
$ ls ->purge ('/url ' );
65
80
$ ls ->purge ('/another/url ' );
@@ -68,12 +83,50 @@ public function testPurge()
68
83
$ ls ->flush ();
69
84
70
85
// Assert file has been deleted again
71
- $ this ->assertDirectoryEmpty ($ this ->targetDir );
86
+ $ this ->assertDirectoryEmpty ($ this ->documentRoot .'/subfolder ' );
87
+ }
88
+
89
+ public function testPurgeWithAbsoluteUrls ()
90
+ {
91
+ $ ls = new LiteSpeed ($ this ->httpDispatcher , [
92
+ 'document_root ' => $ this ->documentRoot ,
93
+ ]);
94
+
95
+ $ expectedContents = [];
96
+ $ expectedContents [] = <<<'EOT'
97
+ <?php
98
+
99
+ header('X-LiteSpeed-Purge: /url');
100
+
101
+ EOT;
102
+ $ expectedContents [] = <<<'EOT'
103
+ <?php
104
+
105
+ header('X-LiteSpeed-Purge: /foobar');
106
+
107
+ EOT;
108
+ $ expectedContents [] = <<<'EOT'
109
+ <?php
110
+
111
+ header('X-LiteSpeed-Purge: /foobar');
112
+
113
+ EOT;
114
+ $ this ->assertLiteSpeedPurger ($ expectedContents );
115
+
116
+ $ ls ->purge ('/url ' );
117
+ $ ls ->purge ('https://www.domain.com/foobar ' );
118
+ $ ls ->purge ('https://www.domain.ch/foobar ' );
119
+ $ ls ->flush ();
120
+
121
+ // Assert file has been deleted again
122
+ $ this ->assertDirectoryEmpty ($ this ->documentRoot );
72
123
}
73
124
74
125
public function testInvalidateTags ()
75
126
{
76
- $ ls = new LiteSpeed ($ this ->httpDispatcher , ['target_dir ' => $ this ->targetDir ]);
127
+ $ ls = new LiteSpeed ($ this ->httpDispatcher , [
128
+ 'document_root ' => $ this ->documentRoot ,
129
+ ]);
77
130
78
131
$ expectedContent = <<<'EOT'
79
132
<?php
@@ -82,55 +135,70 @@ public function testInvalidateTags()
82
135
header('X-LiteSpeed-Purge: tag=more, tag=tags');
83
136
84
137
EOT;
85
- $ this ->assertLiteSpeedPurger ($ expectedContent );
138
+ $ this ->assertLiteSpeedPurger ([ $ expectedContent] );
86
139
87
140
$ ls ->invalidateTags (['foobar ' , 'tag ' ]);
88
141
$ ls ->invalidateTags (['more ' , 'tags ' ]);
89
142
$ ls ->flush ();
90
143
91
144
// Assert file has been deleted again
92
- $ this ->assertDirectoryEmpty ($ this ->targetDir );
145
+ $ this ->assertDirectoryEmpty ($ this ->documentRoot );
93
146
}
94
147
95
148
public function testClear ()
96
149
{
97
- $ ls = new LiteSpeed ($ this ->httpDispatcher , ['target_dir ' => $ this ->targetDir ]);
150
+ $ ls = new LiteSpeed ($ this ->httpDispatcher , [
151
+ 'document_root ' => $ this ->documentRoot ,
152
+ ]);
98
153
99
154
$ expectedContent = <<<'EOT'
100
155
<?php
101
156
102
157
header('X-LiteSpeed-Purge: *');
103
158
104
159
EOT;
105
- $ this ->assertLiteSpeedPurger ($ expectedContent );
160
+ $ this ->assertLiteSpeedPurger ([ $ expectedContent] );
106
161
107
162
$ ls ->clear ();
108
163
$ ls ->flush ();
109
164
110
165
// Assert file has been deleted again
111
- $ this ->assertDirectoryEmpty ($ this ->targetDir );
166
+ $ this ->assertDirectoryEmpty ($ this ->documentRoot );
112
167
}
113
168
114
- private function assertLiteSpeedPurger ($ expectedContent )
169
+ private function assertLiteSpeedPurger (array $ expectedContents , $ targetDir = '' )
115
170
{
116
- $ this ->httpDispatcher ->shouldReceive ('invalidate ' )->once ()->with (
117
- \Mockery::on (
118
- function (RequestInterface $ request ) use ($ expectedContent ) {
171
+ $ methodCallCount = 0 ;
172
+
173
+ $ this ->httpDispatcher ->shouldReceive ('invalidate ' )
174
+ ->times (count ($ expectedContents ))
175
+ ->with (\Mockery::on (
176
+ function (RequestInterface $ request ) use ($ expectedContents , $ targetDir , &$ methodCallCount ) {
119
177
$ this ->assertEquals ('GET ' , $ request ->getMethod ());
120
178
121
- $ filename = ltrim ($ request ->getRequestTarget (), '/ ' );
179
+ $ cutOff = $ targetDir ? (strlen ($ targetDir ) + 2 ) : 1 ;
180
+ $ filename = substr_replace ($ request ->getRequestTarget (), '' , 0 , $ cutOff );
181
+
182
+ $ path = $ this ->documentRoot ;
183
+
184
+ if ($ targetDir ) {
185
+ $ path .= '/ ' .$ targetDir ;
186
+ }
122
187
123
188
// Assert file has been generated
124
- $ this ->assertFileExists ($ this -> targetDir .'/ ' .$ filename );
189
+ $ this ->assertFileExists ($ path .'/ ' .$ filename );
125
190
126
191
// Assert file contents
127
- $ this ->assertSame ($ expectedContent , file_get_contents ($ this ->targetDir .'/ ' .$ filename ));
192
+ $ this ->assertSame ($ expectedContents [$ methodCallCount ], file_get_contents ($ path .'/ ' .$ filename ));
193
+
194
+ ++$ methodCallCount ;
128
195
129
196
return true ;
130
197
}
131
198
),
132
199
true
133
- );
200
+ );
201
+
134
202
$ this ->httpDispatcher ->shouldReceive ('flush ' )->once ();
135
203
}
136
204
0 commit comments