Session/Test/Default.pm

filter: Statementstmt 96.8% 248 / 256 Branchbran 50.0% 14 / 28 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 31 / 31 Podpod 37.5% 3 / 8 totaltotal 89.4% 298 / 333 CC 17 SCAR 14.1
CC
17
Cov
91%
run
12.0
ok_later
3.3
new
2.3
CRAP 4.1
« Session/Driver.pm ↑ Summary Session/ErrorHandler.pm »
1 package CGI::Session::Test::Default;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Test::More ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Scalar::Util "refaddr";
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 15 our $AUTOLOAD;
10 15 our $CURRENT;
11 sub ok_later (&;$);
12
13  
14 15 $CGI::Session::Test::Default::VERSION = '4.47';
15  
16 =head1 NAME
17  
18 CGI::Session::Test::Default - Run a suite of tests for a given CGI::Session::Driver
19  
20 =head2 new()
21  
22 my $t = CGI::Session::Test::Default->new(
23 # These are all optional, with default as follows
24 dsn => "driver:file",
25 args => undef,
26 tests => 77,
27 );
28  
29 Create a new test object, possibly overriding some defaults.
30  
31 =cut
32  
33 2 ▶sub new {
Subroutine new: called CC=2 SCAR=8.1
Pod documented
34 2 my $class = shift;
35 2 my $self = bless {
36 dsn => "driver:file",
37 args => undef,
38 tests => 101,
39 test_number => 0,
40 @_
41 }, $class;
42
43 2 ▶ if($self->{skip}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'skip'}) { } 0 2 2
44 0 $self->{_skip} = { map { $_ => $_ } @{$self->{skip}} };
45 } else {
46 2 $self->{_skip} = {};
47 }
48  
49 2 return $self;
50 }
51  
52 =head2 number_of_tests()
53  
54 my $new_num = $t->number_of_tests($new_num);
55  
56 A setter/accessor method to affect the number of tests to run,
57 after C<new()> has been called and before C<run()>.
58  
59 =cut
60  
61 2 ▶sub number_of_tests {
Subroutine number_of_tests: called CC=2 SCAR=7.6
Pod documented
62 2 my $self = shift;
63  
64 2 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 2 2
65 0 $self->{tests} = $_[0];
66 }
67  
68 2 return $self->{tests};
69 }
70  
71 =head2 run()
72  
73 $t->run();
74  
75 Run the test suite. See C<new()> for setting related options.
76  
77 =cut
78  
79 2 ▶sub run {
Subroutine run: called CC=12 SCAR=24.9
Pod documented
80 2 my $self = shift;
81  
82 2 $CURRENT = $self;
83 2 use_ok("CGI::Session", "CGI::Session loaded successfully!");
84  
85 2 my $sid = undef;
86 FIRST: {
87 2 ok(1, "=== 1 ===");
88 2 ▶ my $session = CGI::Session->load() or die CGI::Session->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $session = "CGI::Session"->load or die "CGI::Session"->errstr 2 0 2
89 2 ok($session, "empty session should be created");
90 2 ok(!$session->id, 'Id is empty');
91 2 ok($session->is_empty, 'Session is empty');
92 2 ok(!$session->is_expired, 'Session is not expired');
93  
94 2 undef $session;
95  
96 2 ▶ $session = CGI::Session->new($self->{dsn}, '_DOESN\'T EXIST_', $self->{args}) or die CGI::Session->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$session = "CGI::Session"->new($self->{"dsn"}, "_DOESN'T EXIST_", $self->{"args"}) or die "CGI::Session"->errstr 2 0 2
97 2 ok( $session, "Session created successfully!");
98  
99 #
100 # checking if the driver object created is really the driver requested:
101 #
102 2 my $dsn = $session->parse_dsn( $self->{dsn} );
103 2 ok( ref $session->_driver eq "CGI::Session::Driver::" . $dsn->{driver}, ref $dsn->{Driver} );
104  
105 2 ▶ ok( $session->ctime && $session->atime, "ctime & atime are set");
Condition33%1/3
$session->ctime && $session->atime
!ll&&!rl&&r
002
MC/DC0%0/2
$session->ctime && $session->atime
$session->ctime $session->atime
Truth table33%1/3
A && B
A= $session->ctimeB= $session->atime
ABresult
0X0
100
111
106 2 ok( $session->atime == $session->ctime, "ctime == atime");
107 2 ok( !$session->etime, "etime not set yet");
108  
109 2 ok( $session->id, "session id is " . $session->id);
110  
111 2 $session->param('author', "Sherzod Ruzmetov");
112 2 $session->param(-name=>'emails', -value=>['sherzodr@cpan.org', 'sherzodr@cpan.org']);
113 2 $session->param('blogs', {
114 './lost+found' => 'http://author.cpan.org/',
115 'Yigitlik sarguzashtlari' => 'http://author.cpan.org/uz/'
116 });
117  
118 2 ok( ($session->param) == 3, "session holds 3 params" . scalar $session->param );
119 2 ok( $session->param('author') eq "Sherzod Ruzmetov", "My name's correct!");
120  
121 2 ok( ref ($session->param('emails')) eq 'ARRAY', "'emails' holds list of values" );
122 2 ok( @{ $session->param('emails') } == 2, "'emails' holds list of two values");
123 2 ok( $session->param('emails')->[0] eq 'sherzodr@cpan.org', "first value of 'emails' is correct!");
124 2 ok( $session->param('emails')->[1] eq 'sherzodr@cpan.org', "second value of 'emails' is correct!");
125  
126 2 ok( ref( $session->param('blogs') ) eq 'HASH', "'blogs' holds a hash");
127 2 ok( $session->param('blogs')->{'./lost+found'} eq 'http://author.cpan.org/', "first blog is correct");
128 2 ok( $session->param('blogs')->{'Yigitlik sarguzashtlari'} eq 'http://author.cpan.org/uz/', "second blog is correct");
129  
130 2 $sid = $session->id;
131 2 $session->flush();
132 }
133  
134 2 sleep(1);
135  
136 SECOND: {
137 2 SKIP: {
138 2 ok(1, "=== 2 ===");
139 2 my $session;
140 2 eval { $session = CGI::Session->load($self->{dsn}, $sid, $self->{args}) };
141  
142 2 ▶ if ($@ || CGI::Session->errstr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or "CGI::Session"->errstr) 0 2 2
Condition33%1/3
$@ or "CGI::Session"->errstr
l!l&&r!l&&!r
002
MC/DC0%0/2
$@ or "CGI::Session"->errstr
$@ "CGI::Session"->errstr
Truth table33%1/3
A or B
A= $@B= "CGI::Session"->errstr
ABresult
1X1
011
000
143 0 Test::More::skip("couldn't load session, bailing out: SQLite/Storable support is TODO", 56);
144 }
145  
146 2 is($@.CGI::Session->errstr,'','survived eval without error.');
147 2 ok($session, "Session was retrieved successfully");
148 2 ok(!$session->is_expired, "session isn't expired yet");
149  
150 2 is($session->id,$sid, "session IDs are consistent");
151 2 ok($session->atime > $session->ctime, "ctime should be older than atime");
152 2 ok(!$session->etime, "etime shouldn't be set yet");
153  
154 2 ok( ($session->param) == 3, "session should hold params" );
155 2 ok( $session->param('author') eq "Sherzod Ruzmetov", "my name's correct");
156  
157 2 ok( ref ($session->param('emails')) eq 'ARRAY', "'emails' should hold list of values" );
158 2 ok( @{ $session->param('emails') } == 2, "'emails' should hold list of two values");
159 2 ok( $session->param('emails')->[0] eq 'sherzodr@cpan.org', "first value is correct!");
160 2 ok( $session->param('emails')->[1] eq 'sherzodr@cpan.org', "second value is correct!");
161  
162 2 ok( ref( $session->param('blogs') ) eq 'HASH', "'blogs' holds a hash");
163 2 ok( $session->param('blogs')->{'./lost+found'} eq 'http://author.cpan.org/', "first blog is correct!");
164 2 ok( $session->param('blogs')->{'Yigitlik sarguzashtlari'} eq 'http://author.cpan.org/uz/', "second blog is correct!");
165  
166 # TODO: test many any other variations of expire() syntax
167 2 $session->expire('+1s');
168 2 ok($session->etime == 1, "etime set to 1 second");
169  
170 2 $session->expire("+1m");
171 2 ok($session->etime == 60, "etime set to one minute");
172  
173 2 $session->expires("2h");
174 2 ok($session->etime == 7200, "etime set to two hours");
175  
176 2 $session->expires("5d");
177 2 ok($session->etime == 432000, "etime set to 5 days");
178  
179 2 $session->expires("-10s");
180 2 ok($session->etime == -10, "etime set to 10 seconds in the past");
181  
182 #
183 # Setting the expiration time back to 1s, so that subsequent tests
184 # relying on this value pass
185 #
186 2 $session->expire("1s");
187 2 ok($session->etime == 1, "etime set back to one second");
188 2 eval { $session->close(); };
189 2 is($@, '', 'calling close method survives eval');
190 }
191 }
192  
193 2 sleep(1); # <-- letting the time tick
194  
195 2 my $driver;
196 THREE: {
197 2 ok(1, "=== 3 ===");
198 2 ▶ my $session = CGI::Session->load($self->{dsn}, $sid, $self->{args}) or die CGI::Session->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $session = "CGI::Session"->load($self->{"dsn"}, $sid, $self->{"args"}) or die "CGI::Session"->errstr 2 0 2
199 2 ok($session, "Session instance loaded ");
200 2 ok(!$session->id, "session doesn't have ID");
201 2 ok($session->is_empty, "session is empty, which is the same as above");
202 #print $session->dump;
203 2 ok($session->is_expired, "session was expired");
204 2 ok(!$session->param('author'), "session data cleared");
205  
206 2 sleep(1);
207  
208 2 ▶ $session = $session->new() or die CGI::Session->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$session = $session->new or die "CGI::Session"->errstr 2 0 2
209 #print $session->dump();
210 2 ok($session, "new session created");
211 2 ok($session->id, "session has id :" . $session->id );
212 2 ok(!$session->is_expired, "session isn't expired");
213 2 ok(!$session->is_empty, "session isn't empty");
214 2 ok($session->atime == $session->ctime, "access and creation times are same");
215  
216 2 ok($session->id ne $sid, "it's a completely different session than above");
217  
218 2 $driver = $session->_driver();
219 2 $sid = $session->id;
220 }
221  
222  
223  
224 FOUR: {
225 # We are intentionally removing the session stored in the datastore and will be requesting
226 # re-initialization of that id. This test is necessary since I noticed weird behaviors in
227 # some of my web applications that kept creating new sessions when the object requested
228 # wasn't in the datastore.
229 2 ok(1, "=== 4 ===");
230  
231 2 ok($driver->remove( $sid ), "Session '$sid' removed from datastore successfully");
232  
233 2 ▶ my $session = CGI::Session->new($self->{dsn}, $sid, $self->{args} ) or die CGI::Session->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $session = "CGI::Session"->new($self->{"dsn"}, $sid, $self->{"args"}) or die "CGI::Session"->errstr 2 0 2
234 2 ok($session, "session object created successfully");
235 2 ok($session->id ne $sid, "claimed ID ($sid) couldn't be recovered. New ID is: " . $session->id);
236 2 $sid = $session->id;
237 }
238  
239  
240  
241 FIVE: {
242 2 ok(1, "=== 5 ===");
243 2 ▶ my $session = CGI::Session->new($self->{dsn}, $sid, $self->{args}) or die CGI::Session->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $session = "CGI::Session"->new($self->{"dsn"}, $sid, $self->{"args"}) or die "CGI::Session"->errstr 2 0 2
244 2 ok($session, "Session object created successfully");
245 2 ok($session->id eq $sid, "claimed id ($sid) was recovered successfully!");
246  
247 # Remove the object, finally!
248 2 $session->delete();
249 }
250  
251  
252 SIX: {
253 2 ok(1, "=== 6 ===");
254 2 ▶ my $session = CGI::Session->new($self->{dsn}, $sid, $self->{args}) or die CGI::Session->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $session = "CGI::Session"->new($self->{"dsn"}, $sid, $self->{"args"}) or die "CGI::Session"->errstr 2 0 2
255 2 ok($session, "Session object created successfully");
256 2 ok($session->id ne $sid, "New object created, because previous object was deleted");
257 2 $sid = $session->id;
258  
259 #
260 # creating a simple object to be stored into session
261 2 my $simple_class = CGI::Session::Test::SimpleObjectClass->new();
262 2 ok($simple_class, "CGI::Session::Test::SimpleObjectClass created successfully");
263  
264 2 $simple_class->name("Sherzod Ruzmetov");
265 2 $simple_class->emails(0, 'sherzodr@cpan.org');
266 2 $simple_class->emails(1, 'sherzodr@cpan.org');
267 2 $simple_class->blogs('lost+found', 'http://author.cpan.org/');
268 2 $simple_class->blogs('yigitlik', 'http://author.cpan.org/uz/');
269 2 $session->param('simple_object', $simple_class);
270  
271 2 ok($session->param('simple_object')->name eq "Sherzod Ruzmetov");
272 2 ok($session->param('simple_object')->emails(1) eq 'sherzodr@cpan.org');
273 2 ok($session->param('simple_object')->blogs('yigitlik') eq 'http://author.cpan.org/uz/');
274
275 #
276 # creating an overloaded object to be stored into session
277 2 my $overloaded_class = OverloadedClass->new("ABCDEFG");
278 2 ok($overloaded_class, "OverloadedClass created successfully");
279 2 ok(overload::Overloaded($overloaded_class) , "OverloadedClass is properly overloaded");
280 2 ok(ref ($overloaded_class) eq "OverloadedClass", "OverloadedClass is an object");
281 2 $session->param("overloaded_object", $overloaded_class);
282
283 2 ok($session->param("overloaded_object") eq "ABCDEFG");
284
285 2 my $simple_class2 = CGI::Session::Test::SimpleObjectClass->new();
286 2 ok($simple_class2, "CGI::Session::Test::SimpleObjectClass created successfully");
287  
288 2 $simple_class2->name("Sherzod Ruzmetov");
289 2 $simple_class2->emails(0, 'sherzodr@cpan.org');
290 2 $simple_class2->emails(1, 'sherzodr@cpan.org');
291 2 $simple_class2->blogs('lost+found', 'http://author.cpan.org/');
292 2 $simple_class2->blogs('yigitlik', 'http://author.cpan.org/uz/');
293 2 my $embedded = OverloadedClass->new("Embedded");
294 2 $session->param("embedded_simple_and_overloaded",[ undef, $simple_class2, $embedded, $embedded ]);
295  
296 2 ok(!defined($session->param("embedded_simple_and_overloaded")->[0]),"First element of anonymous array undef");
297  
298 2 ok($session->param("embedded_simple_and_overloaded")->[1]->name eq "Sherzod Ruzmetov");
299 2 ok($session->param("embedded_simple_and_overloaded")->[1]->emails(1) eq 'sherzodr@cpan.org');
300 2 ok($session->param("embedded_simple_and_overloaded")->[1]->blogs('yigitlik') eq 'http://author.cpan.org/uz/');
301
302 2 ok($session->param("embedded_simple_and_overloaded")->[2] eq "Embedded");
303
304 2 ok(refaddr($session->param("embedded_simple_and_overloaded")->[2]) == refaddr($session->param("embedded_simple_and_overloaded")->[3] ),
305 "Overloaded objects have matching addresses");
306 }
307  
308  
309 SEVEN: {
310 2 ok(1, "=== 7 ===");
311 2 ▶ my $session = CGI::Session->new($self->{dsn}, $sid, $self->{args}) or die CGI::Session->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $session = "CGI::Session"->new($self->{"dsn"}, $sid, $self->{"args"}) or die "CGI::Session"->errstr 2 0 2
312 2 ok($session, "Session object created successfully");
313 2 ok($session->id eq $sid, "Previously stored object loaded successfully");
314  
315  
316 2 my $simple_object = $session->param("simple_object");
317 2 ok(ref $simple_object eq "CGI::Session::Test::SimpleObjectClass", "CGI::Session::Test::SimpleObjectClass loaded successfully");
318  
319 2 my $dsn = CGI::Session->parse_dsn($self->{dsn});
320 2 ▶ ok_later { $simple_object->name eq "Sherzod Ruzmetov" };
Subroutine __ANON__: called CC=1 SCAR=0.0
321 2 ▶ ok_later { $simple_object->emails(1) eq 'sherzodr@cpan.org' };
Subroutine __ANON__: called CC=1 SCAR=0.0
322 2 ▶ ok_later { $simple_object->emails(0) eq 'sherzodr@cpan.org' };
Subroutine __ANON__: called CC=1 SCAR=0.0
323 2 ▶ ok_later { $simple_object->blogs('lost+found') eq 'http://author.cpan.org/' };
Subroutine __ANON__: called CC=1 SCAR=0.0
324 2 ok(ref $session->param("overloaded_object") );
325 2 ok($session->param("overloaded_object") eq "ABCDEFG", "Object is still overloaded");
326 2 ok(overload::Overloaded($session->param("overloaded_object")), "Object is really overloaded");
327  
328 2 ok(!defined($session->param("embedded_simple_and_overloaded")->[0]),"First element of anonymous array undef");
329
330 2 my $simple_object2 = $session->param("embedded_simple_and_overloaded")->[1];
331 2 ok(ref $simple_object2 eq "CGI::Session::Test::SimpleObjectClass", "CGI::Session::Test::SimpleObjectClass loaded successfully");
332  
333 2 ▶ ok_later { $simple_object2->name eq "Sherzod Ruzmetov" };
Subroutine __ANON__: called CC=1 SCAR=0.0
334 2 ▶ ok_later { $simple_object2->emails(1) eq 'sherzodr@cpan.org' };
Subroutine __ANON__: called CC=1 SCAR=0.0
335 2 ▶ ok_later { $simple_object2->emails(0) eq 'sherzodr@cpan.org' };
Subroutine __ANON__: called CC=1 SCAR=0.0
336 2 ▶ ok_later { $simple_object2->blogs('lost+found') eq 'http://author.cpan.org/' };
Subroutine __ANON__: called CC=1 SCAR=0.0
337  
338
339 2 ok($session->param("embedded_simple_and_overloaded")->[2] eq "Embedded");
340 2 ok(overload::Overloaded($session->param("embedded_simple_and_overloaded")->[2]), "Object is really overloaded");
341
342 2 ok(refaddr($session->param("embedded_simple_and_overloaded")->[2]) == refaddr($session->param("embedded_simple_and_overloaded")->[3]),
343 "Overloaded objects have matching addresses");
344 2 $session->delete();
345 }
346
347 2 $CURRENT = undef;
348 2 $self->{test_number} = 0;
349 }
350  
351 186 ▶sub skip_or_run {
Subroutine skip_or_run: called CC=2 SCAR=7.0
Pod undocumented
352 186 my $test = shift;
353
354 186 $CURRENT->{test_number} ++;
355  
356 SKIP: {
357 186 ▶ if($CURRENT->{_skip}->{$CURRENT->{test_number}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CGI::Session::Test::Default::CURRENT->{"_skip"}{$CGI::Session::Test::Default::CURRENT->{"test_number"}}) 0 186 186
358 0 Test::More::skip("Test does not apply to this setup.", 1);
359 }
360
361 15 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
362 2 ▶ &{"Test::More::$test"}(@_);
Subroutine BEGIN: called CC=1 SCAR=0.0
363 }
364 }
365  
366 178 ▶sub ok { skip_or_run("ok", @_); }
Subroutine ok: called CC=1 SCAR=0.0
Pod undocumented
367 2 ▶sub use_ok { skip_or_run("use_ok", @_); }
Subroutine use_ok: called CC=1 SCAR=0.0
Pod undocumented
368 6 ▶sub is { skip_or_run("is", @_); }
Subroutine is: called CC=1 SCAR=0.0
Pod undocumented
369  
370 16 ▶sub ok_later (&;$) {
Subroutine ok_later: called CC=3 SCAR=12.0
Pod undocumented
371 16 my($code, $name) = @_;
372
373 16 $CURRENT->{test_number} ++;
374 16 ▶ $name = '' unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 16 0 16
375  
376 SKIP: {
377 16 ▶ if($CURRENT->{_skip}->{$CURRENT->{test_number}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CGI::Session::Test::Default::CURRENT->{'_skip'}{$CGI::Session::Test::Default::CURRENT->{'test_number'}}) { } 0 16 16
378 0 Test::More::skip("Test does not apply to this setup.", 1);
379 0 fail($name);
380 } else {
381 16 Test::More::ok($code->(), $name);
382 }
383 }
384 }
385  
386 2 ▶sub DESTROY { 1; }
Subroutine DESTROY: called CC=1 SCAR=0.0
387  
388  
389 package CGI::Session::Test::SimpleObjectClass;
390 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
391 15 ▶use Class::Struct;
Subroutine BEGIN: called CC=1 SCAR=0.0
392  
393 15 struct (
394 name => '$',
395 emails => '@',
396 blogs => '%'
397 );
398  
399  
400  
401 package OverloadedClass;
402  
403 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
404 15 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
405 15 '""' => \&as_string,
406 'eq' => \&equals
407 15 );
408  
409 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
410 4 return bless {
411 str_value => $_[1]
412 }, $_[0];
413 }
414  
415  
416 10 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
417 10 return $_[0]->{str_value};
418 }
419  
420 8 ▶sub equals {
Subroutine equals: called CC=1 SCAR=0.0
421 8 my ($self, $arg) = @_;
422  
423 8 return ($self->as_string eq $arg);
424 }
425  
426 15 1;
« Session/Driver.pm ↑ Summary Session/ErrorHandler.pm »