4 use Test::More tests => 32;
8 BEGIN { use_ok('CatalystX::DynamicComponent') }
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);
20 throws_ok { generate_testapp(); }
21 qr/name\) is required/, 'name is required';
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');
34 my $COMPONENT = sub { return bless {}, 'TestClass' };
35 my $app_meta = generate_testapp({
36 name => 'dynamic_component_method',
38 COMPONENT => $COMPONENT,
41 my $app = $app_meta->name;
42 ok $app->can('dynamic_component_method'), 'dynamic component method added';
43 $app->dynamic_component_method( $app . "::Model::Foo", {} );
44 my $foo = $app->model('Foo');
45 ok $foo, 'Have added Foo component';
46 isa_ok($foo, 'TestClass', 'COMPONENT method returned totally different class');
47 my $name = $app . "::Model::Foo";
48 isa_ok(bless({}, $name), 'Catalyst::Component', 'Underlying $app::Model::Foo is a C::C');
49 is($name->can('COMPONENT'), $COMPONENT, 'Supplied COMPONENT method is on $app::Model::Foo');
52 package My::Other::Superclass;
54 __PACKAGE__->meta->make_immutable;
59 extends qw/Catalyst::Model/;
60 __PACKAGE__->meta->make_immutable;
65 sub _some_method_from_role {}
68 package My::Other::Role;
70 sub _some_method_from_other_role {}
72 my %generator_config = (
73 name => 'dynamic_component_method',
74 roles => [qw/My::Other::Role/],
75 superclasses => ['My::Other::Superclass'],
77 my_other_injected_method => sub {},
81 superclasses => ['My::Model'],
82 roles => ['My::Role'],
84 my_injected_method => sub { 'quuux' },
88 # Do not specify any resolve strategies, so get defaults:
90 # superclasses - replace
92 my $app_meta = generate_testapp({
95 my $app = $app_meta->name;
96 $app->dynamic_component_method( $app . "::Model::Foo", $extra_config );
97 my $model = $app->model('Foo');
98 isa_ok($model, 'My::Model', 'Correct superclass');
99 ok(!$model->isa('My::Other::Superclass'),
100 'superclass in extra config replaces by default');
102 ok $model->can('_some_method_from_role'), 'Has had role applied';
103 ok !My::Model->can('_some_method_from_role'), 'Role applied at right place';
105 ok $model->can('_some_method_from_other_role'),
106 'Role application merges by default';
108 ok $model->can('my_injected_method'), 'Injected method there as expected';
109 is $model->my_injected_method, 'quuux', 'Injected method returns correct val';
111 ok $model->can('my_other_injected_method'),
112 'Injected methods merged by default';
115 # Specify resolve strategies, totally opposite the defaults:
116 my $app_meta = generate_testapp({
118 roles_resolve_strategy => 'replace',
119 superclasses_resolve_strategy => 'merge',
120 methods_resolve_strategy => 'replace',
122 my $app = $app_meta->name;
123 $app->dynamic_component_method( $app . "::Model::Foo", $extra_config );
124 my $model = $app->model('Foo');
125 isa_ok($model, 'My::Model', 'Correct superclass');
126 isa_ok($model, 'My::Other::Superclass',
127 'superclasses merged');
129 ok $model->can('_some_method_from_role'), 'Has had role applied';
130 ok !My::Model->can('_some_method_from_role'), 'Role applied at right place';
132 ok !$model->can('_some_method_from_other_role'),
133 'Role application replaces when configured';
135 ok $model->can('my_injected_method'), 'Injected method there as expected';
136 is $model->my_injected_method, 'quuux', 'Injected method returns correct val';
138 ok !$model->can('my_other_injected_method'),
139 'Injected methods replaced';
142 # Test that replace with blank config doesnt actually replace the config with blank
143 my $app_meta = generate_testapp({
145 roles_resolve_strategy => 'replace',
146 superclasses_resolve_strategy => 'replace',
147 methods_resolve_strategy => 'replace',
150 my $app = $app_meta->name;
151 $app->dynamic_component_method( $app . "::Model::Foo", {} );
152 my $model = $app->model('Foo');
154 ok $model->can('my_other_injected_method'),
155 'Injected method present';
157 ok($model->isa('My::Other::Superclass'),
158 'superclasses not replaced as not set');
160 ok $model->can('_some_method_from_other_role'),
161 'Role application does not replace as not set';
164 # Test that replace with explicit blanks does so.
165 my $app_meta = generate_testapp({
167 roles_resolve_strategy => 'replace',
168 superclasses_resolve_strategy => 'replace',
169 methods_resolve_strategy => 'replace',
172 my $app = $app_meta->name;
173 $app->dynamic_component_method( $app . "::Model::Foo", { superclasses => [], roles => [], methods => {} } );
174 my $model = $app->model('Foo');
176 ok !$model->can('my_other_injected_method'),
177 'Injected method not present';
179 ok(!$model->isa('My::Other::Superclass'),
180 'superclasses not replaced as set to []');
182 ok !$model->can('_some_method_from_other_role'),
183 'Role application replaces as set to []';