File Coverage

blib/lib/WebService/PayPal/PaymentsAdvanced/Mocker.pm
Criterion Covered Total %
statement 35 35 100.0
branch 5 6 83.3
condition n/a
subroutine 9 9 100.0
pod n/a
total 49 50 98.0


line stmt bran cond sub pod time code
1             package WebService::PayPal::PaymentsAdvanced::Mocker;
2              
3 6     6   176912 use Moo;
  6         7635  
  6         39  
4              
5 6     6   3617 use namespace::autoclean;
  6         1611  
  6         103  
6              
7             our $VERSION = '0.000026';
8              
9 6     6   1168 use Types::Standard qw( Bool CodeRef InstanceOf );
  6         75191  
  6         76  
10 6     6   7931 use WebService::PayPal::PaymentsAdvanced::Mocker::PayflowLink;
  6         25  
  6         73  
11 6     6   3514 use WebService::PayPal::PaymentsAdvanced::Mocker::PayflowPro;
  6         26  
  6         47  
12              
13             has mocked_ua => (
14             is => 'ro',
15             isa => InstanceOf ['Test::LWP::UserAgent'],
16             init_arg => undef,
17             lazy => 1,
18             builder => '_build_mocked_ua',
19             );
20              
21             # The app builders return different things under different conditions. The
22             # return a CodeRef under Plack. They return a Mojolicious::Lite object when
23             # deployed via morbo. They return a "1" when not run via Plack, using the test
24             # suite.
25              
26             has payflow_link => (
27             is => 'ro',
28             isa => CodeRef | InstanceOf ['Mojolicious::Lite'] | Bool,
29             init_arg => undef,
30             lazy => 1,
31             builder => '_build_payflow_link',
32             );
33              
34             has payflow_pro => (
35             is => 'ro',
36             isa => CodeRef | InstanceOf ['Mojolicious::Lite'] | Bool,
37             init_arg => undef,
38             lazy => 1,
39             builder => '_build_payflow_pro',
40             );
41              
42             has plack => (
43             is => 'ro',
44             isa => Bool,
45             init_arg => 'plack',
46             default => 0,
47             );
48              
49             has _ua => (
50             is => 'ro',
51             isa => InstanceOf ['Test::LWP::UserAgent'],
52             init_arg => 'ua',
53             lazy => 1,
54             builder => '_build_ua',
55             );
56              
57             sub _build_ua {
58 28     28   337 my $self = shift;
59              
60 28 50       144 die 'plack => 1 is required for mocking via useragent'
61             unless $self->plack;
62              
63 28         1833 require Test::LWP::UserAgent;
64              
65 28         21437 my $ua = Test::LWP::UserAgent->new( network_fallback => 0 );
66 28         24120 return $ua;
67             }
68              
69             sub _build_mocked_ua {
70 28     28   30086 my $self = shift;
71              
72 28         2197 require HTTP::Message::PSGI;
73              
74 28         21351 my $link = $self->payflow_link;
75 28         57375 my $pro = $self->payflow_pro;
76              
77 28         41501 $self->_ua->register_psgi( 'payflowlink.paypal.com', $link );
78 28         2843 $self->_ua->register_psgi( 'payflowpro.paypal.com', $pro );
79 28         1602 $self->_ua->register_psgi( 'pilot-payflowlink.paypal.com', $link );
80 28         1529 $self->_ua->register_psgi( 'pilot-payflowpro.paypal.com', $pro );
81              
82 28         1568 return $self->_ua;
83             }
84              
85             sub _build_payflow_link {
86 29     29   1872 my $self = shift;
87              
88 29 100       308 local $ENV{PLACK_ENV} = 'development' if $self->plack;
89 29         242 return WebService::PayPal::PaymentsAdvanced::Mocker::PayflowLink->to_app;
90             }
91              
92             sub _build_payflow_pro {
93 31     31   17897 my $self = shift;
94              
95 31 100       282 local $ENV{PLACK_ENV} = 'development' if $self->plack;
96 31         286 return WebService::PayPal::PaymentsAdvanced::Mocker::PayflowPro->to_app;
97             }
98              
99             1;
100              
101             # ABSTRACT: A class which returns mocked PPA apps.
102              
103             __END__
104              
105             =pod
106              
107             =head1 NAME
108              
109             WebService::PayPal::PaymentsAdvanced::Mocker - A class which returns mocked PPA apps.
110              
111             =head1 VERSION
112              
113             version 0.000026
114              
115             =head1 SYNOPSIS
116              
117             use WebService::PayPal::PaymentsAdvanced::Mocker;
118             my $mocker = WebService::PayPal::PaymentsAdvanced::Mocker->new( plack => 1 );
119             my $app = $mocker->payflow_pro; # returns a PSGI app
120              
121             # OR, to use with a mocking UserAgent
122             use Test::LWP::UserAgent;
123             use HTTP::Message::PSGI;
124              
125             my $ua = Test::LWP::UserAgent->new;
126             my $mocker = WebService::PayPal::PaymentsAdvanced::Mocker->new( plack => 1 );
127             $ua->register_psgi( 'pilot-payflowpro.paypal.com', $mocker->payflow_pro );
128             $ua->register_psgi( 'pilot-payflowlink.paypal.com', $mocker->payflow_link );
129              
130             my $ppa = WebService::PayPal::PaymentsAdvanced->new(
131             ua => $ua,
132             ...
133             );
134              
135             =head1 DESCRIPTION
136              
137             You can use this class to facilitate mocking your PPA integration. When
138             running under $ENV{HARNESS_ACTIVE}, you can pass a Test::LWP::UserAgent to
139             L<WebService::PayPal::PaymentsAdvanced> as in the SYNOPSIS above. Adjust the
140             hostnames as necessary.
141              
142             =head1 CONSTRUCTOR OPTIONS
143              
144             =head2 plack => [0|1]
145              
146             If you require a PSGI app to be returned, you'll need to enable this option.
147             Disabled by default.
148              
149             use WebService::PayPal::PaymentsAdvanced::Mocker;
150             my $mocker = WebService::PayPal::PaymentsAdvanced::Mocker->new( plack => 1 );
151             my $app = $mocker->payflow_pro; # returns a PSGI app
152              
153             =head2 ua
154              
155             If may provide your own UserAgent object to this class. This is only
156             necessary if you intend to call the C<mocked_ua> method and need to provide
157             your own customized UserAgent. The object must be L<Test::LWP::UserAgent>
158             object, or a subclass of it.
159              
160             =head2 payflow_link
161              
162             Returns a Mojolicious::Lite app which mocks the Payflow Link web service.
163              
164             =head2 payflow_pro
165              
166             Returns a Mojolicious::Lite app which mocks the Payflow Pro web service.
167              
168             =head2 mocked_ua
169              
170             Returns a UserAgent object mocking already enabled for both live and sandbox
171             PayPal hostnames. The UserAgent will either be the object which you passed via
172             the C<ua> option when you created the object or a vanilla
173             L<Test::LWP::UserAgent> object which this class will create.
174              
175             =head1 AUTHOR
176              
177             Olaf Alders <olaf@wundercounter.com>
178              
179             =head1 COPYRIGHT AND LICENSE
180              
181             This software is copyright (c) 2020 by MaxMind, Inc.
182              
183             This is free software; you can redistribute it and/or modify it under
184             the same terms as the Perl 5 programming language system itself.
185              
186             =cut