Commit | Line | Data |
4bb0a920 |
1 | use strict; |
2 | use warnings; |
3 | |
c52d8688 |
4 | use Test::More tests => 15; |
4bb0a920 |
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); |
7191ab41 |
16 | $meta->name->setup; |
4bb0a920 |
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'; |
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 | |
c52d8688 |
50 | { |
51 | package My::Model; |
52 | use Moose; |
53 | extends qw/Catalyst::Model/; |
54 | __PACKAGE__->meta->make_immutable; |
55 | } |
56 | { |
57 | package My::Role; |
58 | use Moose::Role; |
59 | sub _some_method_from_role {} |
60 | } |
61 | { |
62 | my $app_meta = generate_testapp({ |
63 | name => 'dynamic_component_method', |
64 | }); |
65 | my $app = $app_meta->name; |
66 | my $extra_config = { |
67 | superclasses => ['My::Model'], |
68 | roles => ['My::Role'], |
69 | methods => { |
70 | my_injected_method => sub { 'quuux' }, |
71 | } |
72 | }; |
73 | $app->dynamic_component_method( $app . "::Model::Foo", $extra_config ); |
74 | my $model = $app->model('Foo'); |
75 | isa_ok($model, 'My::Model'); |
76 | ok $model->can('_some_method_from_role'), 'Has had role applied'; |
77 | ok !My::Model->can('_some_method_from_role'), 'Role applied at right place'; |
78 | ok $model->can('my_injected_method'), 'Injected method there as expected'; |
79 | is $model->my_injected_method, 'quuux', 'Injected method returns correct val'; |
80 | } |
7191ab41 |
81 | |