moved tests to compose_namespace instead of compose_connection, marked compose_connec...
[dbsrgits/DBIx-Class.git] / t / 34exception_action.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 plan tests => 6;
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 sub throwex { $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 eval { throwex };
22 like($@, $ex_regex);
23
24 # Now lets rethrow via exception_action
25 $schema->exception_action(sub { die @_ });
26 eval { throwex };
27 like($@, $ex_regex);
28
29 # Now lets suppress the error
30 $schema->exception_action(sub { 1 });
31 eval { throwex };
32 ok(!$@, "Suppress exception");
33
34 # Now lets fall through and let croak take back over
35 $schema->exception_action(sub { return });
36 eval { throwex };
37 like($@, $ex_regex);
38
39 # Whacky useless exception class
40 {
41     package DBICTest::Exception;
42     use overload '""' => \&stringify, fallback => 1;
43     sub new {
44         my $class = shift;
45         bless { msg => shift }, $class;
46     }
47     sub throw {
48         my $self = shift;
49         die $self if ref $self eq __PACKAGE__;
50         die $self->new(shift);
51     }
52     sub stringify {
53         "DBICTest::Exception is handling this: " . shift->{msg};
54     }
55 }
56
57 # Try the exception class
58 $schema->exception_action(sub { DBICTest::Exception->throw(@_) });
59 eval { throwex };
60 like($@, qr/DBICTest::Exception is handling this: $ex_regex/);
61
62 # While we're at it, lets throw a custom exception through Storage::DBI
63 eval { $schema->storage->throw_exception('floob') };
64 like($@, qr/DBICTest::Exception is handling this: floob/);