Localize the warning tests.
[dbsrgits/DBIx-Class.git] / t / cdbi-t / columns_as_hashes.t
CommitLineData
5ef62e9f 1#!/usr/bin/perl -w
2
3use strict;
4use Test::More;
5use Test::Warn;
6
7BEGIN {
8 eval "use DBIx::Class::CDBICompat;";
9 plan $@ ? (skip_all => "Class::Trigger and DBIx::ContextualFetch required: $@")
92a23d90 10 : (tests=> 10);
5ef62e9f 11}
12
13use lib 't/testlib';
14use Film;
15
16my $waves = Film->insert({
17 Title => "Breaking the Waves",
18 Director => 'Lars von Trier',
19 Rating => 'R'
20});
21
8ed9eec6 22local $ENV{DBIC_CDBICOMPAT_HASH_WARN} = 0;
10221b79 23
8ed9eec6 24{
25 local $ENV{DBIC_CDBICOMPAT_HASH_WARN} = 1;
ebe790db 26
8ed9eec6 27 warnings_like {
28 my $rating = $waves->{rating};
29 $waves->Rating("PG");
30 is $rating, "R", 'evaluation of column value is not deferred';
31 } qr{^Column 'rating' of 'Film/$waves' was fetched as a hash at \Q$0};
5ef62e9f 32
8ed9eec6 33 warnings_like {
34 is $waves->{title}, $waves->Title, "columns can be accessed as hashes";
35 } qr{^Column 'title' of 'Film/$waves' was fetched as a hash at\b};
5ef62e9f 36
8ed9eec6 37 $waves->Rating("G");
5ef62e9f 38
8ed9eec6 39 warnings_like {
40 is $waves->{rating}, "G", "updating via the accessor updates the hash";
41 } qr{^Column 'rating' of 'Film/$waves' was fetched as a hash at\b};
5ef62e9f 42
5ef62e9f 43
8ed9eec6 44 warnings_like {
45 $waves->{rating} = "PG";
46 } qr{^Column 'rating' of 'Film/$waves' was stored as a hash at\b};
10221b79 47
8ed9eec6 48 $waves->update;
49 my @films = Film->search( Rating => "PG", Title => "Breaking the Waves" );
50 is @films, 1, "column updated as hash was saved";
51}
10221b79 52
53warning_is {
10221b79 54 $waves->{rating}
92a23d90 55} '', 'DBIC_CDBICOMPAT_HASH_WARN controls warnings';
56
57
8ed9eec6 58{
92a23d90 59 $waves->rating("R");
60 $waves->update;
61
62 no warnings 'redefine';
63 local *Film::rating = sub {
64 return "wibble";
65 };
66
67 is $waves->{rating}, "R";
68}
8ed9eec6 69