1# test for loading additional methods from file-defined packages
2# by Mark Hedges (  hedges   -at|   scriptdolphin.com )
3
4use strict;
5use Test::More tests => 7 * 5;
6use Test::Exception;
7
8use lib 't/lib';
9
10use make_dbictest_db;
11
12use DBIx::Class::Schema::Loader;
13
14$ENV{SCHEMA_LOADER_BACKCOMPAT} = 1;
15
16# In the first test run, then, Foo should be a DBICTestMethods::Namespaces::Schema::Result::Foo
17
18run_test_sequence(
19    testname        => "naming => 'current'",
20    schema_class    => 'DBICTestMethods::Namespaces::Schema',
21    foo_class       => 'DBICTestMethods::Namespaces::Schema::Result::Foo',
22    schema_opts     => {
23        naming => 'current',
24    },
25);
26
27# In the second test run with use_namespaces => 0 (backcompat), Foo should be a DBICTestMethods::Backcompat::Schema
28
29run_test_sequence(
30    testname        => "naming => 'current', use_namespaces => 0",
31    schema_class    => 'DBICTestMethods::Backcompat::Schema',
32    foo_class       => 'DBICTestMethods::Backcompat::Schema::Foo',
33    schema_opts     => {
34        naming              => 'current',
35        use_namespaces      => 0,
36    },
37);
38
39# In the third test, with use_namespaces => 1, Foo gets the explicit Result class again
40
41run_test_sequence(
42    testname        => "naming => 'current', use_namespaces => 1",
43    schema_class    => 'DBICTestMethods::Namespaces::Schema',
44    foo_class        => 'DBICTestMethods::Namespaces::Schema::Result::Foo',
45    schema_opts     => {
46        naming              => 'current',
47        use_namespaces      => 1,
48    },
49);
50
51# try it in full backcompat 0.04006 mode with no schema options
52
53run_test_sequence(
54    testname        => "no naming or namespaces options (0.04006 mode)",
55    schema_class    => 'DBICTestMethods::Backcompat::Schema',
56    foo_class        => 'DBICTestMethods::Backcompat::Schema::Foo',
57    schema_opts     => {
58    },
59);
60
61# try it in backcompat mode (no naming option) but with use_namespaces => 1
62
63run_test_sequence(
64    testname        => "no naming, but with use_namespaces options (0.04006 mode)",
65    schema_class    => 'DBICTestMethods::Namespaces::Schema',
66    foo_class        => 'DBICTestMethods::Namespaces::Schema::Result::Foo',
67    schema_opts     => {
68        use_namespaces      => 1,
69    },
70);
71
72sub run_test_sequence {
73    my %p = @_;
74    die "specify a $_ test param" for grep !$p{$_}, 
75        qw( testname schema_opts schema_class foo_class );
76
77    my $schema; 
78    lives_ok { $schema = make_schema_with(%p) } "($p{testname}) get schema";
79
80    SKIP: {
81        skip 'no point in checking if schema could not be connected', 6 unless defined $schema;
82
83        # well, if that worked, try to get a ResultSet
84        my $foo_rs;
85        lives_ok {
86            $foo_rs = $schema->resultset('Foo')->search();
87        } "($p{testname}) get a ResultSet for Foo";
88    
89        # get a foo
90        my $foo;
91        lives_ok {
92            $foo = $foo_rs->first();
93        } "($p{testname}) get the first foo";
94    
95        ok(defined $foo, "($p{testname}) \$foo is defined");
96    
97        SKIP: {
98            skip 'foo is not defined', 3 unless defined $foo;
99    
100            isa_ok $foo, $p{foo_class};
101    
102            # call the file-defined method
103            my $biz;
104            lives_ok {
105                $biz = $foo->biz();
106            } "($p{testname}) call the file-defined Foo->biz method";
107    
108            SKIP: {
109                skip 'no point in checking value if method was not found', 1 unless defined $biz;
110        
111                ok(
112                    $biz eq 'foo bar biz baz boz noz schnozz', 
113                    "($p{testname}) biz() method returns correct string"
114                );
115            }
116        }
117    }
118}
119    
120sub make_schema_with {
121    my %p = @_;
122    return DBIx::Class::Schema::Loader::make_schema_at(
123        $p{schema_class},
124        $p{schema_opts},
125        [ $make_dbictest_db::dsn ],
126    );
127}
128