forked from activemerchant/active_merchant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_helper.rb
261 lines (222 loc) · 6.81 KB
/
test_helper.rb
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/env ruby
$:.unshift File.expand_path('../../lib', __FILE__)
begin
require 'rubygems'
require 'bundler'
Bundler.setup
rescue LoadError => e
puts "Error loading bundler (#{e.message}): \"gem install bundler\" for bundler support."
end
require 'test/unit'
require 'money'
require 'mocha/version'
if(Mocha::VERSION.split(".")[1].to_i < 12)
require 'mocha'
else
require 'mocha/setup'
end
require 'yaml'
require 'json'
require 'active_merchant'
require 'comm_stub'
require 'active_support/core_ext/integer/time'
require 'active_support/core_ext/numeric/time'
begin
require 'active_support/core_ext/time/acts_like'
rescue LoadError
end
begin
gem 'actionpack'
rescue LoadError
raise StandardError, "The view tests need ActionPack installed as gem to run"
end
require 'action_controller'
require "action_view/template"
begin
require 'active_support/core_ext/module/deprecation'
require 'action_dispatch/testing/test_process'
rescue LoadError
require 'action_controller/test_process'
end
require 'active_merchant/billing/integrations/action_view_helper'
ActiveMerchant::Billing::Base.mode = :test
if ENV['DEBUG_ACTIVE_MERCHANT'] == 'true'
require 'logger'
ActiveMerchant::Billing::Gateway.logger = Logger.new(STDOUT)
ActiveMerchant::Billing::Gateway.wiredump_device = STDOUT
end
# Test gateways
class SimpleTestGateway < ActiveMerchant::Billing::Gateway
end
class SubclassGateway < SimpleTestGateway
end
module ActiveMerchant
module Assertions
AssertionClass = RUBY_VERSION > '1.9' ? MiniTest::Assertion : Test::Unit::AssertionFailedError
def assert_field(field, value)
clean_backtrace do
assert_equal value, @helper.fields[field]
end
end
# Allows the testing of you to check for negative assertions:
#
# # Instead of
# assert !something_that_is_false
#
# # Do this
# assert_false something_that_should_be_false
#
# An optional +msg+ parameter is available to help you debug.
def assert_false(boolean, message = nil)
message = build_message message, '<?> is not false or nil.', boolean
clean_backtrace do
assert_block message do
not boolean
end
end
end
# A handy little assertion to check for a successful response:
#
# # Instead of
# assert response.success?
#
# # DRY that up with
# assert_success response
#
# A message will automatically show the inspection of the response
# object if things go afoul.
def assert_success(response, message=nil)
clean_backtrace do
assert response.success?, build_message(nil, "#{message + "\n" if message}Response expected to succeed: <?>", response)
end
end
# The negative of +assert_success+
def assert_failure(response, message=nil)
clean_backtrace do
assert !response.success?, build_message(nil, "#{message + "\n" if message}Response expected to fail: <?>", response)
end
end
def assert_valid(validateable)
clean_backtrace do
assert validateable.valid?, "Expected to be valid"
end
end
def assert_not_valid(validateable)
clean_backtrace do
assert_false validateable.valid?, "Expected to not be valid"
end
end
def assert_deprecation_warning(message, target)
target.expects(:deprecated).with(message)
yield
end
def assert_no_deprecation_warning(target)
target.expects(:deprecated).never
yield
end
private
def clean_backtrace(&block)
yield
rescue AssertionClass => e
path = File.expand_path(__FILE__)
raise AssertionClass, e.message, e.backtrace.reject { |line| File.expand_path(line) =~ /#{path}/ }
end
end
module Fixtures
HOME_DIR = RUBY_PLATFORM =~ /mswin32/ ? ENV['HOMEPATH'] : ENV['HOME'] unless defined?(HOME_DIR)
LOCAL_CREDENTIALS = File.join(HOME_DIR.to_s, '.active_merchant/fixtures.yml') unless defined?(LOCAL_CREDENTIALS)
DEFAULT_CREDENTIALS = File.join(File.dirname(__FILE__), 'fixtures.yml') unless defined?(DEFAULT_CREDENTIALS)
private
def credit_card(number = '4242424242424242', options = {})
defaults = {
:number => number,
:month => 9,
:year => Time.now.year + 1,
:first_name => 'Longbob',
:last_name => 'Longsen',
:verification_value => '123',
:brand => 'visa'
}.update(options)
Billing::CreditCard.new(defaults)
end
def check(options = {})
defaults = {
:name => 'Jim Smith',
:bank_name => 'Bank of Elbonia',
:routing_number => '244183602',
:account_number => '15378535',
:account_holder_type => 'personal',
:account_type => 'checking',
:number => '1'
}.update(options)
Billing::Check.new(defaults)
end
def address(options = {})
{
:name => 'Jim Smith',
:address1 => '1234 My Street',
:address2 => 'Apt 1',
:company => 'Widgets Inc',
:city => 'Ottawa',
:state => 'ON',
:zip => 'K1C2N6',
:country => 'CA',
:phone => '(555)555-5555',
:fax => '(555)555-6666'
}.update(options)
end
def all_fixtures
@@fixtures ||= load_fixtures
end
def fixtures(key)
data = all_fixtures[key] || raise(StandardError, "No fixture data was found for '#{key}'")
data.dup
end
def load_fixtures
[DEFAULT_CREDENTIALS, LOCAL_CREDENTIALS].inject({}) do |credentials, file_name|
if File.exists?(file_name)
yaml_data = YAML.load(File.read(file_name))
credentials.merge!(symbolize_keys(yaml_data))
end
credentials
end
end
def symbolize_keys(hash)
return unless hash.is_a?(Hash)
hash.symbolize_keys!
hash.each{|k,v| symbolize_keys(v)}
end
end
end
Test::Unit::TestCase.class_eval do
include ActiveMerchant::Billing
include ActiveMerchant::Assertions
include ActiveMerchant::Utils
include ActiveMerchant::Fixtures
end
module ActionViewHelperTestHelper
def self.included(base)
base.send(:include, ActiveMerchant::Billing::Integrations::ActionViewHelper)
base.send(:include, ActionView::Helpers::FormHelper)
base.send(:include, ActionView::Helpers::FormTagHelper)
base.send(:include, ActionView::Helpers::UrlHelper)
base.send(:include, ActionView::Helpers::TagHelper)
base.send(:include, ActionView::Helpers::CaptureHelper)
base.send(:include, ActionView::Helpers::TextHelper)
base.send(:attr_accessor, :output_buffer)
end
def setup
@controller = Class.new do
attr_reader :url_for_options
def url_for(options, *parameters_for_method_reference)
@url_for_options = options
end
end
@controller = @controller.new
@output_buffer = ''
end
protected
def protect_against_forgery?
false
end
end