]> git.openstreetmap.org Git - nominatim.git/blobdiff - test/php/Nominatim/DBTest.php
Merge pull request #2562 from lonvia/copyright-headers
[nominatim.git] / test / php / Nominatim / DBTest.php
index e7bd18c7653d04570640a0077ba83d56814e2a35..1c6f763742e4062f77f28d9a09128a7d4a9da231 100644 (file)
@@ -1,12 +1,42 @@
 <?php
+/**
+ * SPDX-License-Identifier: GPL-2.0-only
+ *
+ * This file is part of Nominatim. (https://nominatim.org)
+ *
+ * Copyright (C) 2022 by the Nominatim developer community.
+ * For a full list of authors see the git log.
+ */
 
 namespace Nominatim;
 
-require_once(CONST_BasePath.'/lib/lib.php');
-require_once(CONST_BasePath.'/lib/db.php');
+require_once(CONST_LibDir.'/lib.php');
+require_once(CONST_LibDir.'/DB.php');
 
+// subclassing so we can set the protected connection variable
+class NominatimSubClassedDB extends \Nominatim\DB
+{
+    public function setConnection($oConnection)
+    {
+        $this->connection = $oConnection;
+    }
+}
+
+// phpcs:ignore PSR1.Classes.ClassDeclaration.MultipleClasses
 class DBTest extends \PHPUnit\Framework\TestCase
 {
+    public function testReusingConnection()
+    {
+        $oDB = new NominatimSubClassedDB('');
+        $oDB->setConnection('anything');
+        $this->assertTrue($oDB->connect());
+    }
+
+    public function testCheckConnection()
+    {
+        $oDB = new \Nominatim\DB('');
+        $this->assertFalse($oDB->checkConnection());
+    }
 
     public function testErrorHandling()
     {
@@ -36,9 +66,35 @@ class DBTest extends \PHPUnit\Framework\TestCase
                      throw new \PDOException('ERROR:  syntax error at or near "FROM"');
                  }));
 
-        $oDB = new \Nominatim\DB('');
-        $oDB->connection = $oPDOStub;
-        $oDB->tableExists('abc');
+        $oDB = new NominatimSubClassedDB('');
+        $oDB->setConnection($oPDOStub);
+        $oDB->getOne('SELECT name FROM');
+    }
+
+    public function testGetPostgresVersion()
+    {
+        $oDBStub = $this->getMockBuilder(\Nominatim\DB::class)
+                        ->disableOriginalConstructor()
+                        ->setMethods(array('getOne'))
+                        ->getMock();
+
+        $oDBStub->method('getOne')
+                ->willReturn('100006');
+
+        $this->assertEquals(10, $oDBStub->getPostgresVersion());
+    }
+
+    public function testGetPostgisVersion()
+    {
+        $oDBStub = $this->getMockBuilder(\Nominatim\DB::class)
+                        ->disableOriginalConstructor()
+                        ->setMethods(array('getOne'))
+                        ->getMock();
+
+        $oDBStub->method('getOne')
+                ->willReturn('2.4.4');
+
+        $this->assertEquals(2.4, $oDBStub->getPostgisVersion());
     }
 
     public function testParseDSN()
@@ -65,4 +121,108 @@ class DBTest extends \PHPUnit\Framework\TestCase
             \Nominatim\DB::parseDSN('pgsql:dbname=db1;host=machine1;port=1234;user=john;password=secret')
         );
     }
+
+    public function testGenerateDSN()
+    {
+        $this->assertEquals(
+            'pgsql:',
+            \Nominatim\DB::generateDSN(array())
+        );
+        $this->assertEquals(
+            'pgsql:host=machine1;dbname=db1',
+            \Nominatim\DB::generateDSN(\Nominatim\DB::parseDSN('pgsql:host=machine1;dbname=db1'))
+        );
+    }
+
+    public function testAgainstDatabase()
+    {
+        $unit_test_dsn = getenv('UNIT_TEST_DSN') != false ?
+                            getenv('UNIT_TEST_DSN') :
+                            'pgsql:dbname=nominatim_unit_tests';
+
+        ## Create the database.
+        {
+            $aDSNParsed = \Nominatim\DB::parseDSN($unit_test_dsn);
+            $sDbname = $aDSNParsed['database'];
+            $aDSNParsed['database'] = 'postgres';
+
+            $oDB = new \Nominatim\DB(\Nominatim\DB::generateDSN($aDSNParsed));
+            $oDB->connect();
+            $oDB->exec('DROP DATABASE IF EXISTS ' . $sDbname);
+            $oDB->exec('CREATE DATABASE ' . $sDbname);
+        }
+
+        $oDB = new \Nominatim\DB($unit_test_dsn);
+        $oDB->connect();
+
+        $this->assertTrue(
+            $oDB->checkConnection($sDbname)
+        );
+
+        # Tables, Indices
+        {
+            $oDB->exec('CREATE TABLE table1 (id integer, city varchar, country varchar)');
+
+            $this->assertTrue($oDB->tableExists('table1'));
+            $this->assertFalse($oDB->tableExists('table99'));
+            $this->assertFalse($oDB->tableExists(null));
+        }
+
+        # select queries
+        {
+            $oDB->exec(
+                "INSERT INTO table1 VALUES (1, 'Berlin', 'Germany'), (2, 'Paris', 'France')"
+            );
+
+            $this->assertEquals(
+                array(
+                    array('city' => 'Berlin'),
+                    array('city' => 'Paris')
+                ),
+                $oDB->getAll('SELECT city FROM table1')
+            );
+            $this->assertEquals(
+                array(),
+                $oDB->getAll('SELECT city FROM table1 WHERE id=999')
+            );
+
+
+            $this->assertEquals(
+                array('id' => 1, 'city' => 'Berlin', 'country' => 'Germany'),
+                $oDB->getRow('SELECT * FROM table1 WHERE id=1')
+            );
+            $this->assertEquals(
+                false,
+                $oDB->getRow('SELECT * FROM table1 WHERE id=999')
+            );
+
+
+            $this->assertEquals(
+                array('Berlin', 'Paris'),
+                $oDB->getCol('SELECT city FROM table1')
+            );
+            $this->assertEquals(
+                array(),
+                $oDB->getCol('SELECT city FROM table1 WHERE id=999')
+            );
+
+            $this->assertEquals(
+                'Berlin',
+                $oDB->getOne('SELECT city FROM table1 WHERE id=1')
+            );
+            $this->assertEquals(
+                null,
+                $oDB->getOne('SELECT city FROM table1 WHERE id=999')
+            );
+
+            $this->assertEquals(
+                array('Berlin' => 'Germany', 'Paris' => 'France'),
+                $oDB->getAssoc('SELECT city, country FROM table1')
+            );
+            $this->assertEquals(
+                array(),
+                $oDB->getAssoc('SELECT city, country FROM table1 WHERE id=999')
+            );
+        }
+    }
 }