1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
use warnings;
use strict;
use Net::Facebook::Oauth2;
use Test::More;
#########################
#########################
# Fixture Data
my $app_id = 'testapp_id';
my $app_secret = 'test_app_secret';
my $access_token = 'test_access_token';
my $url = 'test.www.com';
my $class = 'Net::Facebook::Oauth2';
eval "use Test::Requires qw/Test::Exception Test::MockObject Test::MockModule/";
if ($@){
plan skip_all => 'Test::Requires required for testing';
} else {
#had to re use??!
use Test::Exception;
use Test::MockObject;
use Test::MockModule;
can_instantiate_class();
test_get_method_with_no_browser_parameter();
can_pass_browser_param();
can_do_delete_request();
done_testing();
}
sub can_instantiate_class {
my $net_fb_oauth2 = $class->new(
application_id => $app_id,
application_secret => $app_secret,
);
ok $net_fb_oauth2,
"Can instantiate $class with application_id and application_secret";
dies_ok { $class->new( application_id => $app_id ) }
'Dies if no application_secret passed to constructor';
dies_ok { $class->new( application_secret => $app_secret ) }
'Dies if no application_id passed to constructor';
}
sub test_get_method_with_no_browser_parameter {
# Test that browser attribute is LWP::UserAgent if no browser param passed
my $test_json = '{"data":"this is the get data"}';
my $mock_get_response = _mock_object(
{
is_success => 1,
content => $test_json,
}
);
# Mock LWP::UserAgent methods so can test offline
my $mock_user_agent = _mock_object(
{
get => $mock_get_response,
}
);
my $mock_user_agent_module = new Test::MockModule('LWP::UserAgent');
$mock_user_agent_module->mock( 'new', sub {return $mock_user_agent;} );
my $net_fb_oauth2 = $class->new(
application_id => $app_id,
application_secret => $app_secret,
access_token => $access_token,
);
is $net_fb_oauth2->get( $url )->as_json, $test_json,
'Passing no browser param will use LWP::UserAgent';
}
sub can_pass_browser_param {
my $test_json = '{"data":"this is the get data"}';
my $mock_get_response = _mock_object(
{
is_success => 1,
content => $test_json,
}
);
my $mock_browser = _mock_object( {
get => $mock_get_response,
}
);
my $net_fb_oauth2 = $class->new(
application_id => $app_id,
application_secret => $app_secret,
access_token => $access_token,
browser => $mock_browser,
);
is $net_fb_oauth2->get( $url )->as_json, $test_json,
'Can pass browser param';
}
sub can_do_delete_request {
my $test_json = '{"data":"this is the delete data"}';
my $mock_delete_response = _mock_object(
{
is_success => 1,
content => $test_json,
}
);
# Mock LWP::UserAgent methods so can test offline
my $mock_user_agent = _mock_object(
{
delete => $mock_delete_response,
}
);
my $mock_user_agent_module = new Test::MockModule('LWP::UserAgent');
$mock_user_agent_module->mock( 'new', sub {return $mock_user_agent;} );
my $net_fb_oauth2 = $class->new(
application_id => $app_id,
application_secret => $app_secret,
access_token => $access_token,
);
is $net_fb_oauth2->delete( $url )->as_json, $test_json,
'Delete request returns correct JSON';
}
sub _mock_object {
my $mock_kv = shift;
my $mock_object = Test::MockObject->new;
while ( my($key, $value) = each %$mock_kv) {
$mock_object->set_always($key, $value);
}
return $mock_object;
}
|