blib/lib/Labyrinth/Test/Harness.pm

filter: Statementstmt 46.3% 126 / 272 Branchbran 23.2% 20 / 86 Conditioncond 20.0% 4 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 65.9% 29 / 44 Podpod 100% 20 / 20 totaltotal 43.7% 199 / 455 SCAR 68.7
CC
64
Cov
40%
create_mysql_databases
90.0
prep
73.0
create_config
72.0
CRAP 962.9
↑ Summary
1 package Labyrinth::Test::Harness;
2  
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 $|++;
6  
7 7 our $VERSION = '1.09';
8  
9 #----------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 Labyrinth::Test::Harness - Test Harness for Labyrinth Plugin modules
14  
15 =head1 SYNOPSIS
16  
17 my $harness = Labyrinth::Test::Harness->new( %options );
18  
19 my $res = $harness->prep('file1.sql','file2.sql');
20  
21 $res = $harness->labyrinth(@plugins);
22  
23 $res = $harness->action('Base::Admin');
24  
25 $harness->refresh( \@plugins );
26 $harness->refresh(
27 \@plugins,
28 { test1 => 1 },
29 { test2 => 2 } );
30  
31 $harness->cleanup;
32  
33 $harness->clear();
34 my $vars = $harness->vars;
35 my $params = $harness->params;
36 my $settings = $harness->settings;
37 $harness->set_vars( name => 'Test', test => 1 );
38 $harness->set_params( name => 'Test', test => 1 );
39 $harness->set_settings( name => 'Test', test => 1 );
40  
41 my $error = $harness->error;
42  
43 my $config = $harness->config;
44 my $directory = $harness->directory;
45 $harness->copy_files($source,$target);
46  
47 =head1 DESCRIPTION
48  
49 Contains all the harness code around Labyrinth, to enable plugin testing.
50  
51 =cut
52  
53 #----------------------------------------------------------------------------
54 # Libraries
55  
56 7 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 7 ▶use Config::IniFiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 7 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 7 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 7 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 7 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 7 ▶use Module::Pluggable search_path => ['Labyrinth::Plugin'];
Subroutine BEGIN: called CC=1 SCAR=0.0
64 7 ▶use Test::Database;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 # Required Core
67 7 ▶use Labyrinth;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 7 ▶use Labyrinth::Audit;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 7 ▶use Labyrinth::DTUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 7 ▶use Labyrinth::Globals qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
71 7 ▶use Labyrinth::Mailer;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 7 ▶use Labyrinth::Plugins;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 7 ▶use Labyrinth::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 7 ▶use Labyrinth::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 7 ▶use Labyrinth::Support;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 7 ▶use Labyrinth::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 7 ▶use Labyrinth::Variables;
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79 #----------------------------------------------------------------------------
80 # Default Test Variables
81  
82 7 my $CONFIG = 't/_DBDIR/test-config.ini';
83 7 my $DIRECTORY = 't/_DBDIR';
84  
85 #----------------------------------------------------------------------------
86 # Class Methods
87  
88 sub new {
89 2 ▶ my ($class, %hash) = @_;
Subroutine new: called CC=4 SCAR=15.2
Pod documented
90 2 my $self = {};
91 2 bless $self, $class;
92  
93 2 ▶ $self->keep( $hash{keep} || 0 );
Condition50%1/2
$hash{'keep'} || 0
l!l
02
MC/DC0%0/1
$hash{'keep'} || 0
$hash{'keep'}
Truth table50%1/2
A
A= $hash{'keep'}
Aresult
11
00
94 2 ▶ $self->config( $hash{config} || $CONFIG );
Condition33%1/3
$hash{'config'} || $CONFIG
l!l&&r!l&&!r
020
MC/DC0%0/2
$hash{'config'} || $CONFIG
$hash{'config'} $CONFIG
Truth table33%1/3
A || B
A= $hash{'config'}B= $CONFIG
ABresult
1X1
011
000
95 2 ▶ $self->directory( $hash{directory} || $DIRECTORY );
Condition33%1/3
$hash{'directory'} || $DIRECTORY
l!l&&r!l&&!r
020
MC/DC0%0/2
$hash{'directory'} || $DIRECTORY
$hash{'directory'} $DIRECTORY
Truth table33%1/3
A || B
A= $hash{'directory'}B= $DIRECTORY
ABresult
1X1
011
000
96  
97 2 return $self;
98 }
99  
100 sub DESTROY {
101 2 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=7.2
102 2 ▶ $self->cleanup unless($self->keep);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keep 2 0 2
103 }
104  
105 #----------------------------------------------------------------------------
106 # Object Methods
107  
108 7 __PACKAGE__->mk_accessors(qw( config directory keep ));
109  
110 sub prep {
111 1 ▶ my ($self,%hash) = @_;
Subroutine prep: called CC=15 SCAR=42.9
Pod documented
112 1 $self->{error} = '';
113  
114 1 my $directory = $self->directory;
115  
116 # prep test directories
117 1 rmtree($directory);
118 1 ▶ mkpath($directory) or ( $self->{error} = "cannot create test directory" && return 0 );
Branch: 1 branch
BranchTrueFalseTotal
mkpath($directory) or $self->{"error"} = (return 0) 0 1 1
119  
120 1 for my $dir ('html','cgi-bin') {
121 2 ▶ next unless(-d "vhost/$dir");
Branch: 1 branch
BranchTrueFalseTotal
unless -d "vhost/$dir" 1 1 2
122 1 ▶ unless ($self->copy_files("vhost/$dir","$directory/$dir")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->copy_files("vhost/$dir", "$directory/$dir")) 0 1 1
123 0 $self->{error} = "cannot create test files: " . $self->{error};
124 0 return 0;
125 }
126 }
127  
128 1 ▶ mkpath("$directory/html/cache") or ( $self->{error} = "cannot create cache directory" && return 0 );
Branch: 1 branch
BranchTrueFalseTotal
mkpath("$directory/html/cache") or $self->{"error"} = (return 0) 0 1 1
129  
130 # copy additional files
131 1 ▶ if($hash{files}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash{"files"}) 0 1 1
132 0 for my $source (keys %{$hash{files}}) {
133 0 my $target = "$directory/$hash{files}{$source}";
134 0 ▶ unless ($self->copy_file($source,$target)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->copy_file($source, $target)) 0 0 0
135 0 $self->{error} = "cannot create test files: " . $self->{error};
136 0 return 0;
137 }
138 }
139 }
140  
141 # prep database
142 1 my $td1 = Test::Database->handle( 'mysql' );
143 1 ▶ unless($td1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($td1) 1 0 1
144 7 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
145 1 $self->{error} = "Unable to load a test database instance.";
146 1 return 0;
147 }
148  
149 0 create_mysql_databases($td1,$hash{sql});
150  
151 0 my %opts;
152 0 ($opts{dsn}, $opts{dbuser}, $opts{dbpass}) = $td1->connection_info();
153 0 ($opts{driver}) = $opts{dsn} =~ /dbi:([^;:]+)/;
154 0 ($opts{database}) = $opts{dsn} =~ /database=([^;]+)/;
155 0 ▶ ($opts{database}) = $opts{dsn} =~ /dbname=([^;]+)/ unless($opts{database});
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"database"} 0 0 0
156 0 ($opts{dbhost}) = $opts{dsn} =~ /host=([^;]+)/;
157 0 ($opts{dbport}) = $opts{dsn} =~ /port=([^;]+)/;
158 0 ▶ my %db_config = map {my $v = $opts{$_}; defined($v) ? ($_ => $v) : () }
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 0 0 0
159 qw(driver database dbfile dbhost dbport dbuser dbpass);
160  
161 # prep config files
162 0 ▶ unless( $self->create_config(\%db_config,$hash{config}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->create_config(\%db_config, $hash{"config"})) 0 0 0
163 0 $self->{error} = "Failed to create config file";
164 0 return 0;
165 }
166  
167 # prep environment variables
168  
169 0 my %env = (
170 SERVER_PROTOCOL => 'http',
171 SERVER_PORT => 80,
172 HTTP_HOST => 'example.com',
173 REQUEST_URI => '/',
174 PATH_INFO => undef
175 );
176  
177 0 for my $key (keys %env) {
178 0 ▶ $ENV{$key} = $hash{ENV}{$key} || $env{$key};
Condition0%0/3
$hash{'ENV'}{$key} || $env{$key}
l!l&&r!l&&!r
000
MC/DC0%0/2
$hash{'ENV'}{$key} || $env{$key}
$hash{'ENV'}{$key} $env{$key}
Truth table0%0/3
A || B
A= $hash{'ENV'}{$key}B= $env{$key}
ABresult
1X1
011
000
179 }
180  
181 0 return 1;
182 }
183  
184 sub cleanup {
185 3 ▶ my ($self) = @_;
Subroutine cleanup: called CC=3 SCAR=11.2
Pod documented
186  
187 3 my $directory = $self->directory;
188  
189 # remove test directories
190 3 rmtree($directory);
191  
192 # remove test database
193 3 ▶ eval "use Test::Database";
Subroutine BEGIN: called CC=1 SCAR=0.0
194 3 ▶ return if($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
195  
196 3 my $td1 = Test::Database->handle( 'mysql' );
197 3 ▶ return unless($td1);
Branch: 1 branch
BranchTrueFalseTotal
unless $td1 3 0 3
198  
199 0 $td1->{driver}->drop_database($td1->name);
200 }
201  
202 sub labyrinth {
203 0 ▶ my ($self,@plugins) = @_;
Subroutine labyrinth: not called CC=2 SCAR=17.9
Pod documented
204 0 $self->{error} = '';
205  
206 0 my $config = $self->config;
207  
208 0 eval {
209 # configure labyrinth instance
210 0 $self->{labyrinth} = Labyrinth->new;
211  
212 0 Labyrinth::Variables::init(); # initial standard variable values
213  
214 0 UnPublish(); # Start a fresh slate
215 0 LoadSettings($config); # Load All Global Settings
216  
217 0 DBConnect();
218  
219 0 load_plugins( @plugins );
220 };
221  
222 0 ▶ return 1 unless($@);
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
223 0 $self->{error} = "Failed to load Labyrinth: $@";
224 0 return 0;
225 }
226  
227 sub action {
228 0 ▶ my ($self,$action) = @_;
Subroutine action: not called CC=2 SCAR=17.9
Pod documented
229 0 $self->{error} = '';
230  
231 0 eval {
232 # run plugin action
233 0 $self->{labyrinth}->action($action);
234 };
235  
236 0 ▶ return 1 unless($@);
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
237 0 $self->{error} = "Failed to run action: $action: $@";
238 0 return 0;
239 }
240  
241 sub refresh {
242 0 ▶ my ($self,$plugins,$vars,$params,$settings) = @_;
Subroutine refresh: not called CC=4 SCAR=30.0
Pod documented
243  
244 0 $self->labyrinth(@$plugins);
245 0 ▶ $self->set_vars( %$vars ) if($vars);
Branch: 1 branch
BranchTrueFalseTotal
if $vars 0 0 0
246 0 ▶ $self->set_params( %$params ) if($params);
Branch: 1 branch
BranchTrueFalseTotal
if $params 0 0 0
247 0 ▶ $self->set_settings( %$settings ) if($settings);
Branch: 1 branch
BranchTrueFalseTotal
if $settings 0 0 0
248 }
249  
250 sub login {
251 0 ▶ my ($self,$id) = @_;
Subroutine login: not called CC=4 SCAR=30.0
Pod documented
252 0 ▶ return unless($dbi && $id);
Branch: 1 branch
BranchTrueFalseTotal
unless $Labyrinth::Test::Harness::dbi and $id 0 0 0
Condition0%0/3
$Labyrinth::Test::Harness::dbi and $id
!ll&&!rl&&r
000
MC/DC0%0/2
$Labyrinth::Test::Harness::dbi and $id
$Labyrinth::Test::Harness::dbi $id
Truth table0%0/3
A and B
A= $Labyrinth::Test::Harness::dbiB= $id
ABresult
0X0
100
111
253 0 my @user = $dbi->GetQuery('hash','GetUserByID',$id);
254 0 ▶ Labyrinth::Session::InternalLogin($user[0]) if(@user);
Branch: 1 branch
BranchTrueFalseTotal
if @user 0 0 0
255 }
256  
257 sub clear {
258 0 ▶ my ($self) = @_;
Subroutine clear: not called CC=1 SCAR=6.9
Pod documented
259 0 %tvars = ();
260 0 %cgiparams = ();
261 }
262  
263 sub vars {
264 0 ▶ my ($self) = @_;
Subroutine vars: not called CC=1 SCAR=6.9
Pod documented
265 0 return \%tvars;
266 }
267  
268 sub set_vars {
269 0 ▶ my ($self,%hash) = @_;
Subroutine set_vars: not called CC=2 SCAR=17.9
Pod documented
270 0 for my $name (keys %hash) {
271 0 $tvars{$name} = $hash{$name}
272 }
273 }
274  
275 sub params {
276 0 ▶ my ($self) = @_;
Subroutine params: not called CC=1 SCAR=6.9
Pod documented
277 0 return \%cgiparams;
278 }
279  
280 sub set_params {
281 0 ▶ my ($self,%hash) = @_;
Subroutine set_params: not called CC=2 SCAR=17.9
Pod documented
282 0 for my $name (keys %hash) {
283 0 $cgiparams{$name} = $hash{$name}
284 }
285 }
286  
287 sub settings {
288 0 ▶ my ($self) = @_;
Subroutine settings: not called CC=1 SCAR=6.9
Pod documented
289 0 return \%settings;
290 }
291  
292 sub set_settings {
293 0 ▶ my ($self,%hash) = @_;
Subroutine set_settings: not called CC=2 SCAR=17.9
Pod documented
294 0 for my $name (keys %hash) {
295 0 $settings{$name} = $hash{$name}
296 }
297 }
298  
299 sub error {
300 1 ▶ my ($self) = @_;
Subroutine error: called CC=1 SCAR=0.0
Pod documented
301 1 return $self->{error};
302 }
303  
304 #----------------------------------------------------------------------------
305 # Internal Functions
306  
307 sub copy_files {
308 1 ▶ my ($self,$source_dir,$target_dir) = @_;
Subroutine copy_files: called CC=11 SCAR=28.6
Pod documented
309  
310 1 ▶ unless($source_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($source_dir) 0 1 1
311 0 $self->{error} = "no source directory given";
312 0 return 0;
313 }
314 1 ▶ unless($target_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($target_dir) 0 1 1
315 0 $self->{error} = "no target directory given";
316 0 return 0;
317 }
318 1 ▶ unless(-f $source_dir || -d $source_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $source_dir or -d $source_dir) 0 1 1
Condition33%1/3
-f $source_dir or -d $source_dir
l!l&&r!l&&!r
010
MC/DC0%0/2
-f $source_dir or -d $source_dir
-f $source_dir -d $source_dir
Truth table33%1/3
A or B
A= -f $source_dirB= -d $source_dir
ABresult
1X1
011
000
319 0 $self->{error} = "failed to find source directory/file: $source_dir";
320 0 return 0;
321 }
322  
323 1 my @dirs = ($source_dir);
324 1 ▶ while(@dirs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dirs) 3 1 4
325 3 my $dir = shift @dirs;
326  
327 3 my @files = glob("$dir/*");
328  
329 3 for my $filename (@files) {
330 5 my $source = $filename;
331 5 ▶ if(-f $source) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $source) { } 3 2 5
elsif (-d $source) { } 2 0 2
332 3 my $target = $filename;
333 3 $target =~ s/^$source_dir/$target_dir/;
334 3 ▶ next if(-f $target);
Branch: 1 branch
BranchTrueFalseTotal
if -f $target 0 3 3
335  
336 3 mkpath( dirname($target) );
337 3 ▶ if(-d dirname($target)) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d dirname($target)) { } 3 0 3
338 3 copy( $source, $target );
339 } else {
340 0 $self->{error} = "failed to created directory: " . dirname($target);
341 0 return 0;
342 }
343 } elsif(-d $source) {
344 2 push @dirs, $source;
345  
346 } else {
347 0 $self->{error} = "failed to to find source: $source";
348 0 return 0;
349 }
350 }
351 }
352  
353 1 return 1;
354 }
355  
356 sub copy_file {
357 0 ▶ my ($self,$source,$target) = @_;
Subroutine copy_file: not called CC=6 SCAR=37.4
Pod documented
358  
359 0 ▶ unless($source) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($source) 0 0 0
360 0 $self->{error} = "no source file given";
361 0 return 0;
362 }
363 0 ▶ unless($target) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($target) 0 0 0
364 0 $self->{error} = "no target file given";
365 0 return 0;
366 }
367 0 ▶ unless(-f $source) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $source) 0 0 0
368 0 $self->{error} = "failed to find source file: $source";
369 0 return 0;
370 }
371  
372 0 my $dir = dirname($target);
373 0 ▶ mkpath($dir) unless(-d $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
374 0 ▶ if(-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir) { } 0 0 0
375 0 copy( $source, $target );
376 } else {
377 0 $self->{error} = "failed to created directory: $dir";
378 0 return 0;
379 }
380 }
381  
382 sub create_config {
383 0 ▶ my ($self,$db_config,$user_config) = @_;
Subroutine create_config: not called CC=8 SCAR=42.8
Pod documented
384  
385 0 my $config = $self->config;
386 0 my $directory = $self->directory;
387  
388 # main config
389 0 ▶ unlink $config if -f $config;
Branch: 1 branch
BranchTrueFalseTotal
if -f $config 0 0 0
390  
391 0 my %CONFIG = (
392 PROJECT => {
393 icode => 'testsite',
394 iname => 'Test Site',
395 administrator => 'admin@example.com',
396 mailhost => '',
397 cookiename => 'session',
398 timeout => 3600,
399 autoguest => 1,
400 copyright => '2013-2014 Me',
401 lastpagereturn => 0,
402 minpasslen => 6,
403 maxpasslen => 20,
404 evalperl => 1
405 },
406 INTERNAL => {
407 phrasebook => "$directory/cgi-bin/config/phrasebook.ini",
408 logfile => "$directory/html/cache/audit.log",
409 loglevel => 4,
410 logclear => 1
411 },
412 HTTP => {
413 webpath => '',
414 cgipath => '/cgi-bin',
415 realm => 'public',
416 basedir => "$directory",
417 webdir => "$directory/html",
418 cgidir => "$directory/cgi-bin",
419 requests => "$directory/cgi-bin/config/requests"
420 },
421 CMS => {
422 htmltags => '+img',
423 maxpicwidth => 500,
424 randpicwidth => 400,
425 blank => 'images/blank.png',
426 testing => 0
427 }
428 );
429  
430 0 ▶ if($user_config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_config) 0 0 0
431 0 for my $section (keys %$user_config) {
432 0 for my $key (keys %{$user_config->{$section}}) {
433 0 $CONFIG{$section}{$key} = $user_config->{$section}{$key};
434 }
435 }
436 }
437  
438 # just in case, do this last to avoid being overwritten.
439 0 $CONFIG{DATABASE} = $db_config;
440  
441 0 ▶ my $fh = IO::File->new($config,'w+') or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($config, "w+") or return 0 0 0 0
442 0 for my $section (keys %CONFIG) {
443 0 print $fh "[$section]\n";
444 0 for my $key (keys %{$CONFIG{$section}}) {
445 0 print $fh "$key=$CONFIG{$section}{$key}\n";
446 }
447 0 print $fh "\n";
448 }
449  
450 0 $fh->close;
451 0 return 1;
452 }
453  
454 # this is primitive, but works :)
455  
456 sub create_mysql_databases {
457 0 ▶ my ($db1,$files) = @_;
Subroutine create_mysql_databases: not called CC=9 SCAR=45.0
Pod documented
458  
459 0 ▶ return unless($files && @$files > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $files and @$files > 0 0 0 0
Condition0%0/3
$files and @$files > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$files and @$files > 0
$files @$files > 0
Truth table0%0/3
A and B
A= $filesB= @$files > 0
ABresult
0X0
100
111
460  
461 0 my (@statements);
462 0 my $sql = '';
463  
464 0 for my $file (@$files) {
465 #print STDERR "# file=$file\n";
466 0 ▶ my $fh = IO::File->new($file,'r') or next;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($file, "r") or next 0 0 0
467 0 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
468 0 ▶ next if(/^--/); # ignore comment lines
Branch: 1 branch
BranchTrueFalseTotal
if /^--/ 0 0 0
469 0 s/;\s+--.*/;/; # remove end of line comments
470 0 s/\s+$//; # remove trailing spaces
471 0 ▶ next unless($_);
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 0 0
472  
473 #print STDERR "# line=$_\n";
474 0 $sql .= ' ' . $_;
475 #print STDERR "# sql=$sql\n";
476 #exit;
477 0 ▶ if($sql =~ /;$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /;$/) 0 0 0
478 0 $sql =~ s/;$//;
479 0 push @statements, $sql;
480 0 $sql = '';
481 }
482 }
483 0 $fh->close;
484 }
485  
486 #print STDERR "# statements=".join("\n# ",@statements)."\n";
487 0 dosql($db1,\@statements);
488 }
489  
490 sub dosql {
491 0 ▶ my ($db,$sql) = @_;
Subroutine dosql: not called CC=3 SCAR=24.8
Pod documented
492  
493 0 for(@$sql) {
494 #print STDERR "#SQL: [$db] $_\n";
495 0 eval { $db->dbh->do($_); };
496 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
497 0 diag $@;
498 0 return 1;
499 }
500 }
501  
502 0 return 0;
503 }
504  
505 7 1;
506  
507 __END__
508  
509 =head1 METHODS
510  
511 =head2 The Constructor
512  
513 =over
514  
515 =item new( %options )
516  
517 Harness object constructor.
518  
519 Defines a default config file and directory, unless otherwise provided.
520  
521 Options available are:
522  
523 keep => 1 # default is 0
524 config => $config
525 directory => $directory
526 env => \%environment
527  
528 When a harness object goes out of scope, the DESTROY method is called, unless
529 'keep' is a true value, the cleanup method will be automatically called.
530  
531 'config' and 'directory' preset the respective file and directory names,
532 although these can also be set by the respective methods calls prior to
533 calling the prep method.
534  
535 'env' can contain settings for internal environment variables that are used
536 within Labyrinth. The environment variables that can be overridden, together
537 with their current defaults, are:
538  
539 SERVER_PROTOCOL => 'http',
540 SERVER_PORT => 80,
541 HTTP_HOST => 'example.com',
542 REQUEST_URI => '/',
543 PATH_INFO => undef
544  
545 =back
546  
547 =head2 Public Methods
548  
549 =over
550  
551 =item keep( $value )
552  
553 If set to a true value, don't cleanup when object oes out of scope.
554  
555 =item config( $value )
556  
557 Set the name of the file to use as the configuration file. Default is
558 't/_DBDIR/test-config.ini'.
559  
560 =item directory( $value )
561  
562 Set the name of the working directory. Default is 't/_DBDIR'.
563  
564 =item prep( %options )
565  
566 Prepares the environment. Copies files from the current vhost directory,
567 creates a database, and runs the necessary SQL to create the required tables
568 and add the appropriate data. Saves the configuration settings to the
569 designated config file.
570  
571 The options allow for overides and additional test information to be provided.
572 Example option are:
573  
574 sql => [ 'file1', 'file2' ],
575 files => { 'file3.jpg' => 'html/images/file3.jpg' },
576 config => { SECTION => { key => 'value' } }
577  
578 SQL files listed in 'sql' are loaded and run prior to testing. After the
579 initial copyin of the vhost files, the list in 'files' are copied. Note that
580 files should be listed as source => target (beneath the $directory) as a
581 key/value pair.
582  
583 In the 'config' list, the additional sections and parameters can be provided,
584 which override the default configurations if necessary. Default sections are
585 PROJECT, INTERNAL, HTTP and CMS. DATABASE is also provide, but cannot be
586 overriden.
587  
588 =item labyrinth( @plugins )
589  
590 Loads an instance of Labyrinth. Will also pre-load the list of given plugins.
591  
592 =item action( $action )
593  
594 Runs the named plugin action.
595  
596 =item refresh( \@plugins, $vars_hash, $params_hash )
597  
598 Refreshes the current instance by reloading the Labyrinth instances, together
599 with the name plugins, and adding the variables and parameters to the current
600 internal hashes.
601  
602 Essentially a short cut to calling labyrinth(), set_vars() and set_params()
603 separately.
604  
605 =item login( $id )
606  
607 Given a user id, will automatically log that user into the system. This then
608 loads the appropriate permissions for that user.
609  
610 =item cleanup
611  
612 Clean up the instance, removes the current directory and deletes the test
613 database.
614  
615 =back
616  
617 =head2 Internal Variables
618  
619 =over
620  
621 =item clear
622  
623 Clear the internal variables and parameters hashes.
624  
625 =item params
626  
627 returns the current parameters hash.
628  
629 =item set_params( %hash )
630  
631 Adds the given parameters to the current paraments hash.
632  
633 =item vars
634  
635 returns the current variables hash.
636  
637 =item set_vars( %hash )
638  
639 Adds the given variables to the current variables hash.
640  
641 =item settings
642  
643 returns the current settings hash.
644  
645 =item set_settings( %hash )
646  
647 Adds the given variables to the current settings hash.
648  
649 =item error
650  
651 Returns the last error recorded.
652  
653 =back
654  
655 =head2 Internal Methods
656  
657 =over
658  
659 =item copy_files( $source, $target )
660  
661 Copies files between the source and target directories.
662  
663 =item copy_file( $source, $target )
664  
665 Copies a single file from source to target.
666  
667 =item create_config( $db_config )
668  
669 Creates a configuration file.
670  
671 =item create_mysql_databases( @files )
672  
673 Creates the test database. The @files array, lists the SQL files containing
674 SQL statements to run on the test database.
675  
676 =item dosql
677  
678 Runs an SQL command.
679  
680 =back
681  
682 =head1 SEE ALSO
683  
684 L<Labyrinth>
685  
686 L<http://labyrinth.missbarbell.co.uk>
687  
688 =head1 AUTHOR
689  
690 Barbie, <barbie@missbarbell.co.uk> for
691 Miss Barbell Productions, L<http://www.missbarbell.co.uk/>
692  
693 =head1 COPYRIGHT & LICENSE
694  
695 Copyright (C) 2014-2015 Barbie for Miss Barbell Productions
696 All Rights Reserved.
697  
698 This module is free software; you can redistribute it and/or
699 modify it under the Artistic License 2.0.
700  
701 =cut
↑ Summary