summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2021-04-10 13:36:28 +0200
committerHarald Eilertsen <haraldei@anduin.net>2021-04-10 14:19:50 +0200
commit029f4d7a5b9aff1f7b35f060d172611ef81a2943 (patch)
tree9d16309995e120cff7b9488eb06ef45d7aebf108 /tests
parent410135aca1c07409f5909b44dc144bb2c3644645 (diff)
downloadgigologadmin-029f4d7a5b9aff1f7b35f060d172611ef81a2943.tar.gz
gigologadmin-029f4d7a5b9aff1f7b35f060d172611ef81a2943.tar.bz2
gigologadmin-029f4d7a5b9aff1f7b35f060d172611ef81a2943.zip
Change venues into proper objects.
This means most static functions now either return a venue object, or an array of venue objects. The exception is the `all_cities` method, which still return an array of cities as strings. The constructor has been made private, as it should not be used directly from anywhere but the static methods on the Venue class.
Diffstat (limited to 'tests')
-rw-r--r--tests/VenueTest.php92
-rw-r--r--tests/stubs/wpdb_stub.php9
2 files changed, 101 insertions, 0 deletions
diff --git a/tests/VenueTest.php b/tests/VenueTest.php
new file mode 100644
index 0000000..814bfe8
--- /dev/null
+++ b/tests/VenueTest.php
@@ -0,0 +1,92 @@
+<?php
+declare(strict_types=1);
+
+use PHPUnit\Framework\TestCase;
+
+require 'tests/stubs/wpdb_stub.php';
+require 'includes/venue.php';
+
+final class VenueTest extends TestCase
+{
+ public function testCreatingVenueWithName(): void
+ {
+ $venue = GiglogAdmin_Venue::create("Svene Samfunns- og Bedehus");
+ $this->assertEquals("Svene Samfunns- og Bedehus", $venue->name());
+ $this->assertEquals(1, $venue->id());
+ }
+
+ public function testFindOrCreateNonExistingVenue() : void
+ {
+ $venue = GiglogAdmin_Venue::find_or_create("Svene Samfunns- og Bedehus");
+ $this->assertEquals("Svene Samfunns- og Bedehus", $venue->name());
+ $this->assertEquals(1, $venue->id());
+ }
+
+ public function testFindOrCreateExistingVenue() : void
+ {
+ global $wpdb;
+
+ $v = new stdClass();
+ $v->id = 42;
+ $v->wpgvenue_name = 'Slarkhaillen';
+ $v->wpgvenue_city = 'Ofoten';
+ $v->wpgvenue_address = 'Baillsvingen 4';
+ $v->wpgvenue_webpage = 'https://slarkhaillen.no';
+
+ $wpdb = $this->createStub(wpdb::class);
+ $wpdb->method('get_results')->willReturn(array($v));
+ $venue = GiglogAdmin_Venue::find_or_create("Slarkhaillen");
+
+ $this->assertEquals(42, $venue->id());
+ $this->assertEquals($v->wpgvenue_name, $venue->name());
+ }
+
+ public function testFindAllVenuesInCity() : void
+ {
+ global $wpdb;
+
+ $results = array();
+ for ($i = 0; $i < 3; $i++) {
+ $results[$i] = new stdClass();
+ $results[$i]->id = 42 + $i;
+ $results[$i]->wpgvenue_name = "Venue #" . $i;
+ $results[$i]->wpgvenue_city = "Osaka";
+ }
+
+ $wpdb = $this->createStub(wpdb::class);
+ $wpdb->method('prepare')->willReturn("prepared");
+ $wpdb->method('get_results')->willReturn($results);
+
+ $venues = GiglogAdmin_Venue::venues_in_city("Osaka");
+
+ for ($i = 0; $i < 3; $i++) {
+ $this->assertEquals("Osaka", $venues[$i]->city());
+ $this->assertEquals("Venue #" . $i, $venues[$i]->name());
+ $this->assertEquals(42 + $i, $venues[$i]->id());
+ }
+ }
+
+ public function testFindAllVenues() : void
+ {
+ global $wpdb;
+
+ $results = array();
+ for ($i = 0; $i < 3; $i++) {
+ $results[$i] = new stdClass();
+ $results[$i]->id = 42 + $i;
+ $results[$i]->wpgvenue_name = "Venue #" . $i;
+ $results[$i]->wpgvenue_city = "City #" . $i;
+ }
+
+ $wpdb = $this->createStub(wpdb::class);
+ $wpdb->method('get_results')->willReturn($results);
+
+ $venues = GiglogAdmin_Venue::all_venues();
+
+ for ($i = 0; $i < 3; $i++) {
+ $this->assertEquals("City #" . $i, $venues[$i]->city());
+ $this->assertEquals("Venue #" . $i, $venues[$i]->name());
+ $this->assertEquals(42 + $i, $venues[$i]->id());
+ }
+ }
+}
diff --git a/tests/stubs/wpdb_stub.php b/tests/stubs/wpdb_stub.php
new file mode 100644
index 0000000..72312bc
--- /dev/null
+++ b/tests/stubs/wpdb_stub.php
@@ -0,0 +1,9 @@
+<?php
+class wpdb {
+ public $insert_id = NULL;
+ public function insert(string $table, array $data) { $this->insert_id = 1; }
+ public function prepare(string $query, mixed $args) { return "prepared"; }
+ public function get_results(string $query) { return NULL; }
+}
+
+$wpdb = new wpdb();