From e570c572b8ccc377a74ab168f2698ac706400141 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 9 May 2021 21:38:41 +0200 Subject: Method to find concerts for a given city. --- includes/concert.php | 15 +++++++++++++++ tests/ConcertTest.php | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) 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()); + } + } } -- cgit v1.2.3 From 61c48191a8e9ab757be4bf845072f4cfdbf9075f Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 9 May 2021 22:12:21 +0200 Subject: Method to find concerts at given venue --- includes/concert.php | 14 ++++++++++++++ tests/ConcertTest.php | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/includes/concert.php b/includes/concert.php index c773c12..050c924 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -185,6 +185,20 @@ if ( !class_exists('GiglogAdmin_Concert') ) { return array_map(function($c) { return new GiglogAdmin_Concert($c); }, $results); } + public static function find_concerts_at(GiglogAdmin_Venue $venue) : 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_concerts.venue = ' . $wpdb->prepare('%d', $venue->id()); + + $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 dc82839..96fc72b 100644 --- a/tests/ConcertTest.php +++ b/tests/ConcertTest.php @@ -128,4 +128,44 @@ final class ConcertTest extends WP_UnitTestCase $this->assertEquals("Sogndal", $gig->venue()->city()); } } + + public function testOnlyFetchConcertsAtGivenVenue() : void + { + $venue1 = GiglogAdmin_Venue::create("Sentrum Scene", "Oslo"); + $venue2 = GiglogAdmin_Venue::create("Rockefeller Music Hall", "Oslo"); + $venue3 = GiglogAdmin_Venue::create("Revolver", "Oslo"); + + 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_at_ss = GiglogAdmin_Concert::find_concerts_at($venue1); + + $this->assertEquals(4, count($gigs_at_ss)); + while ($gig = array_pop($gigs_at_ss)) { + $this->assertEquals("Sentrum Scene", $gig->venue()->name()); + } + + $gigs_at_rmh = GiglogAdmin_Concert::find_concerts_at($venue2); + + $this->assertEquals(2, count($gigs_at_rmh)); + while ($gig = array_pop($gigs_at_rmh)) { + $this->assertEquals("Rockefeller Music Hall", $gig->venue()->name()); + } + + $gigs_at_r = GiglogAdmin_Concert::find_concerts_at($venue3); + + $this->assertEquals(5, count($gigs_at_r)); + while ($gig = array_pop($gigs_at_r)) { + $this->assertEquals("Revolver", $gig->venue()->name()); + } + } } -- cgit v1.2.3