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