Fix introspection with a complicated supported_actions
[Business-OnlinePayment.git] / t / introspection.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Test::More tests => 13;
6
7 {    # fake test driver 1 (no _info hash)
8
9     package Business::OnlinePayment::MOCK1;
10     use strict;
11     use warnings;
12     use base qw(Business::OnlinePayment);
13 }
14
15 {    # fake test driver 2 (with _info hash)
16
17     package Business::OnlinePayment::MOCK2;
18     use base qw(Business::OnlinePayment::MOCK1);
19     sub _info {
20       {
21         'info_compat'           => '0.01', # always 0.01 for now,
22                                            # 0.02 will have requirements
23         'gateway_name'          => 'Example Gateway',
24         'gateway_url'           => 'http://www.example.com/',
25         'module_version'        => '0.01', #$VERSION,
26         'supported_types'       => [ qw( CC ECHECK ) ],
27         'token_support'         => 0, #card storage/tokenization support
28         'test_transaction'      => 0, #set true if ->test_transaction(1) works
29         'supported_actions'     => [
30                                      'Normal Authorization',
31                                      'Authorization Only',
32                                      'Post Authorization',
33                                      'Void',
34                                      'Credit',
35                                    ],
36         'CC_void_requires_card' => 1,
37       };
38     }
39 }
40
41 {    # fake test driver 3 (with _info hash)
42
43     package Business::OnlinePayment::MOCK3;
44     use base qw(Business::OnlinePayment::MOCK1);
45     sub _info {
46       {
47         'info_compat'           => '0.01', # always 0.01 for now,
48                                            # 0.02 will have requirements
49         'gateway_name'          => 'Example Gateway',
50         'gateway_url'           => 'http://www.example.com/',
51         'module_version'        => '0.01', #$VERSION,
52         'supported_types'       => [ qw( CC ECHECK ) ],
53         'token_support'         => 1,
54         'test_transaction'      => 1,
55         'supported_actions'     => { 'CC' => [
56                                        'Normal Authorization',
57                                        'Authorization Only',
58                                        'Post Authorization',
59                                        'Void',
60                                        'Credit',
61                                        'Recurring Authorization',
62                                        'Modify Recurring Authorization',
63                                        'Cancel Recurring Authorization',
64                                      ],
65                                      'ECHECK' => [
66                                        'Normal Authorization',
67                                        'Void',
68                                        'Credit',
69                                      ],
70                                    },
71       };
72     }
73 }
74
75 my $package = "Business::OnlinePayment";
76 my @drivers = qw(MOCK1 MOCK2 MOCK3);
77 my $driver  = $drivers[0];
78
79 # trick to make use() happy (called in Business::OnlinePayment->new)
80 foreach my $drv (@drivers) {
81     $INC{"Business/OnlinePayment/${drv}.pm"} = "testing";
82 }
83
84
85 my $obj = $package->new($driver);
86 isa_ok( $obj, $package );
87 isa_ok( $obj, $package . "::" . $driver );
88
89 my %throwaway_actions = eval { $obj->info('supported_actions') };
90 ok( !$@, "->info('supported_actions') works w/o gateway module introspection");
91
92
93 $driver = 'MOCK2';
94 $obj = $package->new($driver);
95 isa_ok( $obj, $package );
96 isa_ok( $obj, $package . "::" . $driver );
97
98 my %actions = eval { $obj->info('supported_actions') };
99 ok( grep { $_ eq 'Void' } @{ $actions{$_} },
100     "->info('supported_actions') works w/gateway module introspection ($_)"
101   ) foreach qw( CC ECHECK );
102
103 ok($obj->info('CC_void_requires_card'),
104    'CC_void_requires_card introspection');
105 ok(!$obj->info('ECHECK_void_requires_account'),
106    'ECHECK_void_requires_account introspection');
107
108
109 $driver = 'MOCK3';
110 $obj = $package->new($driver);
111 isa_ok( $obj, $package );
112 isa_ok( $obj, $package . "::" . $driver );
113
114 %actions = eval { $obj->info('supported_actions') };
115 ok( grep { $_ eq 'Authorization Only' } @{ $actions{CC} },
116     "->info('supported_actions') w/hashref supported_actions");
117 ok( ! grep { $_ eq 'Authorization Only' } @{ $actions{ECHECK} },
118     "->info('supported_actions') w/hashref supported_actions");
119