# $Id: 4-mock-operation.t 317 2008-03-19 00:21:48Z davidp $ # mock tests for Net::Shoutcast::Admin # use Test::MockObject::Extends to fake communication with a Shoutcast server, # returning the contents of example.xml, so we can test that the module # handles it correctly and gives us the data we expect. use strict; use warnings; use Test::More; use lib '../lib'; eval "use Test::MockObject::Extends"; plan skip_all => "Test::MockObject::Extends required for mock testing" if $@; # OK, we have Test::MockObject::Extends, we're good to go plan tests => 12; use_ok( 'Net::Shoutcast::Admin' ); my $shoutcast = Net::Shoutcast::Admin->new( host => 'testhost', port => 1234, admin_password => 'testpass', ); # This wraps the ua so that methods can be overridden for testing purposes my $mocked_ua = $shoutcast->{ua} = Test::MockObject::Extends->new( $shoutcast->{ua} ); $mocked_ua->mock("get", \&mock_get); $mocked_ua->mock("is_success", sub { 1 }); my $example_xml = join '', ; sub mock_get { my ($self, $url) = @_; is($url, "http://testhost:1234/admin.cgi?pass=testpass&mode=viewxml", "URL for status XML is correct"); # prepare a fake response object to return: my $res = Test::MockObject->new(); $res->set_true( "is_success" ); $res->set_always('content', $example_xml); return $res; } # right, now try a few requests; my $song = $shoutcast->currentsong; isa_ok($song, 'Net::Shoutcast::Admin::Song', '->currentsong returned an N::S::A::Song object'); is($song->title, 'Fake Song Title', 'Current song title is correct'); my $listeners_count = $shoutcast->listeners; is($listeners_count, 2, '->listeners() returns 2 listeners in scalar context'); my @listeners = $shoutcast->listeners; is(@listeners, 2, '->listeners returned 2 listeners in scalar context'); isa_ok($listeners[0], 'Net::Shoutcast::Admin::Listener', 'first element of listeners list is a N::S::A::Listener object'); is($listeners[0]->host, '127.0.0.1', 'first listener has correct host'); is($listeners[0]->agent, 'testclient/1.2.3', 'first listener has correct agent'); is($listeners[0]->listen_time, 67, 'first listener has correct listen_time'); #diag("Total listeners: " . $shoutcast->listeners); #diag("Current song: " . $shoutcast->currentsong->title); __DATA__ 2 16 64 2 26255 mixed http://example.com/ Fake Radio Stream Fake Song Title http://www.example.net/ #example 163038 11574 1 128 audio/mpeg 1.9.8 1828 755 0 0 51 17 5 155 20059 117 0 0 0 1 0 0 1 139881 1 158 127.0.0.1 testclient/1.2.3 3 67 0 11575 127.0.0.2 badgerous/3.2.1 0 351 0 11577 1205868797 15 9PM (Till I Come) (Signum Mix) 1205868345 14 - I Get Lost - Eric Clapton 1205868066 01 - Headstrong - Trapt 1205867953 03 - Blur - Song 2 1205867567 20 - Lynyrd Skynyrd - Free Bird