Only load DBICTest::Schema when needed in tests
[dbsrgits/DBIx-Class.git] / t / 103many_to_many_warning.t
1 use strict;
2 use warnings;
3 use Test::More;
4
5 use lib qw(t/lib);
6 use DBICTest;
7 use DBICTest::Schema;
8
9 my $exp_warn = qr/The many-to-many relationship 'bars' is trying to create/;
10
11 {
12   my @w;
13   local $SIG{__WARN__} = sub { $_[0] =~ $exp_warn ? push @w, $_[0] : warn $_[0] };
14   my $code = gen_code ( suffix => 1 );
15
16   local $ENV{DBIC_OVERWRITE_HELPER_METHODS_OK};
17   eval "$code";
18   ok (! $@, 'Eval code without warnings suppression')
19     || diag $@;
20
21   ok (@w, "Warning triggered without DBIC_OVERWRITE_HELPER_METHODS_OK");
22 }
23
24 {
25   my @w;
26   local $SIG{__WARN__} = sub { $_[0] =~ $exp_warn ? push @w, $_[0] : warn $_[0] };
27
28   my $code = gen_code ( suffix => 2 );
29
30   local $ENV{DBIC_OVERWRITE_HELPER_METHODS_OK} = 1;
31   eval "$code";
32   ok (! $@, 'Eval code with warnings suppression')
33     || diag $@;
34
35   ok (! @w, "No warning triggered with DBIC_OVERWRITE_HELPER_METHODS_OK");
36 }
37
38 sub gen_code {
39
40   my $args = { @_ };
41   my $suffix = $args->{suffix};
42
43   return <<EOF;
44 use strict;
45 use warnings;
46
47 {
48   package #
49     DBICTest::Schema::Foo${suffix};
50   use base 'DBIx::Class::Core';
51
52   __PACKAGE__->table('foo');
53   __PACKAGE__->add_columns(
54     'fooid' => {
55       data_type => 'integer',
56       is_auto_increment => 1,
57     },
58   );
59   __PACKAGE__->set_primary_key('fooid');
60
61
62   __PACKAGE__->has_many('foo_to_bar' => 'DBICTest::Schema::FooToBar${suffix}' => 'bar');
63   __PACKAGE__->many_to_many( foos => foo_to_bar => 'bar' );
64 }
65 {
66   package #
67     DBICTest::Schema::FooToBar${suffix};
68
69   use base 'DBIx::Class::Core';
70   __PACKAGE__->table('foo_to_bar');
71   __PACKAGE__->add_columns(
72     'foo' => {
73       data_type => 'integer',
74     },
75     'bar' => {
76       data_type => 'integer',
77     },
78   );
79   __PACKAGE__->belongs_to('foo' => 'DBICTest::Schema::Foo${suffix}');
80   __PACKAGE__->belongs_to('bar' => 'DBICTest::Schema::Foo${suffix}');
81 }
82 {
83   package #
84     DBICTest::Schema::Bar${suffix};
85
86   use base 'DBIx::Class::Core';
87
88   __PACKAGE__->table('bar');
89   __PACKAGE__->add_columns(
90     'barid' => {
91       data_type => 'integer',
92       is_auto_increment => 1,
93     },
94   );
95
96   __PACKAGE__->set_primary_key('barid');
97   __PACKAGE__->has_many('foo_to_bar' => 'DBICTest::Schema::FooToBar${suffix}' => 'foo');
98
99   __PACKAGE__->many_to_many( bars => foo_to_bar => 'foo' );
100
101   sub add_to_bars {}
102 }
103 EOF
104
105 }
106
107 done_testing;