| 1 | package XML::FeedWriter::Test::RSS20::Channel; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | use Test::Classy::Base; |
|---|
| 6 | use XML::FeedWriter; |
|---|
| 7 | use DateTime; |
|---|
| 8 | use Encode; |
|---|
| 9 | |
|---|
| 10 | __PACKAGE__->mk_classdata( 'xs' ); |
|---|
| 11 | |
|---|
| 12 | sub _channel_fixture {( |
|---|
| 13 | version => '2.0', |
|---|
| 14 | title => 'title', |
|---|
| 15 | link => 'http://example.com/', |
|---|
| 16 | description => 'description', |
|---|
| 17 | )} |
|---|
| 18 | |
|---|
| 19 | sub _test { |
|---|
| 20 | my ($class, $elem, $arg, $expected) = @_; |
|---|
| 21 | |
|---|
| 22 | my $writer; |
|---|
| 23 | eval { $writer = XML::FeedWriter->new( $class->_channel_fixture, |
|---|
| 24 | $elem => $arg, |
|---|
| 25 | )}; |
|---|
| 26 | return $@ if $@; |
|---|
| 27 | $writer->close; |
|---|
| 28 | |
|---|
| 29 | my $got = $class->xs->parse_string( $writer->as_string ); |
|---|
| 30 | my $exp = $class->xs->parse_string('<test>'.$expected.'</test>'); |
|---|
| 31 | |
|---|
| 32 | is_deeply |
|---|
| 33 | $got->{rss}{channel}{$elem} => $exp->{test}{$elem}, $class->test_name; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | sub initialize { |
|---|
| 37 | my $class = shift; |
|---|
| 38 | |
|---|
| 39 | eval { require XML::Simple }; |
|---|
| 40 | return $class->skip_this_class('requires XML::Simple 2.17') |
|---|
| 41 | if $@ or $XML::Simple::VERSION lt "2.17"; |
|---|
| 42 | |
|---|
| 43 | $class->xs( XML::Simple->new( ForceArray => 0, KeepRoot => 1 ) ); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | sub basic : Test(5) { |
|---|
| 47 | my $class = shift; |
|---|
| 48 | |
|---|
| 49 | my $writer = XML::FeedWriter->new( $class->_channel_fixture() ); |
|---|
| 50 | |
|---|
| 51 | isa_ok $writer => 'XML::FeedWriter::RSS20'; |
|---|
| 52 | |
|---|
| 53 | $writer->close; |
|---|
| 54 | |
|---|
| 55 | my $string = $writer->as_string; |
|---|
| 56 | |
|---|
| 57 | # should not be a scalar (or blessed) reference |
|---|
| 58 | ok $string && !ref $string, 'has some output'; |
|---|
| 59 | |
|---|
| 60 | my $file = 't/test.xml'; |
|---|
| 61 | unlink $file if -f $file; |
|---|
| 62 | |
|---|
| 63 | ok !-f $file, "make sure there's no file of the name"; |
|---|
| 64 | $writer->save($file); |
|---|
| 65 | ok -f $file, 'now we have the file'; |
|---|
| 66 | |
|---|
| 67 | local $/; |
|---|
| 68 | open my $fh, '<', $file; |
|---|
| 69 | my $saved = <$fh>; |
|---|
| 70 | close $fh; |
|---|
| 71 | |
|---|
| 72 | is decode_utf8( $saved ) => $string, 'and content looks fine'; |
|---|
| 73 | |
|---|
| 74 | unlink $file; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | sub channel_error : Test(3) { |
|---|
| 78 | my $class = shift; |
|---|
| 79 | |
|---|
| 80 | foreach my $elem (qw( link title description )) { |
|---|
| 81 | my $error = $class->_test( $elem => undef ); |
|---|
| 82 | ok $error =~ /is required/, $class->test_name . ": $elem"; |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | sub channel_pubdate_plain_epoch : Test { |
|---|
| 87 | my $class = shift; |
|---|
| 88 | |
|---|
| 89 | $class->_test( pubDate => 1215423575, <<'EXPECTED'); |
|---|
| 90 | <pubDate>Mon, 07 Jul 2008 09:39:35 -0000</pubDate> |
|---|
| 91 | EXPECTED |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | sub channel_pubdate_epoch : Test { |
|---|
| 95 | my $class = shift; |
|---|
| 96 | |
|---|
| 97 | $class->_test( pubDate => { epoch => 1215423575 }, <<'EXPECTED'); |
|---|
| 98 | <pubDate>Mon, 07 Jul 2008 09:39:35 -0000</pubDate> |
|---|
| 99 | EXPECTED |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | sub channel_pubdate : Test { |
|---|
| 103 | my $class = shift; |
|---|
| 104 | |
|---|
| 105 | my $arg = { |
|---|
| 106 | year => 2008, month => 7, day => 7, |
|---|
| 107 | hour => 9, minute => 39, second => 35, |
|---|
| 108 | }; |
|---|
| 109 | $class->_test( pubDate => $arg, <<'EXPECTED'); |
|---|
| 110 | <pubDate>Mon, 07 Jul 2008 09:39:35 -0000</pubDate> |
|---|
| 111 | EXPECTED |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | sub channel_pubdate_object : Test { |
|---|
| 115 | my $class = shift; |
|---|
| 116 | |
|---|
| 117 | my $dt = DateTime->from_epoch( epoch => 1215423575 ); |
|---|
| 118 | $class->_test( pubDate => $dt, <<'EXPECTED'); |
|---|
| 119 | <pubDate>Mon, 07 Jul 2008 09:39:35 -0000</pubDate> |
|---|
| 120 | EXPECTED |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | sub channel_category_single : Test { |
|---|
| 124 | my $class = shift; |
|---|
| 125 | |
|---|
| 126 | $class->_test( category => 'category', <<'EXPECTED'); |
|---|
| 127 | <category>category</category> |
|---|
| 128 | EXPECTED |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | sub channel_category_multiple : Test { |
|---|
| 132 | my $class = shift; |
|---|
| 133 | |
|---|
| 134 | my $arg = [qw( category1 category2 )]; |
|---|
| 135 | $class->_test( category => $arg, <<'EXPECTED'); |
|---|
| 136 | <category>category1</category> |
|---|
| 137 | <category>category2</category> |
|---|
| 138 | EXPECTED |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | sub channel_category_multiple_with_domain : Test { |
|---|
| 142 | my $class = shift; |
|---|
| 143 | |
|---|
| 144 | my $arg = [ ['category1', domain => 'domain'], 'category2' ]; |
|---|
| 145 | $class->_test( category => $arg, <<'EXPECTED'); |
|---|
| 146 | <category domain="domain">category1</category> |
|---|
| 147 | <category>category2</category> |
|---|
| 148 | EXPECTED |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | sub channel_cloud : Test { |
|---|
| 152 | my $class = shift; |
|---|
| 153 | |
|---|
| 154 | my $arg = { |
|---|
| 155 | domain => 'example.com', |
|---|
| 156 | path => '/rpc', |
|---|
| 157 | port => '80', |
|---|
| 158 | protocol => 'xml-rpc', |
|---|
| 159 | registerProcedure => 'cloud.notify', |
|---|
| 160 | }; |
|---|
| 161 | |
|---|
| 162 | $class->_test( cloud => $arg, <<'EXPECTED'); |
|---|
| 163 | <cloud domain="example.com" path="/rpc" port="80" protocol="xml-rpc" registerProcedure="cloud.notify" /> |
|---|
| 164 | EXPECTED |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | sub channel_image : Test { |
|---|
| 168 | my $class = shift; |
|---|
| 169 | |
|---|
| 170 | my $arg = { |
|---|
| 171 | link => 'http://example.com/', |
|---|
| 172 | title => 'title', |
|---|
| 173 | url => 'http://example.com/image.gif', |
|---|
| 174 | description => 'image description', |
|---|
| 175 | height => 32, |
|---|
| 176 | width => 96, |
|---|
| 177 | }; |
|---|
| 178 | |
|---|
| 179 | $class->_test( image => $arg, <<'EXPECTED'); |
|---|
| 180 | <image> |
|---|
| 181 | <link>http://example.com/</link> |
|---|
| 182 | <title>title</title> |
|---|
| 183 | <url>http://example.com/image.gif</url> |
|---|
| 184 | <description>image description</description> |
|---|
| 185 | <height>32</height> |
|---|
| 186 | <width>96</width> |
|---|
| 187 | </image> |
|---|
| 188 | EXPECTED |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | sub channel_image_error : Test(3) { |
|---|
| 192 | my $class = shift; |
|---|
| 193 | |
|---|
| 194 | my $arg = { |
|---|
| 195 | link => 'http://example.com/', |
|---|
| 196 | title => 'title', |
|---|
| 197 | url => 'http://example.com/image.gif', |
|---|
| 198 | }; |
|---|
| 199 | |
|---|
| 200 | foreach my $elem (qw( link title url )) { |
|---|
| 201 | my %hash = %{ $arg }; |
|---|
| 202 | delete $hash{$elem}; |
|---|
| 203 | my $error = $class->_test( image => \%hash ); |
|---|
| 204 | ok $error =~ /is required/, $class->test_name . ": $elem"; |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | sub channel_skipdays : Test { |
|---|
| 209 | my $class = shift; |
|---|
| 210 | |
|---|
| 211 | my $arg = [qw( Saturday Sunday )]; |
|---|
| 212 | |
|---|
| 213 | $class->_test( skipDays => $arg, <<'EXPECTED'); |
|---|
| 214 | <skipDays> |
|---|
| 215 | <day>Saturday</day> |
|---|
| 216 | <day>Sunday</day> |
|---|
| 217 | </skipDays> |
|---|
| 218 | EXPECTED |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | sub channel_skiphours : Test { |
|---|
| 222 | my $class = shift; |
|---|
| 223 | |
|---|
| 224 | my $arg = [qw( 1 2 3 )]; |
|---|
| 225 | |
|---|
| 226 | $class->_test( skipHours => $arg, <<'EXPECTED'); |
|---|
| 227 | <skipHours> |
|---|
| 228 | <hour>1</hour> |
|---|
| 229 | <hour>2</hour> |
|---|
| 230 | <hour>3</hour> |
|---|
| 231 | </skipHours> |
|---|
| 232 | EXPECTED |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | sub channel_textinput : Test { |
|---|
| 236 | my $class = shift; |
|---|
| 237 | |
|---|
| 238 | my $arg = { |
|---|
| 239 | link => 'http://example.com/textinput', |
|---|
| 240 | title => 'title', |
|---|
| 241 | name => 'query', |
|---|
| 242 | description => 'image description', |
|---|
| 243 | }; |
|---|
| 244 | |
|---|
| 245 | $class->_test( textinput => $arg, <<'EXPECTED'); |
|---|
| 246 | <textinput> |
|---|
| 247 | <link>http://example.com/textinput</link> |
|---|
| 248 | <title>title</title> |
|---|
| 249 | <name>query</name> |
|---|
| 250 | <description>image description</description> |
|---|
| 251 | </textinput> |
|---|
| 252 | EXPECTED |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | sub channel_textinput_error : Test(4) { |
|---|
| 256 | my $class = shift; |
|---|
| 257 | |
|---|
| 258 | my $arg = { |
|---|
| 259 | link => 'http://example.com/textinput', |
|---|
| 260 | title => 'title', |
|---|
| 261 | name => 'query', |
|---|
| 262 | description => 'image description', |
|---|
| 263 | }; |
|---|
| 264 | |
|---|
| 265 | foreach my $elem (qw( link title name description )) { |
|---|
| 266 | my %hash = %{ $arg }; |
|---|
| 267 | delete $hash{$elem}; |
|---|
| 268 | my $error = $class->_test( textinput => \%hash ); |
|---|
| 269 | ok $error =~ /is required/, $class->test_name . ": $elem"; |
|---|
| 270 | } |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | 1; |
|---|