Some cleanups to the m2m warnings test
[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 Data::Dumper;
7
8 plan tests => 2;
9
10 {
11   my @w; 
12   local $SIG{__WARN__} = sub { push @w, @_ };
13
14   my $code = gen_code ( suffix => 1 );
15   eval "$code";
16
17   ok ( (grep { $_ =~ /The many-to-many relationship bars is trying to create/ } @w), "Warning triggered without relevant 'no warnings'");
18 }
19
20 {
21   my @w; 
22   local $SIG{__WARN__} = sub { push @w, @_ };
23
24   my $code = gen_code ( suffix => 2, no_warn => 1 );
25   eval "$code";
26
27 diag Dumper \@w;
28
29   ok ( (not grep { $_ =~ /The many-to-many relationship bars is trying to create/ } @w), "No warning triggered with relevant 'no warnings'");
30 }
31
32 sub gen_code {
33
34   my $args = { @_ };
35   my $suffix = $args->{suffix};
36   my $no_warn = ( $args->{no_warn}
37     ? "no warnings 'DBIx::Class::Relationship::ManyToMany';"
38     : '',
39   );
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   __PACKAGE__->table('foo');
50   __PACKAGE__->add_columns(
51     'fooid' => {
52       data_type => 'integer',
53       is_auto_increment => 1,
54     },
55   );
56   __PACKAGE__->set_primary_key('fooid');
57
58
59   __PACKAGE__->has_many('foo_to_bar' => 'DBICTest::Schema::FooToBar${suffix}' => 'bar');
60   __PACKAGE__->many_to_many( foos => foo_to_bar => 'bar' );
61 }
62 {
63   package #
64     DBICTest::Schema::FooToBar${suffix};
65
66   use base 'DBIx::Class::Core';
67   __PACKAGE__->table('foo_to_bar');
68   __PACKAGE__->add_columns(
69     'foo' => {
70       data_type => 'integer',
71     },
72     'bar' => {
73       data_type => 'integer',
74     },
75   );
76   __PACKAGE__->belongs_to('foo' => 'DBICTest::Schema::Foo${suffix}');
77   __PACKAGE__->belongs_to('bar' => 'DBICTest::Schema::Foo${suffix}');
78 }
79 {
80   package #
81     DBICTest::Schema::Bar${suffix};
82
83   use base 'DBIx::Class::Core';
84   __PACKAGE__->table('bar');
85   __PACKAGE__->add_columns(
86     'barid' => {
87       data_type => 'integer',
88       is_auto_increment => 1,
89     },
90   );
91
92   ${no_warn}
93   __PACKAGE__->set_primary_key('barid');
94   __PACKAGE__->has_many('foo_to_bar' => 'DBICTest::Schema::FooToBar${suffix}' => 'foo');
95
96   __PACKAGE__->many_to_many( bars => foo_to_bar => 'foo' );
97
98   sub add_to_bars {}
99 }
100 EOF
101
102 }