Bump to v0.06, adding in Mason tests and better config handling
J. Shirley [Wed, 22 Aug 2007 16:31:23 +0000 (16:31 +0000)]
14 files changed:
Changes
MANIFEST
Makefile.PL
lib/Catalyst/View/Email.pm
lib/Catalyst/View/Email/Template.pm
t/05template.t
t/06config.t
t/07mason.t [new file with mode: 0644]
t/lib/TestApp.pm
t/lib/TestApp/Controller/Root.pm
t/lib/TestApp/View/Mason.pm [new file with mode: 0644]
t/lib/TestApp/View/TT.pm
t/root/text_html/test.tt
t/root/text_plain/test.tt

diff --git a/Changes b/Changes
index 101f6db..f5734d8 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,12 @@
 Revision history for Perl extension Catalyst::View::Email.
 
+0.06
+        - Fixing some slight issues with configuration not being handled
+          appropriately (thanks dwc and mst)
+0.05
+        - Better support for configuration
+        - Adding Mason tests
+
 0.04
         - Fixing MANIFEST, distribution
 
index eb51b70..57b49ce 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -22,11 +22,15 @@ t/02pod.t
 t/04basic.t
 t/05template.t
 t/06config.t
+t/07mason.t
 t/lib/TestApp.pm
 t/lib/TestApp/Controller/Root.pm
 t/lib/TestApp/View/Email.pm
 t/lib/TestApp/View/Email/AppConfig.pm
 t/lib/TestApp/View/Email/Template.pm
+t/lib/TestApp/View/Mason.pm
 t/lib/TestApp/View/TT.pm
+t/root/text_html/test.m
 t/root/text_html/test.tt
+t/root/text_plain/test.m
 t/root/text_plain/test.tt
index 38a0ff7..b911820 100644 (file)
@@ -10,7 +10,13 @@ requires 'Email::Send'          => '2.185';
 requires 'Email::MIME'          => '1.859';
 requires 'Email::MIME::Creator' => '1.453';
 
-build_requires 'Catalyst::View::TT';
+feature 'Template Toolkit Support',
+    -default => 1,
+    'Catalyst::View::TT' => 0;
+
+feature 'Mason Support',
+    -default => 0,
+    'Catalyst::View::Mason';
 
 auto_install;
 WriteAll;
index b5593a4..7fa09dc 100644 (file)
@@ -11,7 +11,7 @@ use Email::MIME::Creator;
 
 use base qw|Catalyst::View|;
 
-our $VERSION = '0.05';
+our $VERSION = '0.06';
 
 __PACKAGE__->mk_accessors(qw(sender stash_key content_type mailer));
 
index 0202714..9d73778 100644 (file)
@@ -10,7 +10,9 @@ use Email::MIME::Creator;
 
 use base qw|Catalyst::View::Email|;
 
-our $VERSION = '0.03';
+our $VERSION = '0.06';
+
+__PACKAGE__->mk_accessors( qw(default_view template_prefix) );
 
 =head1 NAME
 
