summaryrefslogblamecommitdiffstats
path: root/tests/ConcertTest.php
blob: bde488b9422dcb259c55ae1ed52fadb1b01b7a78 (plain) (tree)
1
2
3
4
5
6
7
8







                                                                               




















                                                              
     




                                                                                        

                               








                                                            
 

















                                                                          

     
                                                                
     
                                                                           

                               






                                             
                                
     


                                                                  
                                                                           

                               
                                           
                        






                                             


                                                                       

                                                     
 


                                                                      





                                                                                      

                                                     
 
                                                                           

                             
                                                       
                                                                                                
     


                                                               


                                                                                      












                                                                                    
                                                                                  






                                                                
                                                                               





                                                               
                                                                                     





                                                                  


                                                              


                                                                                      












                                                                                    
                                                                                        





                                                                        
                                                                                         





                                                                                 
                                                                                       





                                                                   


                                                 


                                                                                      












                                                                                    
                                                     
 
                                                                       
     
 
<?php declare(strict_types=1);
// SPDX-FileCopyrightText: 2021 Andrea Chirulescu <andrea.chirulescu@gmail.com>
// SPDX-FileCopyrightText: 2021 Harald Eilertsen <haraldei@anduin.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

final class ConcertTest extends WP_UnitTestCase
{
    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
    {
        $created_venues = [];
        foreach (self::VENUES as $venue) {
            $created_venues[] = GiglogAdmin_Venue::find_or_create($venue[0], $venue[1]);
        }

        $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");
        }
    }

    /* 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 testCreateExistingConcertShouldFail() : void
    {
        $venue = GiglogAdmin_Venue::find_or_create("a venue", "Somewhere");
        $today = date("Y-m-d");

        $new = GiglogAdmin_Concert::create(
            "a concert",
            $venue->id(),
            $today,
            "https://example.com/tickets/42",
            "https://example.com/events/93");

        $this->assertNull($new);
    }

    public function testCreateExistingConcertVariableCase() : void
    {
        $venue = GiglogAdmin_Venue::find_or_create("a venue", "Somewhere");
        $today = date("Y-m-d");

        $new = GiglogAdmin_Concert::create(
            "a CoNceRt",
            $venue->id(),
            $today,
            "https://example.com/tickets/42",
            "https://example.com/events/93");

        $this->assertNull($new);
    }

    public function testGetConcertByIdReturnsFullConcertObject() : void
    {
        $id = self::$concerts[0]->id();
        $fetched_gig = GiglogAdmin_Concert::get($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
    {
        $id = self::$concerts[0]->id();
        $fetched_gig = GiglogAdmin_Concert::get($id);

        $fetched_gig->set_status( GiglogAdmin_Concert::STATUS_ACCRED_REQ );
        $fetched_gig->save();

        $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::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));
        while ($gig = array_pop($gigs_in_svene)) {
            $this->assertEquals("Svene", $gig->venue()->city());
        }


        $gigs_in_oslo = GiglogAdmin_Concert::find_concerts(["city" => "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(["city" => "Sogndal"]);

        $this->assertEquals(5, count($gigs_in_sogndal));
        while ($gig = array_pop($gigs_in_sogndal)) {
            $this->assertEquals("Sogndal", $gig->venue()->city());
        }
    }

    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));
        while ($gig = array_pop($gigs_at_ss)) {
            $this->assertEquals("Sentrum Scene", $gig->venue()->name());
        }

        $gigs_at_rmh = GiglogAdmin_Concert::find_concerts(["venue_id" => $venue2->id()]);

        $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(["venue_id" => $venue3->id()]);

        $this->assertEquals(5, count($gigs_at_r));
        while ($gig = array_pop($gigs_at_r)) {
            $this->assertEquals("Revolver", $gig->venue()->name());
        }
    }

    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));
    }
}