File Coverage

blib/lib/Dredd/Hooks.pm
Criterion Covered Total %
statement 9 40 22.5
branch 0 10 0.0
condition 0 5 0.0
subroutine 3 13 23.0
pod 6 8 75.0
total 18 76 23.6


line stmt bran cond sub pod time code
1             package Dredd::Hooks;
2              
3             =encoding utf-8
4              
5             =head1 NAME
6              
7             Dredd::Hooks - Handler for running Hook files for Dredd
8              
9             =head1 SYNOPSIS
10              
11             use Dredd::Hooks;
12              
13             my $hook_runner = Dredd::Hooks->new(hook_files => [...]);
14              
15             my $hook_runner->$event."_hook"
16              
17             =head1 DESCRIPTION
18              
19             Dredd::Hooks provides the code to actually run the hooks used in the
20             L API testing suite.
21              
22             Unless you are righting your own TCP server to accept Dredd Hook requests
23             then you likely want L which describes how to write
24             Dredd Hook files.
25              
26             =cut
27              
28 1     1   876 use Moo;
  1         10074  
  1         4  
29              
30 1     1   1489 use Dredd::Hooks::Methods '-handler';
  1         3  
  1         5  
31              
32             our $VERSION = "0.07";
33              
34 1     1   874 use Types::Standard qw/HashRef ArrayRef/;
  1         87448  
  1         16  
35              
36             # Stores the hooks that result from Dredd::Hooks::Methods
37             has _hooks => (
38             is => 'lazy',
39             isa => HashRef
40             );
41              
42             =head1 ATTRIBUTES
43              
44             =head2 hook_files
45              
46             An arrayref of fully expanded file names that will be required
47             and should contain hook code.
48              
49             See L for information on creating these
50             files
51              
52             =cut
53              
54             has hook_files => (
55             is => 'ro',
56             isa => ArrayRef
57             );
58              
59             # Run each hook file in $self->hook_files and then
60             # return the value stored in Dredd::Hooks::Methods
61              
62             sub _build__hooks {
63 0     0     my ($self) = @_;
64              
65 0           my $hook_files = $self->hook_files;
66 0 0 0       return {} unless $hook_files && scalar @$hook_files;
67              
68 0           for my $hook_file (@$hook_files){
69 0 0         next unless -e $hook_file;
70              
71 0           do $hook_file;
72             }
73              
74 0           return Dredd::Hooks::Methods::get_hooks();
75             }
76              
77             # Run the hook callback specified for this event/transaction
78             # combination.
79              
80             sub _run_hooks {
81 0     0     my ($self, $hooks, $transaction) = @_;
82              
83 0 0         return $transaction unless $hooks;
84              
85 0 0         $hooks = [$hooks] unless ref $hooks eq 'ARRAY';
86              
87 0           for my $hook (@$hooks){
88 0 0 0       next unless $hook && ref $hook eq 'CODE';
89              
90 0           $hook->($transaction);
91 0           STDOUT->flush;
92             }
93 0           return $transaction;
94             }
95              
96             =head1 EVENT METHODS
97              
98             =head2 beforeEach_hook (transaction HashRef -> transaction HashRef)
99              
100             Runs hooks for the BeforeEach event from Dredd. This then calls
101             the before_hook before returning. This is because the before event
102             is not an event directly called by dredd.
103              
104             =cut
105              
106             sub beforeEach_hook {
107 0     0 1   my ($self, $transaction) = @_;
108              
109             return $self->before_hook(
110             $self->_run_hooks(
111             $self->_hooks->{beforeEach},
112 0           $transaction
113             )
114             );
115             }
116              
117             =head2 before_hook (transaction HashRef -> transaction HashRef)
118              
119             Runs the before event hooks and returns the modified transaction object
120              
121             NOTE: This is currently run by the beforeEach handler as the before event
122             is not an event directly sent from Dredd.
123              
124             =cut
125              
126             sub before_hook {
127 0     0 1   my ($self, $transaction) = @_;
128              
129             return $self->_run_hooks(
130             $self->_hooks->{before}{$transaction->{name}},
131 0           $transaction,
132             );
133             }
134              
135             =head2 beforeEachValidation_hook (transaction HashRef -> transaction HashRef)
136              
137             Handles the beforeEachValidation event from Dredd. This then calls the beforeValidation_hook
138             to handle the beforeValidation event.
139              
140             =cut
141              
142             sub beforeEachValidation_hook {
143 0     0 1   my ($self, $transaction) = @_;
144              
145             return $self->beforeValidation_hook(
146             $self->_run_hooks(
147             $self->_hooks->{beforeEachValidation},
148 0           $transaction
149             )
150             );
151             }
152              
153             =head2 beforeValidation_hook (transaction HashRef -> transaction HashRef)
154              
155             Handles the beforeValidation event and returns the modified transaction.
156              
157             NOTE: This event is called from beforeEachValidation_hook as it is not
158             and event directly run from Dredd.
159              
160             =cut
161              
162             sub beforeValidation_hook {
163 0     0 1   my ($self, $transaction) = @_;
164              
165             return $self->_run_hooks(
166             $self->_hooks->{beforeValidation}{$transaction->{name}},
167 0           $transaction
168             );
169             }
170              
171             =head2 afterEach_hook (transaction HashRef -> transaction HashRef)
172              
173             Handles the afterEach event from Dredd. Runs the after_hook handler
174             first before running hooks for this event.
175              
176             =cut
177              
178             sub afterEach_hook {
179 0     0 1   my ($self, $transaction) = @_;
180              
181             return $self->_run_hooks(
182             $self->_hooks->{afterEach},
183 0           $self->after_hook($transaction),
184             )
185             }
186              
187             =head2 after_hook (transaction HashRef -> transaction HashRef)
188              
189             Handles the after event and returns the modified transaction.
190              
191             NOTE: This event is called from the afterEach_hook as it is not
192             and event directly run from Dredd.
193              
194             =cut
195              
196             sub after_hook {
197 0     0 1   my ($self, $transaction) = @_;
198              
199             return $self->_run_hooks(
200             $self->_hooks->{after}{$transaction->{name}},
201 0           $transaction
202             )
203             }
204              
205             # *All hooks recieve and arrayref of hook transaction objects
206              
207             =head2 beforeAll (transactions ArrayRef[transaction] -> transactions ArrayRef[transaction])
208              
209             Handles the beforeAll event from Dredd. Receives an arrayref of transaction hashrefs and
210             returns the modified version.
211              
212             =cut
213              
214             sub beforeAll_hook {
215 0     0 0   my ($self, $transactions) = @_;
216              
217             return $self->_run_hooks(
218             $self->_hooks->{beforeAll},
219 0           $transactions
220             );
221             }
222              
223             =head2 afterAll (transactions ArrayRef[transaction] -> transactions ArrayRef[transaction])
224              
225             Handles the afterAll event from Dredd. Receives an arrayref of transaction hashrefs and
226             returns the modified version.
227              
228             =cut
229              
230             sub afterAll_hook {
231 0     0 0   my ($self, $transactions) = @_;
232              
233             return $self->_run_hooks(
234             $self->_hooks->{afterAll},
235 0           $transactions
236             );
237             }
238              
239             1;
240             __END__