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   111465 use Moo;
  6         12048  
  6         44  
4              
5 6     6   3891 use namespace::autoclean;
  6         13949  
  6         56  
6              
7             our $VERSION = '0.000028';
8              
9 6     6   1312 use Types::Standard qw( Bool CodeRef InstanceOf );
  6         79058  
  6         79  
10 6     6   9052 use WebService::PayPal::PaymentsAdvanced::Mocker::PayflowLink ();
  6         27  
  6         211  
11 6     6   3682 use WebService::PayPal::PaymentsAdvanced::Mocker::PayflowPro ();
  6         27  
  6         3340  
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   409 my $self = shift;
59              
60 28 50       189 die 'plack => 1 is required for mocking via useragent'
61             unless $self->plack;
62              
63 28         2566 require Test::LWP::UserAgent;
64              
65 28         61694 my $ua = Test::LWP::UserAgent->new( network_fallback => 0 );
66 28         27353 return $ua;
67             }
68              
69             sub _build_mocked_ua {
70 28     28   39270 my $self = shift;
71              
72 28         2475 require HTTP::Message::PSGI;
73              
74 28         28140 my $link = $self->payflow_link;
75 28         68600 my $pro = $self->payflow_pro;
76              
77 28         43574 $self->_ua->register_psgi( 'payflowlink.paypal.com', $link );
78 28         4092 $self->_ua->register_psgi( 'payflowpro.paypal.com', $pro );
79 28         1987 $self->_ua->register_psgi( 'pilot-payflowlink.paypal.com', $link );
80 28         1930 $self->_ua->register_psgi( 'pilot-payflowpro.paypal.com', $pro );
81              
82 28         1841 return $self->_ua;
83             }
84              
85             sub _build_payflow_link {
86 29     29   1971 my $self = shift;
87              
88 29 100       470 local $ENV{PLACK_ENV} = 'development' if $self->plack;
89 29         347 return WebService::PayPal::PaymentsAdvanced::Mocker::PayflowLink->to_app;
90             }
91              
92             sub _build_payflow_pro {
93 31     31   18467 my $self = shift;
94              
95 31 100       334 local $ENV{PLACK_ENV} = 'development' if $self->plack;
96 31         364 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             =encoding UTF-8
108              
109             =head1 NAME
110              
111             WebService::PayPal::PaymentsAdvanced::Mocker - A class which returns mocked PPA apps.
112              
113             =head1 VERSION
114              
115             version 0.000028
116              
117             =head1 SYNOPSIS
118              
119             use WebService::PayPal::PaymentsAdvanced::Mocker;
120             my $mocker = WebService::PayPal::PaymentsAdvanced::Mocker->new( plack => 1 );
121             my $app = $mocker->payflow_pro; # returns a PSGI app
122              
123             # OR, to use with a mocking UserAgent
124             use Test::LWP::UserAgent;
125             use HTTP::Message::PSGI;
126              
127             my $ua = Test::LWP::UserAgent->new;
128             my $mocker = WebService::PayPal::PaymentsAdvanced::Mocker->new( plack => 1 );
129             $ua->register_psgi( 'pilot-payflowpro.paypal.com', $mocker->payflow_pro );
130             $ua->register_psgi( 'pilot-payflowlink.paypal.com', $mocker->payflow_link );
131              
132             my $ppa = WebService::PayPal::PaymentsAdvanced->new(
133             ua => $ua,
134             ...
135             );
136              
137             =head1 DESCRIPTION
138              
139             You can use this class to facilitate mocking your PPA integration. When
140             running under $ENV{HARNESS_ACTIVE}, you can pass a Test::LWP::UserAgent to
141             L<WebService::PayPal::PaymentsAdvanced> as in the SYNOPSIS above. Adjust the
142             hostnames as necessary.
143              
144             =head1 CONSTRUCTOR OPTIONS
145              
146             =head2 plack => [0|1]
147              
148             If you require a PSGI app to be returned, you'll need to enable this option.
149             Disabled by default.
150              
151             use WebService::PayPal::PaymentsAdvanced::Mocker;
152             my $mocker = WebService::PayPal::PaymentsAdvanced::Mocker->new( plack => 1 );
153             my $app = $mocker->payflow_pro; # returns a PSGI app
154              
155             =head2 ua
156              
157             If may provide your own UserAgent object to this class. This is only
158             necessary if you intend to call the C<mocked_ua> method and need to provide
159             your own customized UserAgent. The object must be L<Test::LWP::UserAgent>
160             object, or a subclass of it.
161              
162             =head2 payflow_link
163              
164             Returns a Mojolicious::Lite app which mocks the Payflow Link web service.
165              
166             =head2 payflow_pro
167              
168             Returns a Mojolicious::Lite app which mocks the Payflow Pro web service.
169              
170             =head2 mocked_ua
171              
172             Returns a UserAgent object mocking already enabled for both live and sandbox
173             PayPal hostnames. The UserAgent will either be the object which you passed via
174             the C<ua> option when you created the object or a vanilla
175             L<Test::LWP::UserAgent> object which this class will create.
176              
177             =head1 SUPPORT
178              
179             Bugs may be submitted through L<https://github.com/maxmind/webservice-paypal-paymentsadvanced/issues>.
180              
181             =head1 AUTHOR
182              
183             Olaf Alders <olaf@wundercounter.com>
184              
185             =head1 COPYRIGHT AND LICENSE
186              
187             This software is copyright (c) 2022 by MaxMind, Inc.
188              
189             This is free software; you can redistribute it and/or modify it under
190             the same terms as the Perl 5 programming language system itself.
191              
192             =cut