/
TestApp.pm
321 lines (232 loc) · 7.22 KB
/
TestApp.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# See bottom of file for license and copyright
package Unit::TestApp;
use v5.14;
=begin TML
---+ Class Unit::TestApp
This is the default application class for unit tests. It provides additional
functionality to support testing while offloading it from the parent
=Foswiki::App= class to reduce %WIKITOOLNAME% memory footprint and probably
avoid some slowdown by handling cases which are not gonna be met outside of
the testing environment.
Alongside to this class one must also study =Foswiki::Engine::Test=.
=cut
use Assert;
use Scalar::Util qw(blessed weaken refaddr);
use Try::Tiny;
use Foswiki::Class qw(callbacks);
extends qw(Foswiki::App);
=begin TML
---++ Callbacks
This class defines the following callbacks (see =Foswiki::Aux::Callbacks=):
| *name* | *Description* |
| =testPreHandleRequest= | Executed before control is passed over to =Foswiki::App= =handleRequest()= method. |
| =testPostHandleRequest= | Executed right after =Foswiki::App= =handleRequest()= method finishes. |
See [[#AttrCallbacks][=callbacks= attribute]].
---+++ Callback testPreHandleRequest
No =params= are sent to the handler.
---+++ Callback testPostHandleRequest
=params= contains one key: =rc= with =handleRequest= method return value.
=cut
callback_names qw(testPreHandleRequest testPostHandleRequest);
=begin TML
---++ ObjectAttribute requestParams -> hash
This is a hash of parameters to be passed over to =Foswiki::Request=
constructor.
=cut
has requestParams => (
is => 'rwp',
lazy => 1,
default => sub { {} },
);
=begin TML
---++ ObjectAttribute engineParams -> hash
This is a hash of parameters to be passed over to =Foswiki::Engine=
constructor.
=cut
has engineParams => (
is => 'rw',
lazy => 1,
default => sub { {} },
);
=begin TML
---++ ObjectAttribute cfgParams -> hash
This is a hash of parameters to be passed over to =Foswiki::Config=
constructor.
=cut
has cfgParams => (
is => 'rw',
lazy => 1,
builder => 'prepareCfgParams',
);
=begin TML
#AttrCallbacks
---++ ObjectAttribute callbacks -> hash
A hash of =callback => \&handler= pairs. Handlers are registered for their
respective callbacks. Each handler =data= parameter is a hash whith the only
key =app= containing reference to the application object.
=cut
has callbacks => (
is => 'rw',
lazy => 1,
predicate => 1,
isa => Foswiki::Object::isaHASH('callbacks'),
default => sub { {} },
);
has _cbRegistered => (
is => 'rw',
default => 0,
);
around BUILDARGS => sub {
my $orig = shift;
my $this = shift;
# SMELL The global $inUnitTestMode must be gone.
$Foswiki::inUnitTestMode = 1;
return $orig->( $this, inUnitTestMode => 1, @_ );
};
sub BUILD {
my $this = shift;
# Fixup Foswiki::AppObject descendants which have been cloned from objects
# on another Foswiki::App instance.
foreach my $attr ( keys %$this ) {
if (
blessed( $this->{$attr} )
&& $this->$attr->isa('Foswiki::Object')
&& $this->$attr->does('Foswiki::AppObject')
&& ( !defined( $this->$attr->app )
|| ( $this->$attr->app != $this ) )
)
{
$this->$attr->_set_app($this);
}
}
}
=begin TML
---++ ObjectMethod cloneEnv => \%envHash
Clones current application =env= hash.
=cut
sub cloneEnv {
my $this = shift;
# SMELL Use Foswiki::Object internals.
my $clonedEnv = $this->_cloneData( $this->env, 'env' );
$this->_clear__clone_heap;
return $clonedEnv;
}
sub registerCallbacks {
my $this = shift;
return if $this->_cbRegistered;
my $cbData = { app => $this, };
weaken( $cbData->{app} );
foreach my $cbName ( keys %{ $this->callbacks } ) {
$this->registerCallback( $cbName, $this->callbacks->{$cbName},
$cbData );
}
$this->_cbRegistered(1);
}
around callbacksInit => sub {
my $orig = shift;
my $this = shift;
$this->registerCallbacks;
return $orig->( $this, @_ );
};
around _prepareRequest => sub {
my $orig = shift;
my $this = shift;
return $orig->( $this, %{ $this->requestParams } );
};
around _prepareEngine => sub {
my $orig = shift;
my $this = shift;
return $orig->( $this, %{ $this->engineParams } );
};
around _prepareConfig => sub {
my $orig = shift;
my $this = shift;
return $this->create( 'Foswiki::Config', %{ $this->cfgParams }, );
};
around handleRequest => sub {
my $orig = shift;
my $this = shift;
my $rc;
try {
$this->callback('testPreHandleRequest');
$rc = $orig->( $this, @_ );
}
catch {
Foswiki::Exception::Fatal->rethrow($_);
}
finally {
$this->callback( 'testPostHandleRequest', { rc => $rc }, );
};
return $rc;
};
sub prepareCfgParams {
return {};
}
=begin TML
---++ Examples
---+++ A test case code
This code demonstrates a sample case of testing a request. Take a note that
tests are using =Foswiki::Engine::Test= engine.
What is demonstrated here is:
* Handling of application's internal exceptions. Useful for cases when we expect an exception and test success depends on it. It would be then easier to get the exception itself instead of analyzing HTML output.
* Passing of new application parameters via =createNewFoswikiApp= method.
* Defining basic request parameters as engine constructor parameters.
<verbatim>
sub _cbHRE {
my $obj = shift;
my %args = @_;
$args{params}{exception}->rethrow;
}
sub test_someTest {
my $this = shift;
...
$this->createNewFoswikiApp(
requestParams => {
initializer => {
templatetopic => $this->test_web . ".TemplateTopic",
},
},
engineParams => {
initialAttributes => {
path_info => "/" . $this->test_web . "/" . $this->test_topic,
user => $this->app->cfg->data->{AdminUserLogin},
action => 'view',
method => 'GET',
},
simulate => 'psgi',
},
callbacks => { handleRequestException => \&_cbHRE, },
);
try {
my ($text) = $this->capture(
sub {
return $this->app->handleRequest;
}
);
} catch {
my $e = Foswiki::Exception::Fatal->transmute($_, 0);
# Handle any application exception here.
unless ( $e->isa('Foswiki::OopsException') ) {
# Assume that we expected an oops.
$e->rethrow;
}
...
}
}
</verbatim>
---++ See also
=Unit::FoswikiTestRole=, =Foswiki::Engine::Test=
=cut
1;
__END__
Copyright (C) 2016 Foswiki Contributors
All Rights Reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.