File Coverage

blib/lib/Test/Plack/Handler/Stomp.pm
Criterion Covered Total %
statement 44 48 91.6
branch n/a
condition n/a
subroutine 12 13 92.3
pod 6 6 100.0
total 62 67 92.5


line stmt bran cond sub pod time code
1             package Test::Plack::Handler::Stomp;
2             $Test::Plack::Handler::Stomp::VERSION = '1.14';
3             {
4             $Test::Plack::Handler::Stomp::DIST = 'Plack-Handler-Stomp';
5             }
6 11     11   7368680 use Moose;
  11         98  
  11         119  
7 11     11   71343 use Moose::Util::TypeConstraints 'class_type';
  11         25  
  11         108  
8 11     11   7681 use MooseX::Types::Moose qw(ArrayRef HashRef Maybe);
  11         518724  
  11         101  
9              
10 11     11   53313 use namespace::autoclean;
  11         28  
  11         91  
11 11     11   5493 use Test::Plack::Handler::Stomp::FakeStomp;
  11         32  
  11         119  
12 11     11   4168 use Plack::Handler::Stomp;
  11         44  
  11         7005  
13              
14             # ABSTRACT: testing library for Plack::Handler::Stomp
15              
16              
17              
18             has handler_args => (
19             is => 'ro',
20             isa => HashRef,
21             default => sub { {
22             one_shot => 1,
23             } },
24             traits => ['Hash'],
25             handles => {
26             set_arg => 'set',
27             },
28             );
29              
30              
31             has handler => (
32             is => 'ro',
33             isa => class_type('Plack::Handler::Stomp'),
34             lazy => 1,
35             builder => 'setup_handler',
36             clearer => 'clear_handler',
37             );
38              
39              
40             has frames_sent => (
41             is => 'rw',
42             isa => ArrayRef[class_type('Net::Stomp::Frame')],
43             default => sub { [ ] },
44             traits => ['Array'],
45             handles => {
46             queue_sent_frame => 'push',
47             sent_frames_count => 'count',
48             clear_sent_frames => 'clear',
49             }
50             );
51              
52              
53             has frames_to_receive => (
54             is => 'rw',
55             isa => ArrayRef[class_type('Net::Stomp::Frame')],
56             default => sub { [ Net::Stomp::Frame->new({
57             command => 'ERROR',
58             headers => {
59             message => 'placeholder from ' . __PACKAGE__,
60             },
61             body => '',
62             }) ] },
63             traits => ['Array'],
64             handles => {
65             queue_frame_to_receive => 'push',
66             next_frame_to_receive => 'shift',
67             frames_left_to_receive => 'count',
68             clear_frames_to_receive => 'clear',
69             },
70             );
71              
72              
73             has constructor_calls => (
74             is => 'rw',
75             isa => ArrayRef[HashRef],
76             default => sub { [ ] },
77             traits => ['Array'],
78             handles => {
79             queue_constructor_call => 'push',
80             constructor_calls_count => 'count',
81             clear_constructor_calls => 'clear',
82             },
83             );
84              
85              
86             has connection_calls => (
87             is => 'rw',
88             isa => ArrayRef[Maybe[HashRef]],
89             default => sub { [ ] },
90             traits => ['Array'],
91             handles => {
92             queue_connection_call => 'push',
93             connection_calls_count => 'count',
94             clear_connection_calls => 'clear',
95             },
96             );
97              
98              
99             has disconnection_calls => (
100             is => 'rw',
101             isa => ArrayRef[Maybe[HashRef]],
102             default => sub { [ ] },
103             traits => ['Array'],
104             handles => {
105             queue_disconnection_call => 'push',
106             disconnection_calls_count => 'count',
107             clear_disconnection_calls => 'clear',
108             },
109             );
110              
111              
112             has subscription_calls => (
113             is => 'rw',
114             isa => ArrayRef[Maybe[HashRef]],
115             default => sub { [ ] },
116             traits => ['Array'],
117             handles => {
118             queue_subscription_call => 'push',
119             subscription_calls_count => 'count',
120             clear_subscription_calls => 'clear',
121             },
122             );
123              
124              
125             has unsubscription_calls => (
126             is => 'rw',
127             isa => ArrayRef[Maybe[HashRef]],
128             default => sub { [ ] },
129             traits => ['Array'],
130             handles => {
131             queue_unsubscription_call => 'push',
132             unsubscription_calls_count => 'count',
133             clear_unsubscription_calls => 'clear',
134             },
135             );
136              
137              
138             has log_messages => (
139             is => 'rw',
140             isa => ArrayRef,
141             traits => ['Array'],
142             handles => {
143             add_log_message => 'push',
144             log_messages_count => 'count',
145             clear_log_messages => 'clear',
146             },
147             );
148              
149              
150             sub setup_handler {
151 12     12 1 36 my ($self) = @_;
152              
153             return Plack::Handler::Stomp->new({
154             logger => $self,
155 12         354 %{$self->handler_args},
156             connection_builder => sub {
157 14     14   83092 my ($params) = @_;
158             return Test::Plack::Handler::Stomp::FakeStomp->new({
159 14         598 new => sub { $self->queue_constructor_call(shift) },
160 13         507 connect => sub { $self->queue_connection_call(shift) },
161 0         0 disconnect => sub { $self->queue_disconnection_call(shift) },
162 18         693 subscribe => sub { $self->queue_subscription_call(shift) },
163 0         0 unsubscribe => sub { $self->queue_unsubscription_call(shift) },
164 11         451 send_frame => sub { $self->queue_sent_frame(shift) },
165 18         686 receive_frame => sub { $self->next_frame_to_receive() },
166 14         492 },$params);
167             },
168             })
169 12         29 }
170              
171              
172             sub debug {
173 1     1 1 9 my ($self,@msg) = @_;
174 1         42 $self->add_log_message(['debug',@msg]);
175             }
176             sub info {
177 0     0 1 0 my ($self,@msg) = @_;
178 0         0 $self->add_log_message(['info',@msg]);
179             }
180             sub warn {
181 5     5 1 16 my ($self,@msg) = @_;
182 5         171 $self->add_log_message(['warn',@msg]);
183             }
184             sub error {
185 1     1 1 4 my ($self,@msg) = @_;
186 1         34 $self->add_log_message(['error',@msg]);
187             }
188              
189              
190             sub clear_calls_and_queues {
191 3     3 1 2661 my ($self) = @_;
192 3         134 $self->clear_sent_frames;
193 3         131 $self->clear_frames_to_receive;
194 3         134 $self->clear_constructor_calls;
195 3         144 $self->clear_connection_calls;
196 3         154 $self->clear_disconnection_calls;
197 3         137 $self->clear_subscription_calls;
198 3         133 $self->clear_unsubscription_calls;
199 3         127 $self->clear_log_messages;
200 3         8 return;
201             }
202              
203             __PACKAGE__->meta->make_immutable;
204              
205             1;
206              
207             __END__
208              
209             =pod
210              
211             =encoding UTF-8
212              
213             =head1 NAME
214              
215             Test::Plack::Handler::Stomp - testing library for Plack::Handler::Stomp
216              
217             =head1 VERSION
218              
219             version 1.14
220              
221             =head1 SYNOPSIS
222              
223             my $t = Test::Plack::Handler::Stomp->new();
224             $t->set_arg(
225             subscriptions => [
226             { destination => '/queue/input_queue',
227             path_info => '/input_queue', },
228             ],
229             );
230             $t->clear_frames_to_receive;
231              
232             $t->queue_frame_to_receive(Net::Stomp::Frame->new({
233             command => 'MESSAGE',
234             headers => {
235             destination => '/queue/input_queue',
236             subscription => 0,
237             type => 'my_type',
238             'message-id' => 356,
239             },
240             body => '{"foo":"bar"}',
241             }));
242              
243             $t->handler->run($app);
244              
245             check($t->frames_sent);
246              
247             =head1 DESCRIPTION
248              
249             Testing a PSGI application that expects JMS/STOMP messages can be a
250             pain. This library helps reduce that pain.
251              
252             It wraps a L<Plack::Handler::Stomp>, connecting it to a
253             L<Test::Plack::Handler::Stomp::FakeStomp> instead of a real STOMP
254             connection, and allows you to inspect everything that happens to the
255             connection.
256              
257             =head1 ATTRIBUTES
258              
259             =head2 C<handler_args>
260              
261             Hashref, arguments to pass to L<Plack::Handler::Stomp>'s
262             constructor. You can add to this via the L</set_arg> method. Defaults
263             to C<< { one_shot => 1 } >>, to avoid having L<Plack::Handler::Stomp>
264             loop forever.
265              
266             =head2 C<handler>
267              
268             A L<Plack::Handler::Stomp> instance. It's built on-demand via
269             L</setup_handler>. You can clear it with L</clear_handler> to have it
270             rebuilt (for example, if you have changed L</handler_args>)
271              
272             =head2 C<frames_sent>
273              
274             Arrayref of L<Net::Stomp::Frame> objects that L<Plack::Handler::Stomp>
275             sent. Can be edited via L</queue_sent_frame>, L</sent_frames_count>,
276             L</clear_sent_frames>. Defaults to the empty array.
277              
278             =head2 C<frames_to_receive>
279              
280             Arrayref of L<Net::Stomp::Frame> objects that L<Plack::Handler::Stomp>
281             will consume. Can be edited via L</queue_frame_to_receive>,
282             L</next_frame_to_receive>, L</frames_left_to_receive>,
283             L</clear_frames_to_receive>.
284              
285             Defaults to an array with a single C<ERROR> frame.
286              
287             =head2 C<constructor_calls>
288              
289             Arrayref of whatever was passed to the
290             L<Test::Plack::Handler::Stomp::FakeStomp> constructor. Can be edited
291             via L</queue_constructor_call>, L</constructor_calls_count>,
292             L</clear_constructor_calls>.
293              
294             =head2 C<connection_calls>
295              
296             Arrayref of whatever was passed to the
297             L<Test::Plack::Handler::Stomp::FakeStomp> C<connect> method. Can be
298             edited via L</queue_connection_call>, L</connection_calls_count>,
299             L</clear_connection_calls>.
300              
301             =head2 C<disconnection_calls>
302              
303             Arrayref of whatever was passed to the
304             L<Test::Plack::Handler::Stomp::FakeStomp> C<disconnect> method. Can be
305             edited via L</queue_disconnection_call>,
306             L</disconnection_calls_count>, L</clear_disconnection_calls>.
307              
308             =head2 C<subscription_calls>
309              
310             Arrayref of whatever was passed to the
311             L<Test::Plack::Handler::Stomp::FakeStomp> C<subscribe> method. Can be
312             edited via L</queue_subscription_call>, L</subscription_calls_count>,
313             L</clear_subscription_calls>.
314              
315             =head2 C<unsubscription_calls>
316              
317             Arrayref of whatever was passed to the
318             L<Test::Plack::Handler::Stomp::FakeStomp> C<unsubscribe> method. Can
319             be edited via L</queue_unsubscription_call>,
320             L</unsubscription_calls_count>, L</clear_unsubscription_calls>.
321              
322             =head2 C<log_messages>
323              
324             Arrayref of whatever L<Plack::Handler::Stomp> logs. Each element is a
325             pair C<< [ $level, $message ] >>. Can be edited via
326             L</add_log_message>, L</log_messages_count>, L</clear_log_messages>.
327              
328             =head1 METHODS
329              
330             =head2 C<set_arg>
331              
332             $handler->set_arg(foo=>'bar',some=>'thing');
333              
334             Sets arguments for L<Plack::Handler::Stomp>'s constructor, see
335             C</handler_args>.
336              
337             =head2 C<clear_handler>
338              
339             Destroys the L</handler>, forcing it to be rebuilt next time it's
340             needed.
341              
342             =head2 C<queue_sent_frame>
343              
344             Adds a frame to the end of L</frames_sent>.
345              
346             =head2 C<sent_frames_count>
347              
348             Returns the number of elements in L</frames_sent>.
349              
350             =head2 C<clear_sent_frames>
351              
352             Removes all elements from L</frames_sent>.
353              
354             =head2 C<queue_frame_to_receive>
355              
356             Adds a frame to the end of L</frames_to_receive>.
357              
358             =head2 C<next_frame_to_receive>
359              
360             Removes a frame from the beginning of L</frames_to_receive> and
361             returns it.
362              
363             =head2 C<frames_left_to_receive>
364              
365             Returns the number of elements in L</frames_to_receive>.
366              
367             =head2 C<clear_frames_to_receive>
368              
369             Removes all elements from L</frames_to_receive>.
370              
371             =head2 C<queue_constructor_call>
372              
373             Adds a hashref to the end of L</constructor_calls>.
374              
375             =head2 C<constructor_calls_count>
376              
377             Returns the number of elements in L</constructor_calls>.
378              
379             =head2 C<clear_constructor_calls>
380              
381             Removes all elements from L</constructor_calls>.
382              
383             =head2 C<queue_connection_call>
384              
385             Adds a hashref to the end of L</connection_calls>.
386              
387             =head2 C<connection_calls_count>
388              
389             Returns the number of elements in L</connection_calls>.
390              
391             =head2 C<clear_connection_calls>
392              
393             Removes all elements from L</connection_calls>.
394              
395             =head2 C<queue_disconnection_call>
396              
397             Adds a hashref to the end of L</disconnection_calls>.
398              
399             =head2 C<disconnection_calls_count>
400              
401             Returns the number of elements in L</disconnection_calls>.
402              
403             =head2 C<clear_disconnection_calls>
404              
405             Removes all elements from L</disconnection_calls>.
406              
407             =head2 C<queue_subscription_call>
408              
409             Adds a hashref to the end of L</subscription_calls>.
410              
411             =head2 C<subscription_calls_count>
412              
413             Returns the number of elements in L</subscription_calls>.
414              
415             =head2 C<clear_subscription_calls>
416              
417             Removes all elements from L</subscription_calls>.
418              
419             =head2 C<queue_unsubscription_call>
420              
421             Adds a hashref to the end of L</unsubscription_calls>.
422              
423             =head2 C<unsubscription_calls_count>
424              
425             Returns the number of elements in L</unsubscription_calls>.
426              
427             =head2 C<clear_unsubscription_calls>
428              
429             Removes all elements from L</unsubscription_calls>.
430              
431             =head2 C<add_log_message>
432              
433             Adds a pair to the end of L</log_messages>.
434              
435             =head2 C<log_messages_count>
436              
437             Returns the number of elements in L</log_messages>.
438              
439             =head2 C<clear_log_messages>
440              
441             Removes all elements from L</log_messages>.
442              
443             =head2 C<setup_handler>
444              
445             Constructs a L<Plack::Handler::Stomp>, setting it up to capture
446             logging, passing L</handler_args>, and setting a C<connection_builder>
447             that returns a L<Test::Plack::Handler::Stomp::FakeStomp> with all the
448             callbacks set to accumulate calls in this object.
449              
450             =head2 C<debug>
451              
452             =head2 C<info>
453              
454             =head2 C<warn>
455              
456             =head2 C<error>
457              
458             Logger delegate methods, the handler returned by L</setup_handler>
459             uses these to log. These methods accumulate log messages by calling
460             L</add_log_message>.
461              
462             =head2 C<clear_calls_and_queues>
463              
464             Calls the clearer for all the queue / accumulator attributes
465             (L</frames_sent>, L</frames_to_receive>, L</constructor_calls>,
466             L</connection_calls>, L</disconnection_calls>, L</subscription_calls>,
467             L</unsubscription_calls>, L</log_messages>)
468              
469             =head1 AUTHOR
470              
471             Gianni Ceccarelli <gianni.ceccarelli@net-a-porter.com>
472              
473             =head1 COPYRIGHT AND LICENSE
474              
475             This software is copyright (c) 2012 by Net-a-porter.com.
476              
477             This is free software; you can redistribute it and/or modify it under
478             the same terms as the Perl 5 programming language system itself.
479              
480             =cut