| 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<Dredd|https://dredd.readthedocs.org> API testing suite. | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | Unless you are righting your own TCP server to accept Dredd Hook requests | ||||||||||||||||||||||||||||
| 23 | then you likely want L<Dredd::Hooks::Methods> which describes how to write | ||||||||||||||||||||||||||||
| 24 | Dredd Hook files. | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =cut | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use Dredd::Hooks::Methods '-handler'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | our $VERSION = "0.07"; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | use Types::Standard qw/HashRef ArrayRef/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Stores the hooks that result from Dredd::Hooks::Methods | ||||||||||||||||||||||||||||
| 37 | 1 | 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<Dredd::Hooks::Methods> for information on creating these | ||||||||||||||||||||||||||||
| 50 | files | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =cut | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | 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 | 0 | ▶ | sub _build__hooks { | ||||||||||||||||||||||||||
|
Subroutine
_build__hooks: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 63 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | my $hook_files = $self->hook_files; | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | return {} unless $hook_files && scalar @$hook_files; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$hook_files and scalar @$hook_files
MC/DC0%0/1
$hook_files and scalar @$hook_files
$hook_files
Truth table0%0/2
A
A= $hook_files
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | for my $hook_file (@$hook_files){ | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | next unless -e $hook_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub _run_hooks { | ||||||||||||||||||||||||||
|
Subroutine
_run_hooks: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 81 | 0 | my ($self, $hooks, $transaction) = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | return $transaction unless $hooks; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | $hooks = [$hooks] unless ref $hooks eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | for my $hook (@$hooks){ | |||||||||||||||||||||||||||
| 88 | 0 | ▶ | next unless $hook && ref $hook eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$hook and ref $hook eq "CODE"
MC/DC0%0/2
$hook and ref $hook eq "CODE"
$hook
ref $hook eq "CODE"
Truth table0%0/3
A and B
A= $hookB= 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 | 0 | ▶ | sub beforeEach_hook { | ||||||||||||||||||||||||||
|
Subroutine
beforeEach_hook: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 0 | 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 | 0 | ▶ | sub before_hook { | ||||||||||||||||||||||||||
|
Subroutine
before_hook: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 127 | 0 | 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 | 0 | ▶ | sub beforeEachValidation_hook { | ||||||||||||||||||||||||||
|
Subroutine
beforeEachValidation_hook: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | 0 | 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 | 0 | ▶ | sub beforeValidation_hook { | ||||||||||||||||||||||||||
|
Subroutine
beforeValidation_hook: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 0 | 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 | 0 | ▶ | sub afterEach_hook { | ||||||||||||||||||||||||||
|
Subroutine
afterEach_hook: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 179 | 0 | 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 | 0 | ▶ | sub after_hook { | ||||||||||||||||||||||||||
|
Subroutine
after_hook: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 197 | 0 | 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 | 0 | ▶ | sub beforeAll_hook { | ||||||||||||||||||||||||||
|
Subroutine
beforeAll_hook: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 215 | 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 | 0 | ▶ | sub afterAll_hook { | ||||||||||||||||||||||||||
|
Subroutine
afterAll_hook: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 231 | 0 | my ($self, $transactions) = @_; | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | return $self->_run_hooks( | ||||||||||||||||||||||||||||
| 234 | $self->_hooks->{afterAll}, | ||||||||||||||||||||||||||||
| 235 | 0 | $transactions | |||||||||||||||||||||||||||
| 236 | ); | ||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 1 | 1; | |||||||||||||||||||||||||||
| 240 | __END__ | ||||||||||||||||||||||||||||