From 7160aacdc83cfcef9ffd795cec7e9397bfbb3698 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 15 Apr 2021 21:50:58 +0200 Subject: Make Band::create return band object, not just id. --- includes/admin/views/giglog_import_gigs.php | 6 +++--- includes/band.php | 21 ++++++++++----------- tests/BandTest.php | 22 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 tests/BandTest.php diff --git a/includes/admin/views/giglog_import_gigs.php b/includes/admin/views/giglog_import_gigs.php index ca1c7b8..17b1cbd 100644 --- a/includes/admin/views/giglog_import_gigs.php +++ b/includes/admin/views/giglog_import_gigs.php @@ -61,7 +61,7 @@ if ( !class_exists( 'GiglogAdmin_ImportGigsPage' ) ) { } $resultArray = explode("\t", $line); - $band = trim($resultArray[0]); + $bandname = trim($resultArray[0]); $venue = trim($resultArray[1]); $condate = date('Y-m-d', strtotime($resultArray[2])); $ticketlink = trim($resultArray[3]); @@ -69,7 +69,7 @@ if ( !class_exists( 'GiglogAdmin_ImportGigsPage' ) ) { //first item in the row should be band $resultArray[0]; second should be venue $resultArray[1]; third should be concert date $resultArray[2]; //fourth item is ticketlink $resultArray[3]; fifth item is eventlink $resultArray[4]; - $newconcert[0] = GiglogAdmin_Band::create($band,''); + $band = GiglogAdmin_Band::create($bandname); if (is_numeric($venue)) $newconcert[1] = $venue; @@ -81,7 +81,7 @@ if ( !class_exists( 'GiglogAdmin_ImportGigsPage' ) ) { //not sure how to check dates, hopefully manual verification of files will take care of it GiglogAdmin_Concert::create( - $newconcert[0], + $band->id(), $newconcert[1], $condate, $ticketlink, diff --git a/includes/band.php b/includes/band.php index ad5ce28..13a816f 100644 --- a/includes/band.php +++ b/includes/band.php @@ -20,10 +20,10 @@ if ( !class_exists('GiglogAdmin_Band') ) { $this->country = isset($attrs->country) ? $attrs->country : 'NO'; } - static function create($bandname, $country) + static function create($bandname, $country = 'NO') { global $wpdb; - if (empty($country)) $country = 'NO'; + $bandsql = 'SELECT id FROM wpg_bands WHERE upper(wpgband_name)="' . $bandname . '" and wpgband_country = "'.$country.'"'; $results = $wpdb->get_results($bandsql); $attrs = new stdClass(); @@ -37,20 +37,19 @@ if ( !class_exists('GiglogAdmin_Band') ) { } else { + $attrs->id = ''; - $attrs->id = ''; - - $bid = new GiglogAdmin_Band($attrs); - $bid->save(); + $bid = new GiglogAdmin_Band($attrs); + $bid->save(); - error_log( 'NEW BAND ADDED: ' - . ' ID: ' . $bid -> id() - . ' BAND NAME ' . $bandname - . ', COUNTRY ' . $country); + error_log( 'NEW BAND ADDED: ' + . ' ID: ' . $bid -> id() + . ' BAND NAME ' . $bandname + . ', COUNTRY ' . $country); } - return ($bid->id()); + return $bid; } diff --git a/tests/BandTest.php b/tests/BandTest.php new file mode 100644 index 0000000..764abe3 --- /dev/null +++ b/tests/BandTest.php @@ -0,0 +1,22 @@ + +// SPDX-FileCopyrightText: 2021 Harald Eilertsen +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +declare(strict_types=1); + +require __DIR__ . '/../includes/band.php'; + +final class BandTest extends WP_UnitTestCase +{ + public function testCreatingBandWithName(): void + { + $count = count(GiglogAdmin_Band::all_bands()); + + $band = GiglogAdmin_Band::create("The Flamboyant Blasphemers"); + + $this->assertEquals("The Flamboyant Blasphemers", $band->bandname()); + $this->assertEquals($count + 1, count(GiglogAdmin_Band::all_bands())); + } +} -- cgit v1.2.3 From a851dd35b8ead5e4688062ff0c6e94d83133b006 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 15 Apr 2021 22:22:40 +0200 Subject: Refactor Band::create into Band::find and ::create Not sure if it's a good idea to have `create` return an existing band. Will have to look at callsites to see if it should be renamed back or if the callsite should be changed. --- includes/band.php | 49 ++++++++++++++++++++++++++++--------------------- 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()); + } } -- cgit v1.2.3