From f3c31f5e3b6f57bbc887e6b4e435e5202eb41208 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 27 Jun 2021 14:53:57 +0200 Subject: Refactor ConcertTests and some cleanup The commit changes the way we populate the database for the tests by creating more entries up front. This reduces the amount of duplicated code between the tests, but also introduce some challenges. As modifications to the database done in the wpSetUpBeforeClass hook are not cleaned up automatically by the WP_PHPUnit framework, we also have to add a wpTearDownAfterClass hook so anything we set up in this class does not disturb any other tests in other classes. --- tests/ConcertTest.php | 143 ++++++++++++++++++++++++++------------------------ 1 file changed, 75 insertions(+), 68 deletions(-) (limited to 'tests') diff --git a/tests/ConcertTest.php b/tests/ConcertTest.php index 7edda46..bde488b 100644 --- a/tests/ConcertTest.php +++ b/tests/ConcertTest.php @@ -6,37 +6,70 @@ final class ConcertTest extends WP_UnitTestCase { - public function testCreateConcert() : void + const VENUES = [ + [ "a venue", "Somewhere" ], + [ "Svene Bedehus", "Svene" ], + [ "Rockefeller Music Hall", "Oslo" ], + [ "Sentrum Scene", "Oslo" ], + [ "Revolver", "Oslo" ], + [ "Meieriet", "Sogndal" ], + ]; + + const CONCERTS = [ + [ "a concert", 0 ], + ]; + + private static $concerts = []; + + /* This function runs _once_ before all the test cases. + * + * Use it to set up a common state that all test cases can + * use + */ + static function wpSetUpBeforeClass() : void { - $venue = GiglogAdmin_Venue::create("a venue"); + $created_venues = []; + foreach (self::VENUES as $venue) { + $created_venues[] = GiglogAdmin_Venue::find_or_create($venue[0], $venue[1]); + } + $today = date("Y-m-d"); - $concert = GiglogAdmin_Concert::create( - "a concert", - $venue->id(), - $today, - "https://example.com/tickets/42", - "https://example.com/events/93"); + foreach (self::CONCERTS as $concert) { + self::$concerts[] = GiglogAdmin_Concert::create( + $concert[0], + $created_venues[$concert[1]]->id(), + $today, + "https://example.com/tickets/42", + "https://example.com/events/93"); + } + } - $this->assertEquals("a concert", $concert->cname()); - $this->assertEquals($venue->id(), $concert->venue()->id()); - $this->assertEquals($today, $concert->cdate()); - $this->assertEquals("https://example.com/tickets/42", $concert->tickets()); - $this->assertEquals("https://example.com/events/93", $concert->eventlink()); + /* This function runs _once_ after all the test cases in this class. + * + * It is needed to clean up changes in the database that we don't want + * to disturb any other tests. + */ + static function wpTearDownAfterClass() : void + { + global $wpdb; + + $tables = [ + "wpg_concerts", + "wpg_venues", + "wpg_concertlogs", + ]; + + foreach( $tables as $table ) { + $wpdb->query("DELETE FROM {$table}"); + } } - public function testCreateExistingConcert() : void + public function testCreateExistingConcertShouldFail() : void { - $venue = GiglogAdmin_Venue::create("a venue"); + $venue = GiglogAdmin_Venue::find_or_create("a venue", "Somewhere"); $today = date("Y-m-d"); - GiglogAdmin_Concert::create( - "a concert", - $venue->id(), - $today, - "https://example.com/tickets/42", - "https://example.com/events/93"); - $new = GiglogAdmin_Concert::create( "a concert", $venue->id(), @@ -49,18 +82,11 @@ final class ConcertTest extends WP_UnitTestCase public function testCreateExistingConcertVariableCase() : void { - $venue = GiglogAdmin_Venue::create("a venue"); + $venue = GiglogAdmin_Venue::find_or_create("a venue", "Somewhere"); $today = date("Y-m-d"); - GiglogAdmin_Concert::create( - "a concert123", - $venue->id(), - $today, - "https://example.com/tickets/42", - "https://example.com/events/93"); - $new = GiglogAdmin_Concert::create( - "a CoNceRt123", + "a CoNceRt", $venue->id(), $today, "https://example.com/tickets/42", @@ -71,52 +97,33 @@ final class ConcertTest extends WP_UnitTestCase public function testGetConcertByIdReturnsFullConcertObject() : void { - $venue = GiglogAdmin_Venue::create("a venue"); - $today = date("Y-m-d"); + $id = self::$concerts[0]->id(); + $fetched_gig = GiglogAdmin_Concert::get($id); - $gig = GiglogAdmin_Concert::create( - "a concert123", - $venue->id(), - $today, - "https://example.com/tickets/42", - "https://example.com/events/93"); - - $fetched_gig = GiglogAdmin_Concert::get($gig->id()); - - $this->assertEquals($gig->id(), $fetched_gig->id()); - $this->assertEquals($gig->cname(), $fetched_gig->cname()); - $this->assertEquals($venue->id(), $fetched_gig->venue()->id()); + $this->assertEquals($id, $fetched_gig->id()); + $this->assertEquals("a concert", $fetched_gig->cname()); + $this->assertEquals("a venue", $fetched_gig->venue()->name()); $this->assertEquals(GiglogAdmin_Concert::STATUS_NONE, $fetched_gig->status()); $this->assertEquals([], $fetched_gig->roles()); } public function testSetConcertStatus() : void { - $venue = GiglogAdmin_Venue::create("a venue"); - $today = date("Y-m-d"); + $id = self::$concerts[0]->id(); + $fetched_gig = GiglogAdmin_Concert::get($id); - $gig = GiglogAdmin_Concert::create( - "a concert123", - $venue->id(), - $today, - "https://example.com/tickets/42", - "https://example.com/events/93"); - - $fetched_gig = GiglogAdmin_Concert::get($gig->id()); $fetched_gig->set_status( GiglogAdmin_Concert::STATUS_ACCRED_REQ ); - $this->assertEquals( GiglogAdmin_Concert::STATUS_ACCRED_REQ, $fetched_gig->status() ); - $fetched_gig->save(); - $fetched_gig_2 = GiglogAdmin_Concert::get($gig->id()); + $fetched_gig_2 = GiglogAdmin_Concert::get($id); $this->assertEquals( GiglogAdmin_Concert::STATUS_ACCRED_REQ, $fetched_gig_2->status() ); } 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"); + $venue1 = GiglogAdmin_Venue::find_or_create("Svene Bedehus", "Svene"); + $venue2 = GiglogAdmin_Venue::find_or_create("Rockefeller Music Hall", "Oslo"); + $venue3 = GiglogAdmin_Venue::find_or_create("Meieriet", "Sogndal"); for ($i = 0; $i < 4; $i++) { GiglogAdmin_Concert::create('Concert ' . $i, $venue1->id(), '', '', ''); @@ -155,9 +162,9 @@ final class ConcertTest extends WP_UnitTestCase 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"); + $venue1 = GiglogAdmin_Venue::find_or_create("Sentrum Scene", "Oslo"); + $venue2 = GiglogAdmin_Venue::find_or_create("Rockefeller Music Hall", "Oslo"); + $venue3 = GiglogAdmin_Venue::find_or_create("Revolver", "Oslo"); for ($i = 0; $i < 4; $i++) { GiglogAdmin_Concert::create('Concert ' . $i, $venue1->id(), '', '', ''); @@ -195,9 +202,9 @@ final class ConcertTest extends WP_UnitTestCase public function testFetchAllConcerts() : void { - $venue1 = GiglogAdmin_Venue::create("Svene Bedehus", "Svene"); - $venue2 = GiglogAdmin_Venue::create("Rockefeller Music Hall", "Oslo"); - $venue3 = GiglogAdmin_Venue::create("Meieriet", "Sogndal"); + $venue1 = GiglogAdmin_Venue::find_or_create("Svene Bedehus", "Svene"); + $venue2 = GiglogAdmin_Venue::find_or_create("Rockefeller Music Hall", "Oslo"); + $venue3 = GiglogAdmin_Venue::find_or_create("Meieriet", "Sogndal"); for ($i = 0; $i < 4; $i++) { GiglogAdmin_Concert::create('Concert ' . $i, $venue1->id(), '', '', ''); @@ -213,6 +220,6 @@ final class ConcertTest extends WP_UnitTestCase $gigs = GiglogAdmin_Concert::find_concerts(); - $this->assertEquals(11, count($gigs)); + $this->assertEquals(count(self::$concerts) + 11, count($gigs)); } } -- cgit v1.2.3 From 06ec509ce40d3ca04c770e542f57891a3540c7cd Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 27 Jun 2021 15:18:39 +0200 Subject: Refactor ConcertTest some more. Create more of the concerts used by tests into the wpSetupBeforeClass hook. --- tests/ConcertTest.php | 83 ++++++++++++++++----------------------------------- 1 file changed, 25 insertions(+), 58 deletions(-) (limited to 'tests') diff --git a/tests/ConcertTest.php b/tests/ConcertTest.php index bde488b..77b1ed3 100644 --- a/tests/ConcertTest.php +++ b/tests/ConcertTest.php @@ -16,7 +16,12 @@ final class ConcertTest extends WP_UnitTestCase ]; const CONCERTS = [ - [ "a concert", 0 ], + [ "a concert", 0, 1 ], + [ "Concert in Svene #", 1, 4 ], + [ "Concert at Rockefeller #", 2, 2 ], + [ "Concert at Sentrum Scene #", 3, 4 ], + [ "Concert at Revolver #", 4, 5 ], + [ "Concert at Meieriet #", 5, 5 ], ]; private static $concerts = []; @@ -36,12 +41,21 @@ final class ConcertTest extends WP_UnitTestCase $today = date("Y-m-d"); foreach (self::CONCERTS as $concert) { - self::$concerts[] = GiglogAdmin_Concert::create( - $concert[0], - $created_venues[$concert[1]]->id(), - $today, - "https://example.com/tickets/42", - "https://example.com/events/93"); + for ($i = 0; $i < $concert[2]; $i++) { + if ($concert[2] > 1) { + $concert_name = $concert[0] . ($i + 1); + } + else { + $concert_name = $concert[0]; + } + + self::$concerts[] = GiglogAdmin_Concert::create( + $concert_name, + $created_venues[$concert[1]]->id(), + $today, + "https://example.com/tickets/42", + "https://example.com/events/93"); + } } } @@ -121,22 +135,6 @@ final class ConcertTest extends WP_UnitTestCase public function testOnlyFetchConcertsFromGivenCity() : void { - $venue1 = GiglogAdmin_Venue::find_or_create("Svene Bedehus", "Svene"); - $venue2 = GiglogAdmin_Venue::find_or_create("Rockefeller Music Hall", "Oslo"); - $venue3 = GiglogAdmin_Venue::find_or_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([ "city" => "Svene"]); $this->assertEquals(4, count($gigs_in_svene)); @@ -144,10 +142,9 @@ final class ConcertTest extends WP_UnitTestCase $this->assertEquals("Svene", $gig->venue()->city()); } - $gigs_in_oslo = GiglogAdmin_Concert::find_concerts(["city" => "Oslo"]); - $this->assertEquals(2, count($gigs_in_oslo)); + $this->assertEquals(11, count($gigs_in_oslo)); while ($gig = array_pop($gigs_in_oslo)) { $this->assertEquals("Oslo", $gig->venue()->city()); } @@ -163,21 +160,6 @@ final class ConcertTest extends WP_UnitTestCase public function testOnlyFetchConcertsAtGivenVenue() : void { $venue1 = GiglogAdmin_Venue::find_or_create("Sentrum Scene", "Oslo"); - $venue2 = GiglogAdmin_Venue::find_or_create("Rockefeller Music Hall", "Oslo"); - $venue3 = GiglogAdmin_Venue::find_or_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(["venue_id" => $venue1->id()]); $this->assertEquals(4, count($gigs_at_ss)); @@ -185,6 +167,7 @@ final class ConcertTest extends WP_UnitTestCase $this->assertEquals("Sentrum Scene", $gig->venue()->name()); } + $venue2 = GiglogAdmin_Venue::find_or_create("Rockefeller Music Hall", "Oslo"); $gigs_at_rmh = GiglogAdmin_Concert::find_concerts(["venue_id" => $venue2->id()]); $this->assertEquals(2, count($gigs_at_rmh)); @@ -192,6 +175,7 @@ final class ConcertTest extends WP_UnitTestCase $this->assertEquals("Rockefeller Music Hall", $gig->venue()->name()); } + $venue3 = GiglogAdmin_Venue::find_or_create("Revolver", "Oslo"); $gigs_at_r = GiglogAdmin_Concert::find_concerts(["venue_id" => $venue3->id()]); $this->assertEquals(5, count($gigs_at_r)); @@ -202,24 +186,7 @@ final class ConcertTest extends WP_UnitTestCase public function testFetchAllConcerts() : void { - $venue1 = GiglogAdmin_Venue::find_or_create("Svene Bedehus", "Svene"); - $venue2 = GiglogAdmin_Venue::find_or_create("Rockefeller Music Hall", "Oslo"); - $venue3 = GiglogAdmin_Venue::find_or_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 = GiglogAdmin_Concert::find_concerts(); - - $this->assertEquals(count(self::$concerts) + 11, count($gigs)); + $this->assertEquals(count(self::$concerts), count($gigs)); } } -- cgit v1.2.3 From 184f1e3f971423fad351de2b2fbde6f436e6d9ce Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 27 Jun 2021 15:23:35 +0200 Subject: Remove Band class and tests. --- tests/BandTest.php | 69 ------------------------------------------------------ 1 file changed, 69 deletions(-) delete mode 100644 tests/BandTest.php (limited to 'tests') diff --git a/tests/BandTest.php b/tests/BandTest.php deleted file mode 100644 index e4fccbe..0000000 --- a/tests/BandTest.php +++ /dev/null @@ -1,69 +0,0 @@ - -// 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 -{ - /* This function runs _once_ before all the test cases. - * - * Use it to set up a common state that all test cases can - * use - */ - static function wpSetUpBeforeClass() : void - { - GiglogAdmin_Band::create("The Flamboyant Blasphemers"); - } - - public function testCreatingBandWithName() : void - { - $count = count(GiglogAdmin_Band::all_bands()); - - $band = GiglogAdmin_Band::create("Tullerusk"); - - $this->assertEquals("Tullerusk", $band->bandname()); - $this->assertEquals($count + 1, count(GiglogAdmin_Band::all_bands())); - } - - public function testCreateExistingBand() : void - { - $count = count(GiglogAdmin_Band::all_bands()); - - $existing_band = GiglogAdmin_Band::find("The Flamboyant Blasphemers", "NO"); - $new_band = GiglogAdmin_Band::create("The Flamboyant Blasphemers"); - - $this->assertEquals($count, count(GiglogAdmin_Band::all_bands())); - $this->assertEquals($existing_band->id(), $new_band->id()); - $this->assertEquals($existing_band->bandname(), $new_band->bandname()); - } - - public function testCreateBandsWithSameNameInDifferentCountry() : void - { - $existing_band = GiglogAdmin_Band::find("The Flamboyant Blasphemers", "NO"); - $new_band = GiglogAdmin_Band::create("The Flamboyant Blasphemers", "RO"); - - $this->assertNotEquals($existing_band->id(), $new_band->id()); - } - - public function testFindExistingBandReturnsObject() : void - { - $found = GiglogAdmin_Band::find("The Flamboyant Blasphemers", "NO"); - - $this->assertNotNull($found); - $this->assertEquals("The Flamboyant Blasphemers", $found->bandname()); - } - - public function testFindNonExistingBandReturnsNULL() : void - { - // Nice, UK isn't in the country list, so let's move Venom to Azerbajan - // for now... - $found = GiglogAdmin_Band::find("Venom", "AZ"); - - $this->assertNull($found); - } -} -- cgit v1.2.3 From 224f0149ea513146b164736f461e6cbba4b86add Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 27 Jun 2021 13:05:08 +0200 Subject: Begin move roles and status field to concerts table. --- tests/ConcertTest.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'tests') diff --git a/tests/ConcertTest.php b/tests/ConcertTest.php index 77b1ed3..0bf345c 100644 --- a/tests/ConcertTest.php +++ b/tests/ConcertTest.php @@ -133,6 +133,31 @@ final class ConcertTest extends WP_UnitTestCase $this->assertEquals( GiglogAdmin_Concert::STATUS_ACCRED_REQ, $fetched_gig_2->status() ); } + public function testAssignConcertRoles() : void + { + $venue = GiglogAdmin_Venue::create("a venue"); + $today = date("Y-m-d"); + + $gig = GiglogAdmin_Concert::create( + "a concert123", + $venue->id(), + $today, + "https://example.com/tickets/42", + "https://example.com/events/93"); + + $gig->assign_role( GiglogAdmin_Roles::PHOTO1, 'user1' ); + $this->assertEquals( [ GiglogAdmin_Roles::PHOTO1 => 'user1' ], $gig->roles() ); + + $gig->save(); + + var_dump($gig); + + $fetched_gig = GiglogAdmin_Concert::get( $gig->id() ); + global $wpdb; + $wpdb->print_error(); + $this->assertEquals( [ GiglogAdmin_Roles::PHOTO1 => 'user1' ], $fetched_gig->roles() ); + } + public function testOnlyFetchConcertsFromGivenCity() : void { $gigs_in_svene = GiglogAdmin_Concert::find_concerts([ "city" => "Svene"]); -- cgit v1.2.3 From fb8d2649e17c94458db247925b796f5ae2141b0d Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 1 Sep 2021 22:01:42 +0200 Subject: Fix saving and fetching roles from Concerts table. --- tests/ConcertTest.php | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) (limited to 'tests') diff --git a/tests/ConcertTest.php b/tests/ConcertTest.php index 0bf345c..5e6ebde 100644 --- a/tests/ConcertTest.php +++ b/tests/ConcertTest.php @@ -135,27 +135,12 @@ final class ConcertTest extends WP_UnitTestCase public function testAssignConcertRoles() : void { - $venue = GiglogAdmin_Venue::create("a venue"); - $today = date("Y-m-d"); - - $gig = GiglogAdmin_Concert::create( - "a concert123", - $venue->id(), - $today, - "https://example.com/tickets/42", - "https://example.com/events/93"); - - $gig->assign_role( GiglogAdmin_Roles::PHOTO1, 'user1' ); - $this->assertEquals( [ GiglogAdmin_Roles::PHOTO1 => 'user1' ], $gig->roles() ); - + $gig = GiglogAdmin_Concert::get(self::$concerts[0]->id()); + $gig->assign_role( 'photo1' , 'user1' ); $gig->save(); - var_dump($gig); - - $fetched_gig = GiglogAdmin_Concert::get( $gig->id() ); - global $wpdb; - $wpdb->print_error(); - $this->assertEquals( [ GiglogAdmin_Roles::PHOTO1 => 'user1' ], $fetched_gig->roles() ); + $fetched_gig = GiglogAdmin_Concert::get( self::$concerts[0]->id() ); + $this->assertEquals( [ 'photo1' => 'user1' ], $fetched_gig->roles() ); } public function testOnlyFetchConcertsFromGivenCity() : void -- cgit v1.2.3