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