File Coverage

blib/lib/Dredd/Hooks/Methods.pm
Criterion Covered Total %
statement 12 31 38.7
branch n/a
condition n/a
subroutine 4 14 28.5
pod 7 10 70.0
total 23 55 41.8


line stmt bran cond sub pod time code
1             package Dredd::Hooks::Methods;
2              
3             =head1 NAME
4              
5             Dredd::Hooks::Methods - Sugar module for each of writing Dredd Hookfiles
6              
7             =head1 SUMMARY
8              
9             #!/usr/bin/env perl
10             # hookfiles.pl
11              
12             use strict;
13             use warnings;
14              
15             use Dredd::Hooks::Methods;
16              
17             before('/messages > GET' => sub {
18             my ($transaction) = @_;
19              
20             $transaction->{headers}{RawData}{Auth} = 'Basic: hud87y2h8o7ysdiuhlku12h=='
21             });
22              
23             =head1 DESCRIPTION
24              
25             Dredd::Hooks::Methods provides useful functions for writing
26             Dredd hook files.
27              
28             L is a testing framework
29             for testing API BluePrint formatted API definition files
30             against the implemenation that exposes that API. This is useful
31             to ensure that the API documentation doesn't get out of sync
32             with an new code changes.
33              
34             L provides and implementation of the
35             L
36             and ensures that hooks from user defined hook files are run in
37             the correct order and with the correct information.
38              
39             Dredd::Hooks::Methods provides functionallity that allow the
40             user to define the hooks files that get run and their
41             functionality.
42              
43             =head1 Creating a hook file
44              
45             Hookfiles are plain .pl perl files containing some callbacks
46             that are run for specific events.
47              
48             The hookfile should have a .pl file extention (required by the
49             dredd funtion and are provided as an argument to the dredd code:
50              
51             C
52              
53             for each event listed in the Dredd documentation a method is
54             provided that takes a sub ref that will be run for that event,
55             This event will receive a transaction (HashRef) or an arrayref of
56             transactions that it should modify in place. The return value of this
57             function will be ignored:
58              
59             beforeAll(sub {
60             my ($transactions) = @_;
61             });
62              
63             beforeEach(sub {
64             my ($transaction) = @_;
65             });
66              
67             See the each event below for the which events take which arguments.
68             If multiple callbacks are defined for the same event then these will
69             be run individually in the order defined. e.g:
70              
71             beforeAll(sub {
72             ... # Run First
73             });
74              
75             beforeAll(sub {
76             ... # Run Second
77             });
78              
79             You can also supply multiple files (or a glob) to the dredd command
80             and these will all be coallated together. Allowing spefic hooks for
81             specific transactions or event types.
82              
83             All events are run in a specific order:
84              
85             beforeAll - Run before all transactions
86             beforeEach - Run before each transaction
87             before - Run before specific transactions
88             beforeEachValidation - Run before each Validation step
89             beforeValidation - Run before a specific validation step
90             after - Run after a specific transaction
91             afterEach - Run after each transaction
92             afterAll - Run after all transactions
93              
94             =cut
95              
96 1     1   4 use strict;
  1         2  
  1         22  
97 1     1   11 use warnings;
  1         1  
  1         18  
98              
99 1     1   422 use Hash::Merge;
  1         1719  
  1         58  
100 1         9 use Sub::Exporter -setup => {
101             exports => [qw/
102             before
103             beforeAll
104             beforeEach
105             beforeEachValidation
106             beforeValidation
107             after
108             afterAll
109             afterEach
110             get_hooks
111             merge_hook
112             /],
113             groups => {
114             default => [qw/
115             before
116             beforeAll
117             beforeEach
118             beforeEachValidation
119             beforeValidation
120             after
121             afterAll
122             afterEach
123             /],
124             handler => [qw/
125             get_hooks
126             merge_hook
127             /]
128             }
129 1     1   486 };
  1         7008  
130              
131             {
132             my $hooks = {};
133             sub get_hooks {
134 0     0 0   return $hooks;
135             }
136              
137             my $merger = Hash::Merge->new('RETAINMENT_PRECEDENT');
138             sub merge_hook {
139 0     0 0   my ($hook) = @_;
140              
141 0           $hooks = $merger->merge($hooks, $hook);
142             }
143             }
144              
145             =head1 Events
146              
147             =head2 beforeAll (callback Sub)
148              
149             The beforeAll event is triggered before all transactions
150             are run. As such it is an ideal place to run test setup code.
151              
152             The hook will receive an array of transaction hashrefs
153             representing each transaction that will be implemented.
154              
155             =cut
156              
157             sub beforeAll {
158 0     0 1   my ($callback) = @_;
159              
160 0           merge_hook({beforeAll => $callback });
161             }
162              
163             =head2 beforeEach (callback Sub)
164              
165             The beforeEach event is called for before each transaction
166             is run.
167              
168             The hook will receive a single transaction hashref.
169              
170             NOTE: This event from Dredd triggers the before event
171              
172             =cut
173              
174             sub beforeEach {
175 0     0 1   my ($callback) = @_;
176              
177 0           merge_hook({beforeEach => $callback });
178             }
179              
180             =head2 before (name Str, callback Sub)
181              
182             The before event is called for each transaction whose
183             name attribute matches that given in the hook creation.
184              
185             The hook will receive a single transaction hashref.
186              
187             NOTE: before is not actually and event called by Dredd
188             directly but it is called after the beforeEach event.
189              
190             =cut
191              
192             sub before {
193 0     0 1   my ($name, $callback) = @_;
194              
195 0           merge_hook({before => { $name => $callback }});
196             }
197              
198             =head2 beforeEachValidation (callback Sub)
199              
200             The beforeEachValidation event is called for before each
201             transactions result is validated.
202              
203             The hook will receive a single transaction hashref.
204              
205             NOTE: This event is from Dredd triggers the beforeValidation event
206              
207             =cut
208              
209             sub beforeEachValidation {
210 0     0 1   my ($callback) = @_;
211              
212 0           merge_hook({beforeEachValidation => $callback });
213             }
214              
215             =head2 beforeValidation (name Str, callback Sub)
216              
217             The beforeEachValidation event is called for before the
218             validation of each transactions result, whose transaction
219             name matches the name supplied to this function, is validatated.
220              
221             The hook will receive a single transaction hashref.
222              
223             NOTE: beforeValidation is not actually and event called by Dredd
224             directly but it is called after the beforeEachValidation event.
225              
226             =cut
227              
228             sub beforeValidation {
229 0     0 1   my ($name, $callback) = @_;
230              
231 0           merge_hook({beforeValidation => { $name => $callback }});
232             }
233              
234             =head2 after (name Str, callback Sub)
235              
236             The after event is called after each transaction whose
237             name attribute matches that given in the hook creation.
238              
239             The hook will receive a single transaction hashref.
240              
241             NOTE: after is not actually and event called by Dredd
242             directly but it is called before the afterEach event.
243              
244             =cut
245              
246             sub after {
247 0     0 1   my ($name, $callback) = @_;
248              
249 0           merge_hook({after => { $name => $callback }});
250             }
251              
252             =head2 beforeEach (callback Sub)
253              
254             The afterEach event is called for after each transaction
255             is run.
256              
257             The hook will receive a single transaction hashref.
258              
259             NOTE: This event from Dredd triggers the after event
260              
261             =cut
262              
263             sub afterEach {
264 0     0 0   my ($callback) = @_;
265              
266 0           merge_hook({afterEach => $callback });
267             }
268              
269             =head2 afterAll (callback Sub)
270              
271             The afterAll event is triggers after all transactions are run.
272             As such it is an ideal place to run test teardown code.
273              
274             The hook will receive an array of transaction hashrefs
275             representing each transaction that will be implemented.
276              
277             =cut
278              
279             sub afterAll {
280 0     0 1   my ($callback) = @_;
281              
282 0           merge_hook({afterAll => $callback });
283             }
284             1;
285             __END__