Test COMPONENT functionality
[catagits/CatalystX-DynamicComponent.git] / t / 03_dynamiccomponent.t
CommitLineData
4bb0a920 1use strict;
2use warnings;
3
7191ab41 4use Test::More tests => 10;
4bb0a920 5use Test::Exception;
6
7use Moose ();
8BEGIN { use_ok('CatalystX::DynamicComponent') }
9
10my $testapp_counter = 0;
11sub generate_testapp {
12 my $role_options = shift || {};
13 my $meta = Moose->init_meta( for_class => "TestApp" . $testapp_counter++ );
14 $meta->superclasses('Catalyst');
15 Moose::Util::apply_all_roles($meta, 'CatalystX::DynamicComponent', $role_options);
7191ab41 16 $meta->name->setup;
4bb0a920 17 return $meta;
18}
19
20throws_ok { generate_testapp(); }
21 qr/name\) is required/, 'name is required';
22
23{
24 my $app_meta = generate_testapp({ name => 'dynamic_component_method' });
25 my $app = $app_meta->name;
26 ok $app->can('dynamic_component_method'), 'dynamic component method added';
7191ab41 27 $app->dynamic_component_method( $app . "::Model::Foo", {} );
28 my $foo = $app->model('Foo');
29 ok $foo, 'Have added Foo component';
30 isa_ok($foo, 'Catalyst::Component');
31}
4bb0a920 32
7191ab41 33{
34 my $COMPONENT = sub { return bless {}, 'TestClass' };
35 my $app_meta = generate_testapp({
36 name => 'dynamic_component_method',
37 COMPONENT => $COMPONENT,
38 });
39 my $app = $app_meta->name;
40 ok $app->can('dynamic_component_method'), 'dynamic component method added';
41 $app->dynamic_component_method( $app . "::Model::Foo", {} );
42 my $foo = $app->model('Foo');
43 ok $foo, 'Have added Foo component';
44 isa_ok($foo, 'TestClass', 'COMPONENT method returned totally different class');
45 my $name = $app . "::Model::Foo";
46 isa_ok(bless({}, $name), 'Catalyst::Component', 'Underlying $app::Model::Foo is a C::C');
47 is($name->can('COMPONENT'), $COMPONENT, 'Supplied COMPONENT method is on $app::Model::Foo');
4bb0a920 48}
49
7191ab41 50