@@ -97,13 +99,13 @@ __PACKAGE__->config(
 sub process {
     my ( $self, $c ) = @_;
 
-    my $stash_key       = $self->config->{stash_key} || 'email';
+    my $stash_key = $self->stash_key || 'email';
 
     croak "No template specified for rendering"
         unless $c->stash->{$stash_key}->{template} or
                 $c->stash->{$stash_key}->{templates};
     # Where to look
-    my $template_prefix = $self->config->{template_prefix};
+    my $template_prefix = $self->template_prefix;
     my @templates = ();
 
     if ( $c->stash->{$stash_key}->{templates} && !ref $c->stash->{$stash_key}->{templates}[0]) {
@@ -116,7 +118,7 @@ sub process {
             $c->stash->{$stash_key}->{template});
     }
    
-    my $default_view = $c->view( $self->config->{default_view} );
+    my $default_view = $c->view( $self->default_view );
 
     unless ( $default_view->can('render') ) {
         croak "Email::Template's configured view does not have a render method!";
@@ -137,8 +139,13 @@ sub process {
         } else {
             $content_type = 'text/plain';
         }
-        my $output = $default_view->render( $c, $template,
-            { content_type => $content_type, %{$c->stash} });
+
+        my $output = $default_view->render( $c, $template, {
+            content_type => $content_type,
+            stash_key => $self->stash_key,
+            %{$c->stash},
+        });
+
         # Got a ref, not a scalar.  An error!
         if ( ref $output ) {
             croak $output->can("as_string") ? $output->as_string : $output;
@@ -178,9 +185,9 @@ sub process {
                }
        }
 
-    delete $c->stash->{email}->{body};
-    $c->stash->{email}->{parts} ||= [];
-    push @{$c->stash->{email}->{parts}}, @parts;
+    delete $c->stash->{$stash_key}->{body};
+    $c->stash->{$stash_key}->{parts} ||= [];
+    push @{$c->stash->{$stash_key}->{parts}}, @parts;
 
     # Let C::V::Email do the actual sending.  We just assemble the tasty bits.
     return $self->next::method($c);
index 688f672..b8c6936 100644 (file)
@@ -1,11 +1,18 @@
 use strict;
 use warnings;
-use Test::More tests => 10;
+use Test::More;
 
 use Email::Send::Test;
 use FindBin;
 use lib "$FindBin::Bin/lib";
 
+eval "use Catalyst::View::TT";
+if ( $@ ) {
+    plan skip_all => 'Catalyst::View::TT required for Template tests';
+    exit;
+}
+plan tests => 10;
+
 use_ok('Catalyst::Test', 'TestApp');
 
 my $response;
index 421478a..0cb206e 100644 (file)
@@ -1,6 +1,6 @@
 use strict;
 use warnings;
-use Test::More tests => 5;
+use Test::More tests => 13;
 
 use Email::Send::Test;
 use FindBin;
@@ -9,11 +9,33 @@ use lib "$FindBin::Bin/lib";
 use_ok('Catalyst::Test', 'TestApp');
 
 my $response;
-my $time = time;
+my $time;
+my @emails;
+
+$time = time;
+
 ok( ($response = request("/email_app_config?time=$time"))->is_success, 'request ok');
 
-my @emails = Email::Send::Test->emails;
+@emails = Email::Send::Test->emails;
 
 is(@emails, 1, "got emails");
 isa_ok( $emails[0], 'Email::MIME', 'email is ok' );
 like($emails[0]->body, qr/$time/, 'Got our email');
+
+Email::Send::Test->clear;
+
+$time = time;
+ok( ($response = request("/template_email_app_config?time=$time"))->is_success, 'request ok');
+
+@emails = Email::Send::Test->emails;
+
+is(@emails, 1, "got emails");
+isa_ok( $emails[0], 'Email::MIME', 'email is ok' );
+my @parts = $emails[0]->parts;
+cmp_ok(@parts, '==', 2, 'got parts');
+
+is($parts[0]->content_type, 'text/plain; charset="us-ascii"', 'text/plain ok');
+like($parts[0]->body, qr/test-email\@example.com on $time/, 'got content back');
+is($parts[1]->content_type, 'text/html; charset="us-ascii"', 'text/html ok');
+like($parts[1]->body, qr{<em>test-email\@example.com</em> on $time}, 'got content back');
+
diff --git a/t/07mason.t b/t/07mason.t
new file mode 100644 (file)
index 0000000..afa9190
--- /dev/null
@@ -0,0 +1,37 @@
+use strict;
+use warnings;
+use Test::More;
+
+use Email::Send::Test;
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+eval "use Catalyst::View::Mason";
+if ( $@ ) {
+    plan skip_all => 'Catalyst::View::Mason required for Mason tests';
+    exit;
+}
+plan tests => 10;
+
+use_ok('Catalyst::Test', 'TestApp');
+
+TestApp->config->{default_view} = 'mason';
+
+my $response;
+my $time = time;
+ok( ( $response = request("/mason_email?time=$time"))->is_success,
+    'request ok' );
+like( $response->content, qr/Mason Email Ok/, 'controller says ok' );
+my @emails = Email::Send::Test->emails;
+
+cmp_ok(@emails, '==', 1, 'got emails');
+isa_ok( $emails[0], 'Email::MIME', 'email is ok' );
+my @parts = $emails[0]->parts;
+cmp_ok(@parts, '==', 2, 'got parts');
+
+is($parts[0]->content_type, 'text/plain; charset="us-ascii"', 'text/plain ok');
+like($parts[0]->body, qr/test-email\@example.com on $time/, 'got content back');
+is($parts[1]->content_type, 'text/html; charset="us-ascii"', 'text/html ok');
+like($parts[1]->body, qr{<em>test-email\@example.com</em> on $time}, 'got content back');
+#like($emails[0]->body, qr/$time/, 'Got our email');
+
index 736e8cd..3789de3 100644 (file)
@@ -12,6 +12,12 @@ TestApp->config(
             method => 'Test',
         },
     },
+    'View::Email::Template::AppConfig' => {
+        stash_key => 'template_email',
+        sender => {
+            method => 'Test',
+        },
+    },
 );
 
 TestApp->setup;
index 79fb38c..d96fc22 100644 (file)
@@ -79,4 +79,57 @@ sub template_email : Global('template_email') {
     }
 }
 
