diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index e8611cdc..fe24c855 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -20,6 +20,17 @@ jobs: # - "6.0" # - "6.2" + services: + mysql: + image: mysql:5.7.33 + env: + # root password is empty + MYSQL_ALLOW_EMPTY_PASSWORD: yes + MYSQL_DATABASE: test + ports: + - "3306:3306" + options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 + steps: - name: Checkout uses: actions/checkout@v2 diff --git a/tests/DatabaseMysqlTest.php b/tests/DatabaseMysqlTest.php index ea633e34..83d8399d 100644 --- a/tests/DatabaseMysqlTest.php +++ b/tests/DatabaseMysqlTest.php @@ -2,6 +2,9 @@ use Nano\NanoBaseTest; +/** + * Requires MySQL server running on 0.0.0.0:3306 + */ class DatabaseMysqlTest extends NanoBaseTest { /** @@ -12,7 +15,7 @@ class DatabaseMysqlTest extends NanoBaseTest protected function setUp(): void { parent::setUp(); - $this->database = Database::connect($this->app, ['driver' => 'mysql', 'host' => 'localhost', 'user' => 'root', 'pass' => '', 'database' => 'test']); + $this->database = Database::connect($this->app, ['driver' => 'mysql', 'host' => '0.0.0.0', 'user' => 'root', 'pass' => '']); } public function testLazyConnect() @@ -29,43 +32,25 @@ public function testLazyConnect() $this->assertTrue($this->database->isConnected(), 'We should be connected now'); } - // requires server running on localhost:3306 public function testMySqlDatabase() { - try { - $this->database->query('SELECT 1'); - } catch (DatabaseException $e) { - $this->markTestSkipped('Requires server running on localhost:3306 - ' . $e->getMessage()); - } - // test performance data $performanceData = $this->database->getPerformanceData(); $this->assertEquals(0, $performanceData['queries']); - $this->assertEquals(0, $performanceData['time']); - - $res = $this->database->select('test', '*'); - foreach ($res as $i => $row) { - #var_dump($row); - } - $res->free(); - $res = $this->database->select('test', '*'); - while ($row = $res->fetchRow()) { - #var_dump($row); + try { + $this->database->query('SELECT 1'); + } catch (DatabaseException $e) { + $this->markTestSkipped('Requires server running on localhost:3306 - ' . $e->getMessage()); } - $res->free(); - - $row = $this->database->selectRow('test', '*', ['id' => 2]); - #var_dump($row); - - $row = $this->database->selectField('test', 'count(*)'); - #var_dump($row); - $res = $this->database->query('SELECT VERSION()'); - #var_dump($res->fetchField()); + // DUAL is purely for the convenience of people who require that all SELECT statements + // should have FROM and possibly other clauses. MySQL may ignore the clauses. + // MySQL does not require FROM DUAL if no tables are referenced. + $this->assertEquals('1', $this->database->selectField('dual', '1')); + $this->assertEquals(1, $this->database->select('dual', '1')->count()); $performanceData = $this->database->getPerformanceData(); - $this->assertEquals(5, $performanceData['queries']); - $this->assertTrue($performanceData['time'] > 0); + $this->assertEquals(3, $performanceData['queries']); } }