Support testing against two apps in same perl interpreter
Ash Berlin [Tue, 3 Feb 2009 21:18:50 +0000 (21:18 +0000)]
CHANGES
lib/Test/WWW/Mechanize/Catalyst.pm
t/lib/Catty.pm
t/lib/CattySession.pm
t/simple.t
t/two_app.t [new file with mode: 0644]

diff --git a/CHANGES b/CHANGES
index 4bfc51a..6201ed0 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,5 +1,14 @@
 Revision history for Perl module Test::WWW::Mechanize::Catalyst:
 
+0.50
+     - App classname no longer has to be passed to import:
+        $m = T::W::M::C->new(catalyst_app => 'Catty')
+       now works.
+     - Can now use TWMC two test two different apps in the same perl
+       interpreter due to the above change
+     - Removed Test::WWW::Mechanize::Catalyst::Aux package as it isn't needed
+       any more
+
 0.45 Mon Nov 24 20:39:19 GMT 2008
      - be forwards-compatible with Catalyst 5.80's virtual 
        domain testing (thanks Jason Gottshall)
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
 
index 3479701..17cff7f 100644 (file)
@@ -84,6 +84,14 @@ sub die : Global {
     die "erk!";
 }
 
+sub name : Global {
+    my ($self, $c) = @_;
+
+    my $html = html( $c->config->{name}, "This is the die page" );
+    $c->response->content_type("text/html");
+    $c->response->output($html);
+}
+
 sub html {
     my ( $title, $body ) = @_;
     return qq{
index 9d3138c..4c96c49 100644 (file)
@@ -35,6 +35,15 @@ sub default : Private {
     $context->response->output($html);
 }
 
+sub name : Global {
+    my ($self, $c) = @_;
+
+    my $html = html( $c->config->{name}, "This is the die page" );
+    $c->response->content_type("text/html");
+    $c->response->output($html);
+}
+
+
 sub html {
     my ( $title, $body ) = @_;
     return qq{
index 21e059b..9ebc568 100644 (file)
@@ -1,7 +1,7 @@
 #!perl
 use strict;
 use warnings;
-use lib 'lib';
+
 use Encode qw();
 use Test::More tests => 37;
 use lib 't/lib';
diff --git a/t/two_app.t b/t/two_app.t
new file mode 100644 (file)
index 0000000..a54529b
--- /dev/null
@@ -0,0 +1,28 @@
+use strict;
+use warnings;
+
+use Test::More;
+use lib 't/lib';
+
+eval {
+    require Catalyst::Plugin::Session;
+    require Catalyst::Plugin::Session::State::Cookie;
+};
+
+if ($@) {
+    diag($@);
+    plan skip_all => "Need Catalyst::Plugin::Session to run this test";
+} else {
+    plan tests => 4;
+}
+
+use Test::WWW::Mechanize::Catalyst;
+
+my $m1 = Test::WWW::Mechanize::Catalyst->new(catalyst_app => 'Catty');
+my $m2 = Test::WWW::Mechanize::Catalyst->new(catalyst_app => 'CattySession');
+
+$m1->get_ok("/name");
+$m1->title_is('Catty');
+
+$m2->get_ok("/name");
+$m2->title_is('CattySession');