diff options
-rw-r--r-- | includes/concert.php | 15 | ||||
-rw-r--r-- | tests/ConcertTest.php | 41 |
2 files changed, 56 insertions, 0 deletions
diff --git a/includes/concert.php b/includes/concert.php index e3c32e8..c773c12 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -170,6 +170,21 @@ if ( !class_exists('GiglogAdmin_Concert') ) { return $wpdb->get_results($sql); } + + public static function find_concerts_in(string $city) : array + { + global $wpdb; + + $query = 'SELECT wpg_concerts.*, wpg_venues.wpgvenue_name, wpg_venues.wpgvenue_city ' + . 'FROM wpg_concerts ' + . 'INNER JOIN wpg_venues ON wpg_concerts.venue = wpg_venues.id ' + . 'WHERE wpg_venues.wpgvenue_city = ' . $wpdb->prepare('%s', $city); + + $results = $wpdb->get_results($query); + + return array_map(function($c) { return new GiglogAdmin_Concert($c); }, $results); + } + public function save(): void { global $wpdb; diff --git a/tests/ConcertTest.php b/tests/ConcertTest.php index 0517ad8..dc82839 100644 --- a/tests/ConcertTest.php +++ b/tests/ConcertTest.php @@ -87,4 +87,45 @@ final class ConcertTest extends WP_UnitTestCase $this->assertEquals($gig->cname(), $fetched_gig->cname()); $this->assertEquals($venue->id(), $fetched_gig->venue()->id()); } + + public function testOnlyFetchConcertsFromGivenCity() : void + { + $venue1 = GiglogAdmin_Venue::create("Svene Bedehus", "Svene"); + $venue2 = GiglogAdmin_Venue::create("Rockefeller Music Hall", "Oslo"); + $venue3 = GiglogAdmin_Venue::create("Meieriet", "Sogndal"); + + for ($i = 0; $i < 4; $i++) { + GiglogAdmin_Concert::create('Concert ' . $i, $venue1->id(), '', '', ''); + } + + for ($i = 4; $i < 6; $i++) { + GiglogAdmin_Concert::create('Concert ' . $i, $venue2->id(), '', '', ''); + } + + for ($i = 6; $i < 11; $i++) { + GiglogAdmin_Concert::create('Concert ' . $i, $venue3->id(), '', '', ''); + } + + $gigs_in_svene = GiglogAdmin_Concert::find_concerts_in("Svene"); + + $this->assertEquals(4, count($gigs_in_svene)); + while ($gig = array_pop($gigs_in_svene)) { + $this->assertEquals("Svene", $gig->venue()->city()); + } + + + $gigs_in_oslo = GiglogAdmin_Concert::find_concerts_in("Oslo"); + + $this->assertEquals(2, count($gigs_in_oslo)); + while ($gig = array_pop($gigs_in_oslo)) { + $this->assertEquals("Oslo", $gig->venue()->city()); + } + + $gigs_in_sogndal = GiglogAdmin_Concert::find_concerts_in("Sogndal"); + + $this->assertEquals(5, count($gigs_in_sogndal)); + while ($gig = array_pop($gigs_in_sogndal)) { + $this->assertEquals("Sogndal", $gig->venue()->city()); + } + } } |