diff options
-rw-r--r-- | includes/band.php | 49 | ||||
-rw-r--r-- | tests/BandTest.php | 10 |
2 files changed, 37 insertions, 22 deletions
diff --git a/includes/band.php b/includes/band.php index 13a816f..47b663c 100644 --- a/includes/band.php +++ b/includes/band.php @@ -22,34 +22,41 @@ if ( !class_exists('GiglogAdmin_Band') ) { static function create($bandname, $country = 'NO') { - global $wpdb; + $band = GiglogAdmin_Band::find($bandname, $country); - $bandsql = 'SELECT id FROM wpg_bands WHERE upper(wpgband_name)="' . $bandname . '" and wpgband_country = "'.$country.'"'; - $results = $wpdb->get_results($bandsql); - $attrs = new stdClass(); - $attrs->bandname = $bandname; - $attrs->country = $country; + if ( ! $band ) { + $band = new GiglogAdmin_Band((object) [ + 'bandname' => $bandname, + 'country' => $country, + ]); - if ($results) - { - $attrs->id = $results[0]->id; - $bid = new GiglogAdmin_Band($attrs); + $band->save(); + + error_log( 'NEW BAND ADDED: ' + . ' ID: ' . $band->id() + . ' BAND NAME ' . $band->bandname() + . ', COUNTRY ' . $band->country()); } - else - { - $attrs->id = ''; - $bid = new GiglogAdmin_Band($attrs); - $bid->save(); + return $band; + } + static function find($name, $country) + { + global $wpdb; - error_log( 'NEW BAND ADDED: ' - . ' ID: ' . $bid -> id() - . ' BAND NAME ' . $bandname - . ', COUNTRY ' . $country); - } + $q = 'SELECT * FROM wpg_bands ' + . 'WHERE upper(wpgband_name)="' . $name + . '" and wpgband_country = "' . $country.'"'; - return $bid; + $results = $wpdb->get_results($q); + + if ($results) { + return new GiglogAdmin_Band($results[0]); + } + else { + return NULL; + } } diff --git a/tests/BandTest.php b/tests/BandTest.php index 764abe3..d3feb51 100644 --- a/tests/BandTest.php +++ b/tests/BandTest.php @@ -10,7 +10,7 @@ require __DIR__ . '/../includes/band.php'; final class BandTest extends WP_UnitTestCase { - public function testCreatingBandWithName(): void + public function testCreatingBandWithName() : void { $count = count(GiglogAdmin_Band::all_bands()); @@ -19,4 +19,12 @@ final class BandTest extends WP_UnitTestCase $this->assertEquals("The Flamboyant Blasphemers", $band->bandname()); $this->assertEquals($count + 1, count(GiglogAdmin_Band::all_bands())); } + + public function testCreateExistingBand() : void + { + $band1 = GiglogAdmin_Band::create("The Flamboyant Blasphemers"); + $band2 = GiglogAdmin_Band::create("The Flamboyant Blasphemers"); + + $this->assertEquals($band1->id(), $band2->id()); + } } |