From 93d74a5d8902df2de58913bd012e15a0873b92e0 Mon Sep 17 00:00:00 2001 From: "Mark A. Stratman" Date: Tue, 22 Mar 2011 23:28:38 -0500 Subject: [PATCH] added tests --- t/lib/MyEntry.pm | 59 +++++++++++ t/lib/MyFeed.pm | 75 ++++++++++++++ t/lib/TestApp/Controller/Root.pm | 170 +++++++++++++++++++++++++++++-- t/live-test.t | 59 ++++++++++- 4 files changed, 355 insertions(+), 8 deletions(-) create mode 100644 t/lib/MyEntry.pm create mode 100644 t/lib/MyFeed.pm diff --git a/t/lib/MyEntry.pm b/t/lib/MyEntry.pm new file mode 100644 index 0000000..c08a20e --- /dev/null +++ b/t/lib/MyEntry.pm @@ -0,0 +1,59 @@ +package MyEntry; +use strict; +use warnings; + +# sub base # intentionally left out. + +sub new { + my $class = shift; + my $self = shift || {}; + return bless $self, $class; +} + +sub link { + my $self = shift; + if (@_) { $self->{link} = shift } + return $self->{link}; +} +sub title { + my $self = shift; + if (@_) { $self->{title} = shift } + return $self->{title}; +} +sub author { + my $self = shift; + if (@_) { $self->{author} = shift } + return $self->{author}; +} +sub id { + my $self = shift; + if (@_) { $self->{id} = shift } + return $self->{id}; +} +sub content { + my $self = shift; + if (@_) { $self->{content} = shift } + return $self->{content}; +} +sub issued { + my $self = shift; + if (@_) { $self->{issued} = shift } + return $self->{issued}; +} +sub modified { + my $self = shift; + if (@_) { $self->{modified} = shift } + return $self->{modified}; +} +sub category { + my $self = shift; + if (@_) { $self->{category} = shift } + return $self->{category}; +} +sub summary { + my $self = shift; + if (@_) { $self->{summary} = shift } + return $self->{summary}; +} + +1; diff --git a/t/lib/MyFeed.pm b/t/lib/MyFeed.pm new file mode 100644 index 0000000..703e72d --- /dev/null +++ b/t/lib/MyFeed.pm @@ -0,0 +1,75 @@ +package MyFeed; +use strict; +use warnings; + +sub new { + my $class = shift; + my $self = shift || {}; + return bless $self, $class; +} + +sub link { + my $self = shift; + if (@_) { $self->{link} = shift } + return $self->{link}; +} +sub base { + my $self = shift; + if (@_) { $self->{base} = shift } + return $self->{base}; +} +sub title { + my $self = shift; + if (@_) { $self->{title} = shift } + return $self->{title}; +} +sub tagline { + my $self = shift; + if (@_) { $self->{tagline} = shift } + return $self->{tagline}; +} +sub description { + my $self = shift; + if (@_) { $self->{description} = shift } + return $self->{description}; +} +sub author { + my $self = shift; + if (@_) { $self->{author} = shift } + return $self->{author}; +} +sub language { + my $self = shift; + if (@_) { $self->{language} = shift } + return $self->{language}; +} +sub copyright { + my $self = shift; + if (@_) { $self->{copyright} = shift } + return $self->{copyright}; +} +sub generator { + my $self = shift; + if (@_) { $self->{generator} = shift } + return $self->{generator}; +} +sub id { + my $self = shift; + if (@_) { $self->{id} = shift } + return $self->{id}; +} +sub modified { + my $self = shift; + if (@_) { $self->{modified} = shift } + return $self->{modified}; +} + +sub entries { + my $self = shift; + if (@_) { $self->{_entries} = shift } + return $self->{_return_entries_array} + ? @{ $self->{_entries} } + : $self->{_entries}; +} + +1; diff --git a/t/lib/TestApp/Controller/Root.pm b/t/lib/TestApp/Controller/Root.pm index 1279e5b..9dce528 100644 --- a/t/lib/TestApp/Controller/Root.pm +++ b/t/lib/TestApp/Controller/Root.pm @@ -2,6 +2,8 @@ package TestApp::Controller::Root; use Moose; use namespace::autoclean; use DateTime; +use MyFeed; +use MyEntry; BEGIN { extends 'Catalyst::Controller' } @@ -13,7 +15,7 @@ sub _feed_title :Private { return 'My awesome site'; } sub _feed_tagline :Private { return 'A great tagline'; } sub _feed_description :Private { return 'The greatest site ever'; } sub _feed_author :Private { return 'Mark A. Stratman'; } -sub _feed_language :Private { return 'English'; } +sub _feed_language :Private { return 'en-us'; } sub _feed_copyright :Private { return 'Copyright me 2011'; } sub _feed_generator :Private { return 'Catalyst::View::XML::Feed ' . $Catalyst::View::XML::Feed::VERSION; } sub _feed_id : Private { return _feed_link(@_); } @@ -102,30 +104,186 @@ sub _xml_feed : Private { return $feed; } -sub atom_xml_feed : Local { +sub xml_feed__atom : Local { my ($self, $c) = @_; $c->stash->{feed} = $self->_xml_feed($c, 'Atom'); $c->forward('View::Feed'); } -sub rss_xml_feed : Local { +sub xml_feed__rss : Local { my ($self, $c) = @_; $c->stash->{feed} = $self->_xml_feed($c, 'RSS'); $c->forward('View::Feed'); } -sub rss09_xml_feed : Local { +sub xml_feed__rss09 : Local { my ($self, $c) = @_; $c->stash->{feed} = $self->_xml_feed($c, 'RSS', version => '0.9'); $c->forward('View::Feed'); } -sub rss1_xml_feed : Local { +sub xml_feed__rss1 : Local { my ($self, $c) = @_; $c->stash->{feed} = $self->_xml_feed($c, 'RSS', version => '1.0'); $c->forward('View::Feed'); } -sub rss2_xml_feed : Local { +sub xml_feed__rss2 : Local { my ($self, $c) = @_; $c->stash->{feed} = $self->_xml_feed($c, 'RSS', version => '2.0'); $c->forward('View::Feed'); } +sub feed_obj_entries_arrayref_objs__rss : Local { + my ($self, $c) = @_; + my $feed = MyFeed->new($self->_my_feed_hash($c)); + my $entry = MyEntry->new($self->_my_entry_hash($c)); + $feed->entries([ $entry ]); + $c->stash->{feed} = $feed; + $c->forward('View::Feed'); +} + +sub feed_obj_array_entries_array_objs__rss : Local { + my ($self, $c) = @_; + my $feed = MyFeed->new($self->_my_feed_hash($c)); + my $entry = MyEntry->new($self->_my_entry_hash($c)); + $feed->{_return_entries_array} = 1; + $feed->entries([ $entry ]); + $c->stash->{feed} = $feed; + $c->forward('View::Feed'); +} + +sub feed_hash_entries_objs__rss : Local { + my ($self, $c) = @_; + my $feed = $self->_my_feed_hash($c); + my $entry = MyEntry->new($self->_my_entry_hash($c)); + $feed->{entries} = [ $entry ]; + $c->stash->{feed} = $feed; + $c->forward('View::Feed'); +} +sub feed_hash_entries_objs__atom : Local { + my ($self, $c) = @_; + my $feed = $self->_my_feed_hash($c); + my $entry = MyEntry->new($self->_my_entry_hash($c)); + $feed->{format} = 'Atom'; + $feed->{entries} = [ $entry ]; + $c->stash->{feed} = $feed; + $c->forward('View::Feed'); +} + +sub feed_hash_entries_hashes__rss : Local { + my ($self, $c) = @_; + my $feed = $self->_my_feed_hash($c); + $feed->{entries} = [ $self->_my_entry_hash($c) ]; + $c->stash->{feed} = $feed; + $c->forward('View::Feed'); +} + +sub xml_rss : Local { + my ($self, $c) = @_; + eval { require XML::RSS; }; + if ($@) { + return $c->res->body('XML::RSS not installed'); + } + my $rss = XML::RSS->new(version => '1.0'); + $rss->channel( + title => $self->_feed_title($c), + link => $self->_feed_link($c), + description => $self->_feed_description($c), + dc => { + date => '2000-08-23T07:00+00:00', + subject => "what is this", + creator => $self->_feed_author($c), + publisher => $self->_feed_author($c), + rights => $self->_feed_copyright($c), + language => $self->_feed_language($c), + }, + ); + $rss->add_item( + title => $self->_entry_title($c), + link => $self->_entry_link($c), + description => $self->_entry_content($c), + ); + $c->stash->{feed} = $rss; + $c->forward('View::Feed'); +} + +sub xml_atom_simplefeed : Local { + my ($self, $c) = @_; + eval { require XML::Atom::SimpleFeed; }; + if ($@) { + return $c->res->body('XML::Atom::SimpleFeed not installed'); + } + my $feed = XML::Atom::SimpleFeed->new( + title => $self->_feed_title($c), + link => $self->_feed_link($c), + updated => '2003-12-13T18:30:02Z', + author => $self->_feed_author($c), + id => $self->_feed_id($c), + ); + $feed->add_entry( + title => $self->_entry_title($c), + link => $self->_entry_link($c), + id => $self->_entry_id($c), + updated => '2003-12-13T18:30:02Z', + summary => $self->_entry_summary($c), + category => $self->_entry_category($c), + content => $self->_entry_content($c), + author => $self->_entry_author($c), + ); + $c->stash->{feed} = $feed; + $c->forward('View::Feed'); +} +sub xml_atom_feed : Local { + my ($self, $c) = @_; + eval { require XML::Atom::Feed; }; + if ($@) { + return $c->res->body('XML::Atom::Feed not installed'); + } + my $feed = XML::Atom::Feed->new(); + $feed->title($self->_feed_title($c)); + $feed->link($self->_feed_link($c)); + $feed->updated('2003-12-13T18:30:02Z'); + $feed->author($self->_feed_author($c)); + $feed->id($self->_feed_id($c)); + my $entry = XML::Atom::Entry->new; + $entry->title($self->_entry_title($c)); + $entry->link($self->_entry_link($c)); + $entry->id($self->_entry_id($c)); + $entry->updated('2003-12-13T18:30:02Z'); + $entry->summary($self->_entry_summary($c)); + $entry->content($self->_entry_content($c)); + $entry->author($self->_entry_author($c)); + $feed->add_entry($entry); + $c->stash->{feed} = $feed; + $c->forward('View::Feed'); +} + +sub _my_feed_hash : Private { + my ($self, $c) = @_; + return { + id => $self->_feed_id($c), + title => $self->_feed_title($c), + description => $self->_feed_description($c), + link => $self->_feed_link($c), + modified => $self->_now($c), + base => $self->_feed_base($c), + tagline=>$self->_feed_tagline($c), + author=>$self->_feed_author($c), + copyright=>$self->_feed_copyright($c), + generator=>$self->_feed_generator($c), + }; +} +sub _my_entry_hash : Private { + my ($self, $c) = @_; + # 'base' value intentionally left out. + return { + title => $self->_entry_title($c), + link=>$self->_entry_link($c), + content => $self->_entry_content($c), + summary=>$self->_entry_summary($c), + category=>$self->_entry_category($c), + author=>$self->_entry_author($c), + id=>$self->_entry_id($c), + issued=>$self->_now($c), + modified=>$self->_now($c), + }; +} + __PACKAGE__->meta->make_immutable; diff --git a/t/live-test.t b/t/live-test.t index c878289..866e333 100644 --- a/t/live-test.t +++ b/t/live-test.t @@ -3,6 +3,7 @@ use strict; use warnings; use Test::More; +use XML::Feed; # setup library path use FindBin qw($Bin); @@ -18,7 +19,58 @@ $mech->get_ok('http://localhost/', 'get main page'); $mech->content_like(qr/it works/i, 'main page has our text'); -for my $action (qw(string atom_xml_feed rss_xml_feed rss09_xml_feed rss1_xml_feed rss2_xml_feed)) { +subtest 'custom_formats' => sub { + for my $action (qw( + string + feed_obj_entries_arrayref_objs__rss + feed_obj_array_entries_array_objs__rss + feed_hash_entries_objs__rss feed_hash_entries_objs__atom + feed_hash_entries_hashes__rss + )) + { + test_action($mech, $action); + } +}; + +subtest 'xml_feed' => sub { + for my $action (qw( + xml_feed__atom xml_feed__rss xml_feed__rss09 + xml_feed__rss1 xml_feed__rss2 + )) + { + test_action($mech, $action); + } +}; + +subtest 'xml_rss' => sub { + eval { require XML::RSS; }; + plan(skip_all => 'XML::RSS not installed') if $@; + for my $action (qw(xml_rss)) + { + test_action($mech, $action); + } +}; + +subtest 'xml_atom_simplefeed' => sub { + eval { require XML::Atom::SimpleFeed; }; + plan(skip_all => 'XML::Atom::SimpleFeed not installed') if $@; + for my $action (qw(xml_atom_simplefeed)) + { + test_action($mech, $action); + } +}; + +subtest 'xml_atom_feed' => sub { + eval { require XML::Atom::Feed; }; + plan(skip_all => 'XML::Atom::Feed not installed') if $@; + for my $action (qw(xml_atom_feed)) + { + test_action($mech, $action); + } +}; + +sub test_action { + my ($mech, $action) = @_; $mech->get_ok("http://localhost/$action", "get /$action"); my $ct = 'text/xml'; if ($action =~ /rss/) { @@ -30,7 +82,10 @@ for my $action (qw(string atom_xml_feed rss_xml_feed rss09_xml_feed rss1_xml_fee $mech->content_like(qr/my awesome site/i, "/$action has 'my awesome site'"); $mech->content_like(qr/my first post/i, "/$action has 'my first post'"); $mech->content_like(qr/my_first_post/i, "/$action has 'my_first_post'"); - unless ($mech->content =~ m!http://my.netscape.com/rdf/simple/0.9/!i) { + + SKIP: { + skip "No author or description expected for RSS 0.9", 2 + if $mech->content =~ m!http://my.netscape.com/rdf/simple/0.9/!i; $mech->content_like(qr/it works/i, "/$action has 'it works'"); $mech->content_like(qr/Mark A\. Stratman/i, "/$action has 'Mark A. Stratman'"); }