From a3aafac44a921bee09df73fbfc6511c87a2a9de3 Mon Sep 17 00:00:00 2001 From: "Bradley M. Kuhn" Date: Wed, 9 Dec 2015 19:48:59 -0800 Subject: [PATCH] addAddressType: initial implementation & unit test --- Supporters/lib/Supporters.pm | 35 +++++++++++++++++++++++++++++++++++ Supporters/t/Supporters.t | 17 +++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/Supporters/lib/Supporters.pm b/Supporters/lib/Supporters.pm index 833058f..dfcd7c5 100644 --- a/Supporters/lib/Supporters.pm +++ b/Supporters/lib/Supporters.pm @@ -110,6 +110,41 @@ sub addSupporter ($$) { } ###################################################################### +=begin addAddressType + +Adds an address type, or returns the existing one of that name if it already exists. + +Arguments: + +=over + +=item $addressType + + Scalar string that contains the email address type. die() is called if not defined. + +=back + + Returns id of the address type. + +=cut + +sub addAddressType($$) { + my($self, $type) = @_; + + die "addAddressType: type argument must be defined" if not defined $type; + + my $val = $self->dbh()->selectall_hashref("SELECT id, name FROM address_type WHERE name = '$type'", 'name'); + return $val->{$type}{id} if (defined $val and defined $val->{$type} and defined $val->{$type}{id}); + + my $sth = $self->dbh->prepare("INSERT INTO address_type(name) VALUES(?)"); + + $sth->execute($type); + my $id = $self->dbh->last_insert_id("","","",""); + $sth->finish(); + + return $id; +} + =begin addEmailAddress Arguments: diff --git a/Supporters/t/Supporters.t b/Supporters/t/Supporters.t index 3ddc595..6b9440c 100644 --- a/Supporters/t/Supporters.t +++ b/Supporters/t/Supporters.t @@ -79,6 +79,23 @@ dies_ok { $sp->_addEmailAdress(undef, 'drapper@example.org', 'paypal'); } dies_ok { $sp->_addEmailAdress("String", 'drapper@example.org', 'paypal'); } "_addEmailAdress: dies for non-numeric id"; +=item addAddressType + +=cut + +dies_ok { $sp->addAddressType(undef); } "addAddressType: dies for undef"; + +my $paypalPayerAddressType; + +ok($paypalPayerAddressType = $sp->addAddressType("paypal payer"), "addAddressType: basic add works"); + +my $same; + +ok($same = $sp->addAddressType("paypal payer"), "addAddressType: lookup works"); + +ok($same == $paypalPayerAddressType, "addAddressType: lookup returns same as the basic add"); + + =back =item Internal methods used only by the module itself.