a6f006eedd2e8eb6e05ed9030b218b68319143bf
[catagits/CatalystX-DynamicComponent.git] / t / 03_dynamiccomponent.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 10;
5 use Test::Exception;
6
7 use Moose ();
8 BEGIN { use_ok('CatalystX::DynamicComponent') }
9
10 my $testapp_counter = 0;
11 sub 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);
16     $meta->name->setup;
17     return $meta;
18 }
19
20 throws_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';
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 }
32
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');
48 }
49
50