d7885d5e23e77e1c1ceb10768e2e72f7079428e4
[dbsrgits/DBIx-Class.git] / t / 34exception_action.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use Test::Warn;
7 use lib qw(t/lib);
8 use DBICTest;
9
10 # Set up the "usual" sqlite for DBICTest
11 my $schema = DBICTest->init_schema;
12
13 # This is how we're generating exceptions in the rest of these tests,
14 #  which might need updating at some future time to be some other
15 #  exception-generating statement:
16
17 my $throw  = sub { $schema->resultset("Artist")->search(1,1,1) };
18 my $ex_regex = qr/Odd number of arguments to search/;
19
20 # Basic check, normal exception
21 throws_ok \&$throw, $ex_regex;
22
23 my $e = $@;
24
25 # Re-throw the exception with rethrow()
26 throws_ok { $e->rethrow }
27   $ex_regex;
28 isa_ok( $@, 'DBIx::Class::Exception' );
29
30 # Now lets rethrow via exception_action
31 {
32   my $handler_execution_counter = 0;
33
34   $schema->exception_action(sub {
35     $handler_execution_counter++;
36     like $_[0], $ex_regex, "Exception is precisely passed to exception_action";
37     die @_
38   });
39
40   throws_ok \&$throw, $ex_regex;
41   is $handler_execution_counter, 1, "exception_action handler executed exactly once";
42 }
43
44 #
45 # This should have never worked!!!
46 #
47 # Now lets suppress the error
48 $schema->exception_action(sub { 1 });
49 throws_ok \&$throw,
50   qr/exception_action handler .+ did \*not\* result in an exception.+original error: $ex_regex/;
51
52 # Now lets fall through and let croak take back over
53 $schema->exception_action(sub { return });
54 throws_ok {
55   warnings_are \&$throw,
56     qr/exception_action handler installed .+ returned false instead throwing an exception/;
57 } $ex_regex;
58
59 # again to see if no warning
60 throws_ok {
61   warnings_are \&$throw,
62     [];
63 } $ex_regex;
64
65
66 # Whacky useless exception class
67 {
68     package DBICTest::Exception;
69     use overload '""' => \&stringify, fallback => 1;
70     sub new {
71         my $class = shift;
72         bless { msg => shift }, $class;
73     }
74     sub throw {
75         my $self = shift;
76         die $self if ref $self eq __PACKAGE__;
77         die $self->new(shift);
78     }
79     sub stringify {
80         "DBICTest::Exception is handling this: " . shift->{msg};
81     }
82 }
83
84 # Try the exception class
85 $schema->exception_action(sub { DBICTest::Exception->throw(@_) });
86 throws_ok \&$throw,
87   qr/DBICTest::Exception is handling this: $ex_regex/;
88
89 # While we're at it, lets throw a custom exception through Storage::DBI
90 throws_ok { $schema->storage->throw_exception('floob') }
91   qr/DBICTest::Exception is handling this: floob/;
92
93 # test antipatterns
94 for my $ap (qw(
95   DBICTest::AntiPattern::TrueZeroLen
96   DBICTest::AntiPattern::NullObject
97 )) {
98   eval "require $ap";
99   my $exp_warn = qr/\QObjects of external exception class '$ap' stringify to '' (the empty string)/;
100
101   # make sure an exception_action can replace $@ with an antipattern
102   $schema->exception_action(sub { die $ap->new });
103   warnings_like {
104     eval { $throw->() };
105     isa_ok $@, $ap;
106   } $exp_warn, 'proper warning on antipattern encountered within exception_action';
107
108   # and make sure that the rethrow works
109   $schema->exception_action(sub { die @_ });
110   warnings_like {
111     eval {
112       $schema->txn_do (sub { die $ap->new });
113     };
114
115     isa_ok $@, $ap;
116   } $exp_warn, 'Proper warning on encountered antipattern';
117 }
118
119 done_testing;