Real Life Example
file: code/validad/OxController.pm
1: package testlib::OxController;
2: use strict;
3: use warnings;
4: use Moose;
5: use Winxle::BreadBoard;
6: use namespace::autoclean;
7: use OX::Request;
8: use Plack::Response;
9: use Test::Most;
10: use Carp qw(croak);
11: use Data::Dumper;
12:
13: has 'bb' => (
14: is=>'ro',
15: default => sub {
16: Winxle::BreadBoard->setup('Test');
17: }
18: );
19:
20: sub init {
21: my ($self, $name, $args) = @_;
22: $args ||= {};
23:
24: my $controller_class = $self->controller_base."::Controller::".$name;
25: Class::Load::load_class($controller_class);
26:
27: my $bb = $self->bb;
28: my %args = (
29: %$args,
30: # TODO figure out what a given controllor needs
31: # should be possible somehow, as OX isa BB
32: renderer => $bb->resolve(service=>'/Environment/TT'),
33: renderer_nowrap => $bb->resolve(service=>'/Environment/TTnowrap'),
34: json => $bb->resolve(service=>'/Environment/json_coder'),
35: localizer => $bb->resolve(service=>'/Environment/localizer'),
36: config => $bb->resolve(service=>'/Component/Config/config'),
37: dbic => $bb->resolve(service=>'/Component/DB/schema'),
38: );
39:
40: my $controller = $controller_class->new( %args );
41: return $controller;
42: }
43:
44: sub init_dbic_txn {
45: my ($self, $name, $args) = @_;
46: my $c = $self->init($name, $args);
47: my $dbic = $c->dbic;
48: $dbic->txn_begin;
49: return ($c,$dbic);
50: }
51:
52: sub request {
53: my ($self, $args) = @_;
54: $args||={};
55:
56: my $env = $args->{env} || {};
57: if ($args->{params}) { # TODO this is a bit too simplistic
58: $env->{QUERY_STRING} = join('&',map { $_.'='.$args->{params}->{$_} } keys %{$args->{params}})
59: }
60: $env->{'psgix.session'} ||= $args->{session} || {};
61:
62: # TODO add/fake more common request data?
63:
64: return OX::Request->new_from_env($env);
65: }
66:
67: sub call {
68: my ($self, $req, $controller, $action, $args) = @_;
69: $args||=[];
70: my $rv;
71: lives_ok {
72: $rv = $controller->$action($req, @$args);
73: } "called $action on ".ref($controller);
74: return $rv
75: }
76:
77: sub call_plack_res {
78: my $self = shift;
79: my $raw = $self->call( @_ );
80: if ($raw->[0] =~ /^\d+$/) {
81: return Plack::Response->new(@$raw);
82: }
83: else {
84: croak("Return value does not look like PSGI:\n".Dumper($raw));
85: }
86: }
87:
88: __PACKAGE__->meta->make_immutable;
89: 1;