+sub template_email_app_config : Global('template_email_app_config') {
+    my ($self, $c, @args) = @_;
+
+    $c->stash->{time} = $c->req->params->{time} || time;
+
+    $c->stash->{template_email} = {
+        to      => 'test-email@example.com',
+        from    => 'no-reply@example.com',
+        subject => 'Just a test',
+        content_type => 'multipart/alternative',
+        templates => [
+            qw{text_plain/test.tt},
+            qw{text_html/test.tt}
+        ]
+    };
+
+    $c->forward('TestApp::View::Email::Template::AppConfig');
+
+    if ( scalar( @{ $c->error } ) ) {
+        $c->res->status(500);
+        $c->res->body('Template Email Failed');
+    } else {
+        $c->res->body('Template Email Ok');
+    }
+}
+
+sub mason_email : Global('mason_email') {
+    my ($self, $c, @args) = @_;
+
+    $c->stash->{time} = $c->req->params->{time} || time;
+
+    $c->stash->{email} = {
+        to      => 'test-email@example.com',
+        from    => 'no-reply@example.com',
+        subject => 'Just a test',
+        content_type => 'multipart/alternative',
+        templates => [
+            qw{text_plain/test.m},
+            qw{text_html/test.m}
+        ]
+    };
+
+    $c->forward('TestApp::View::Email::Template');    
+
+    if ( scalar( @{ $c->error } ) ) {
+        $c->res->status(500);
+        $c->res->body('Mason Email Failed');
+    } else {
+        $c->res->body('Mason Email Ok');
+    }
+}
+
+
 1;
diff --git a/t/lib/TestApp/View/Mason.pm b/t/lib/TestApp/View/Mason.pm
new file mode 100644 (file)
index 0000000..c25d132
--- /dev/null
@@ -0,0 +1,7 @@
+package # Hide me.
+    TestApp::View::Mason;
+
+use strict;
+eval "use base 'Catalyst::View::Mason';";
+
+1;
index c407091..d5ac61b 100644 (file)
@@ -2,6 +2,6 @@ package # Hide me.
     TestApp::View::TT;
 
 use strict;
-use base 'Catalyst::View::TT';
+eval "use base 'Catalyst::View::TT';";
 
 1;
index 256e0de..9467033 100644 (file)
@@ -1,7 +1,7 @@
 <html>
  <body>
   <h1>Look at my style</h1>
-  <p>I was sent to <em>[% email.to %]</em> on [% time %]</p>
+  <p>I was sent to <em>[% $stash_key.to %]</em> on [% time %]</p>
  </body>
 </html>
 
index df8a525..48c5039 100644 (file)
@@ -1,3 +1,3 @@
 I am plain text.  I have no style.
 
-I was sent to [% email.to %] on [% time %]
+I was sent to [% $stash_key.to %] on [% time %]