Elive

 view release on metacpan or  search on metacpan

t/Elive/MockConnection.pm  view on Meta::CPAN


use YAML::Syck;

use t::Elive::MockSOM;

__PACKAGE__->mk_accessors( qw{mockdb server_details_id} );

sub connect {
    goto \&_connect;
}

sub _connect {
    my ($class, $url,  $user, $pass, %opt) = @_;

    my $self = {};
    bless $self, $class;

    $url ||= 'http://elive_mock_connection';
    $url =~ s{/$}{};                    # lose trailing '/'
    $url =~ s{/webservice\.event$}{};   # lose endpoint
    $url =~ s{/v[1-9]$}{};                  # lose adapter path

    if ($url =~ s{^(\w+)://(.*)\@}{$1://}) {  # lose/capture credentials

	my ($_user, $_pass) = split(':', $2, 2);

	$user ||= $_user;
	$pass ||= $_pass if $_pass;
    }

    $self->url($url);

    $self->user($user);
    $self->user('test_user') unless $self->user;

    $self->pass($pass);
    $self->pass('test_pass') unless $self->pass;

    $self->debug($opt{debug})
	if defined $opt{debug};

    $self->mockdb({});

    Elive::Entity::User->insert(
	{loginName => $self->user,
	 loginPassword => $self->pass,
	 role => {roleId => 0},
	},
	connection => $self,
	);

    #
    # Pretend that we can insert a server details record. Just for the
    # purposes of our mockup
    #
    local($self->known_commands->{createServerDetails}) = 'c';

    my $server_details = Elive::Entity::ServerDetails->insert(
	{
	 version => '9.6.0',
	 alive => 1,
	},
	connection => $self,
	);

    $self->server_details_id( $server_details->serverDetailsId );

    return $self;
}

sub call {
    my $self = shift;
    my $cmd = shift;

    my %params = @_;

    my $known_commands = $self->known_commands;
    $self->check_command($cmd);

    my $entities = Elive::Entity->entities;
    my %collections =
	(map {@$_}
	 grep {$_[0]}
	 map {[($_->collection_name||'') => $_]}
	 (values %$entities)
	);
    #
    # Determine an operation for the command
    #
    my $crud = $known_commands->{$cmd};
    die "Uknown command $cmd in mock connection"
	unless $crud;

    my $som = bless {}, 't::Elive::MockSOM';

    my ($op, $entity_name) = ($cmd =~ m{^(add|get|create|check|delete|update|list)(.*)$});

    $entity_name = 'User' if $cmd eq 'changePassword';

    if ($entity_name) {

	$entity_name = lcfirst($entity_name);

	if (my $entity_class = ($entities->{$entity_name} || $collections{$entity_name})) {

	    #
	    # dereference aliases
	    #
	    my %aliases = $entity_class->_to_aliases;
	    for (grep {exists $params{$_}} (keys %aliases)) {
		my $att = $aliases{$_};
		$params{$att} = delete $params{$_};
	    }

	    my @primary_key = @{ $entity_class->_primary_key };

	    $params{$primary_key[0]} ||= $self->server_details_id
		if $entity_name eq 'serverDetails';

	    warn YAML::Syck::Dump {cmd => $cmd, params => \%params}
	    if ($self->debug||0) >= 3;



( run in 3.119 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )