Session/Test/Default.pm

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