Support testing against two apps in same perl interpreter
[catagits/Test-WWW-Mechanize-Catalyst.git] / lib / Test / WWW / Mechanize / Catalyst.pm
index da48764..72e4072 100644 (file)
@@ -1,16 +1,43 @@
 package Test::WWW::Mechanize::Catalyst;
+
 use strict;
 use warnings;
+
+use Carp qw/croak/;
+require Catalyst::Test; # Do not call import
 use Encode qw();
 use HTML::Entities;
 use Test::WWW::Mechanize;
+
 use base qw(Test::WWW::Mechanize);
+
 our $VERSION = '0.45';
 my $Test = Test::Builder->new();
 
 # the reason for the auxiliary package is that both WWW::Mechanize and
 # Catalyst::Test have a subroutine named 'request'
 
+our $APP_CLASS;
+sub new {
+  my ($class, %args) = @_;
+
+  my $app;
+  if (exists $args{catalyst_app}) {
+    $app = $args{catalyst_app};
+
+    require Class::Inspector->filename( $app )
+      unless Class::Inspector->loaded( $app );
+  } elsif (!defined $APP_CLASS) {
+    croak 'Please provide a catalyst_app option or import Test::WWW::Mechanize::Catalyst with a class name'; 
+  } else {
+    $app = $APP_CLASS;
+  }
+
+  my $self = $class->SUPER::new(%args);
+  $self->{catalyst_app} = $app;
+  return $self;
+}
+
 sub allow_external {
     my ( $self, $value ) = @_;
     return $self->{allow_external} unless defined $value;
@@ -37,7 +64,7 @@ sub _make_request {
     my @creds = $self->get_basic_credentials( "Basic", $uri );
     $request->authorization_basic( @creds ) if @creds;
 
-    my $response = Test::WWW::Mechanize::Catalyst::Aux::request($request);
+    my $response = Catalyst::Test::local_request($self->{catalyst_app}, $request);
     $response->header( 'Content-Base', $request->uri );
     $response->request($request);
     if ( $request->uri->as_string =~ m{^/} ) {
@@ -92,19 +119,14 @@ sub _make_request {
 }
 
 sub import {
-    Test::WWW::Mechanize::Catalyst::Aux::import(@_);
+  my ($class, $app) = @_;
+  if (defined $app) {
+    require Class::Inspector->filename( $app )
+      unless Class::Inspector->loaded( $app );
+    $APP_CLASS = $app; 
+  }
 }
 
-package Test::WWW::Mechanize::Catalyst::Aux;
-
-sub import {
-    my ( $class, @args ) = @_;
-    eval {
-        require Catalyst::Test;
-        Catalyst::Test::import(@_);
-    };
-    warn $@ if $@;
-}
 
 1;
 
@@ -356,11 +378,13 @@ L<Test::WWW::Mechanize>, L<WWW::Mechanize>.
 
 =head1 AUTHOR
 
+Current Maintainer: Ash Berlin C<< <ash@cpan.org> >>
+
 Leon Brocard, C<< <acme@astray.com> >>
 
 =head1 COPYRIGHT
 
-Copyright (C) 2005-7, Leon Brocard
+Copyright (C) 2005-8, Leon Brocard
 
 =head1 LICENSE