From: Ash Berlin Date: Tue, 3 Feb 2009 21:18:50 +0000 (+0000) Subject: Support testing against two apps in same perl interpreter X-Git-Tag: 0.51~13 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FTest-WWW-Mechanize-Catalyst.git;a=commitdiff_plain;h=254eca4135088b598bc59093b98475992b4bf6f5 Support testing against two apps in same perl interpreter --- diff --git a/CHANGES b/CHANGES index 4bfc51a..6201ed0 100644 --- 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) diff --git a/lib/Test/WWW/Mechanize/Catalyst.pm b/lib/Test/WWW/Mechanize/Catalyst.pm index da48764..72e4072 100644 --- a/lib/Test/WWW/Mechanize/Catalyst.pm +++ b/lib/Test/WWW/Mechanize/Catalyst.pm @@ -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, L. =head1 AUTHOR +Current Maintainer: Ash Berlin C<< >> + Leon Brocard, C<< >> =head1 COPYRIGHT -Copyright (C) 2005-7, Leon Brocard +Copyright (C) 2005-8, Leon Brocard =head1 LICENSE diff --git a/t/lib/Catty.pm b/t/lib/Catty.pm index 3479701..17cff7f 100644 --- a/t/lib/Catty.pm +++ b/t/lib/Catty.pm @@ -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{ diff --git a/t/lib/CattySession.pm b/t/lib/CattySession.pm index 9d3138c..4c96c49 100644 --- a/t/lib/CattySession.pm +++ b/t/lib/CattySession.pm @@ -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{ diff --git a/t/simple.t b/t/simple.t index 21e059b..9ebc568 100644 --- a/t/simple.t +++ b/t/simple.t @@ -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 index 0000000..a54529b --- /dev/null +++ b/t/two_app.t @@ -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');