Move the Subclassing POD under Examples
[dbsrgits/DBIx-Class.git] / t / 97result_class.t
CommitLineData
6c0ddbf7 1use strict;
8273e845 2use warnings;
6c0ddbf7 3
4use Test::More;
704a0f8e 5use Test::Warn;
c5bc9ba6 6use Test::Exception;
6c0ddbf7 7use lib qw(t/lib);
8use DBICTest;
9
10my $schema = DBICTest->init_schema();
11
6c0ddbf7 12{
13 my $cd_rc = $schema->resultset("CD")->result_class;
8273e845 14
c5bc9ba6 15 throws_ok {
16 $schema->resultset("Artist")
17 ->search_rs({}, {result_class => "IWillExplode"})
18 } qr/Can't locate IWillExplode/, 'nonexistant result_class exception';
19
20# to make ensure_class_loaded happy, dies on inflate
21 eval 'package IWillExplode; sub dummy {}';
22
6c0ddbf7 23 my $artist_rs = $schema->resultset("Artist")
24 ->search_rs({}, {result_class => "IWillExplode"});
25 is($artist_rs->result_class, 'IWillExplode', 'Correct artist result_class');
c5bc9ba6 26
27 throws_ok {
28 $artist_rs->result_class('mtfnpy')
29 } qr/Can't locate mtfnpy/,
30 'nonexistant result_access exception (from accessor)';
31
32 throws_ok {
33 $artist_rs->first
908aa1bb 34 } qr/\QInflator IWillExplode does not provide an inflate_result() method/,
c5bc9ba6 35 'IWillExplode explodes on inflate';
36
6c0ddbf7 37 my $cd_rs = $artist_rs->related_resultset('cds');
38 is($cd_rs->result_class, $cd_rc, 'Correct cd result_class');
b72d5dd3 39
40 my $cd_rs2 = $schema->resultset("Artist")->search_rs({})->related_resultset('cds');
41 is($cd_rs->result_class, $cd_rc, 'Correct cd2 result_class');
42
43 my $cd_rs3 = $schema->resultset("Artist")->search_rs({},{})->related_resultset('cds');
44 is($cd_rs->result_class, $cd_rc, 'Correct cd3 result_class');
8273e845 45
b72d5dd3 46 isa_ok(eval{ $cd_rs->find(1) }, $cd_rc, 'Inflated into correct cd result_class');
6c0ddbf7 47}
48
49
50{
51 my $cd_rc = $schema->resultset("CD")->result_class;
8273e845 52
6c0ddbf7 53 my $artist_rs = $schema->resultset("Artist")
b72d5dd3 54 ->search_rs({}, {result_class => "IWillExplode"})->search({artistid => 1});
6c0ddbf7 55 is($artist_rs->result_class, 'IWillExplode', 'Correct artist result_class');
8273e845 56
6c0ddbf7 57 my $cd_rs = $artist_rs->related_resultset('cds');
58 is($cd_rs->result_class, $cd_rc, 'Correct cd result_class');
8273e845 59
60 isa_ok(eval{ $cd_rs->find(1) }, $cd_rc, 'Inflated into correct cd result_class');
b72d5dd3 61 isa_ok(eval{ $cd_rs->search({ cdid => 1 })->first }, $cd_rc, 'Inflated into correct cd result_class');
6c0ddbf7 62}
704a0f8e 63
64{
65 my $rs = $schema->resultset('Artist')->search(
66 { 'cds.title' => 'Spoonful of bees' },
67 { prefetch => 'cds', result_class => 'DBIx::Class::ResultClass::HashRefInflator' },
68 );
69
70 is ($rs->result_class, 'DBIx::Class::ResultClass::HashRefInflator', 'starting with correct resultclass');
71
72 $rs->result_class('DBICTest::Artist');
73 is ($rs->result_class, 'DBICTest::Artist', 'resultclass changed');
74
75 my $art = $rs->next;
76 is (ref $art, 'DBICTest::Artist', 'Correcty blessed output');
77
78 throws_ok
79 { $rs->result_class('IWillExplode') }
80 qr/\QChanging the result_class of a ResultSet instance with an active cursor is not supported/,
81 'Throws on result class change in progress'
82 ;
83
84 my $cds = $art->cds;
85
86 warnings_exist
87 { $cds->result_class('IWillExplode') }
88 qr/\QChanging the result_class of a ResultSet instance with cached results is a noop/,
89 'Warning on noop result_class change'
90 ;
91
92 is ($cds->result_class, 'IWillExplode', 'class changed anyway');
93
94 # even though the original was HRI (at $rs definition time above)
95 # we lost the control over the *prefetched* object result class
96 # when we handed the root object creation to ::Row::inflate_result
97 is( ref $cds->next, 'DBICTest::CD', 'Correctly inflated prefetched result');
98}
99
100done_testing;