From: Moritz Onken Date: Sat, 13 Mar 2010 09:03:24 +0000 (+0000) Subject: test the dynamic subclassing example X-Git-Tag: v0.08121~66 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=db4e92211be8636ed47cf67b904d0d8479bc3114;p=dbsrgits%2FDBIx-Class.git test the dynamic subclassing example --- diff --git a/t/row/inflate_result.t b/t/row/inflate_result.t new file mode 100644 index 0000000..c31d804 --- /dev/null +++ b/t/row/inflate_result.t @@ -0,0 +1,111 @@ +package My::Schema::Result::User; + +use strict; +use warnings; +use base qw/DBIx::Class::Core/; + +### Define what our admin class is, for ensure_class_loaded() +my $admin_class = __PACKAGE__ . '::Admin'; + +__PACKAGE__->table('users'); + +__PACKAGE__->add_columns( + qw/user_id email password + firstname lastname active + admin/ +); + +__PACKAGE__->set_primary_key('user_id'); + +sub inflate_result { + my $self = shift; + my $ret = $self->next::method(@_); + if ( $ret->admin ) { ### If this is an admin, rebless for extra functions + $self->ensure_class_loaded($admin_class); + bless $ret, $admin_class; + } + return $ret; +} + +sub hello { + return "I am a regular user."; +} + +package My::Schema::Result::User::Admin; + +use strict; +use warnings; +use base qw/My::Schema::Result::User/; + +# This line is important +__PACKAGE__->table('users'); + +sub hello { + return "I am an admin."; +} + +sub do_admin_stuff { + return "I am doing admin stuff"; +} + +package My::Schema; + +use base qw/DBIx::Class::Schema/; + +My::Schema->register_class( Admin => 'My::Schema::Result::User::Admin' ); +My::Schema->register_class( User => 'My::Schema::Result::User' ); + +1; + +package main; + +use lib qw(t/lib); +use DBICTest; + +use Test::More; + +my $user_data = { + email => 'someguy@place.com', + password => 'pass1', + admin => 0 +}; + +my $admin_data = { + email => 'someadmin@adminplace.com', + password => 'pass2', + admin => 1 +}; + +ok( my $schema = My::Schema->connection('dbi:SQLite:dbname=:memory:') ); + +ok( + $schema->storage->dbh->do( +"create table users (user_id, email, password, firstname, lastname, active, admin)" + ) +); + +TODO: { + local $TODO = 'New objects should also be inflated'; + my $user = $schema->resultset('User')->create($user_data); + my $admin = $schema->resultset('User')->create($admin_data); + + is( ref $user, 'My::Schema::Result::User' ); + is( ref $admin, 'My::Schema::Result::User::Admin' ); + +} + +my $user = $schema->resultset('User')->single($user_data); +my $admin = $schema->resultset('User')->single($admin_data); + +is( ref $user, 'My::Schema::Result::User' ); +is( ref $admin, 'My::Schema::Result::User::Admin' ); + +is( $user->password, 'pass1' ); +is( $admin->password, 'pass2' ); +is( $user->hello, 'I am a regular user.' ); +is( $admin->hello, 'I am an admin.' ); + +ok( !$user->can('do_admin_stuff') ); +ok( $admin->can('do_admin_stuff') ); + +done_testing;