From 883b145c44e18a12d1b249459356776a1bb8cf8f Mon Sep 17 00:00:00 2001 From: Graham Knop Date: Wed, 9 Jun 2010 07:03:49 -0500 Subject: [PATCH] some WebGUI::Test cleanups --- t/lib/WebGUI/Test.pm | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/t/lib/WebGUI/Test.pm b/t/lib/WebGUI/Test.pm index f0a417b9f..085ceb837 100644 --- a/t/lib/WebGUI/Test.pm +++ b/t/lib/WebGUI/Test.pm @@ -39,6 +39,7 @@ use List::MoreUtils qw( any ); use Carp qw( carp croak ); use JSON qw( from_json to_json ); use Scope::Guard; +use Try::Tiny; use WebGUI::Paths -inc; our $WEBGUI_TEST_ROOT = File::Spec->catdir( @@ -141,8 +142,6 @@ sub newSession { my $noCleanup = shift; require WebGUI::Session; my $session = WebGUI::Session->open( $CLASS->config, newEnv() ); - # my $pseudoRequest = WebGUI::PseudoRequest->new; - # $session->{_request} = $pseudoRequest; if ( ! $noCleanup ) { $CLASS->addToCleanup($session); } @@ -154,11 +153,24 @@ sub newEnv { require HTTP::Message::PSGI; require HTTP::Request::Common; my $config = $CLASS->config; - my $url = 'http://' . $config->get('sitename'); + my $url = 'http://' . $config->get('sitename')->[0]; my $env = HTTP::Request->new( $form ? ( POST => $url, [ %$form ] ) : ( GET => $url ) )->to_psgi; return $env; } +sub clientTest (&) { + my $client = shift; + local $ENV{WEBGUI_CONFIG} = $CLASS->file; + my $test_psgi = Plack::Util::load_psgi( + $CLASS->config->get('psgiFile') + || WebGUI::Paths->defaultPSGI, + ); + Plack::Test::test_psgi( + app => $test_psgi, + client => $client, + ); +} + #---------------------------------------------------------------------------- =head2 mockAssetId ( $assetId, $object ) @@ -419,7 +431,6 @@ sub getPage { # Return the page's output return join '', @{$session->response->body}; - } #---------------------------------------------------------------------------- @@ -841,7 +852,14 @@ Example call: }, 'SQL' => sub { my (undef, $sql) = @_; - return $CLASS->session->db->dbh->prepare($sql); + my $db = $CLASS->session->db; + my @params; + if ( ref $sql ) { + ( $sql, @params ) = @$sql; + } + return sub { + $db->do( $sql, {}, @params ); + } }, ); @@ -922,7 +940,9 @@ Example call: 'CODE' => sub { (shift)->(); }, - 'SQL' => 'execute', + 'SQL' => sub { + (shift)->(); + }, ); sub cleanupGuard { @@ -1007,7 +1027,7 @@ This is a class method. my @guarded; sub addToCleanup { shift - if eval { $_[0]->isa($CLASS) }; + if try { $_[0]->isa($CLASS) }; push @guarded, cleanupGuard(@_); } @@ -1030,13 +1050,4 @@ sub cleanup { } } -#---------------------------------------------------------------------------- - -=head1 BUGS - -When trying to load the APR module, perl invariably throws an Out Of Memory -error. For this reason, getPage disables header processing. - -=cut - 1;