Skip to content

Commit

Permalink
Merge pull request #24 from elecena/test-with-db
Browse files Browse the repository at this point in the history
CI - add a test MySQL instance
  • Loading branch information
macbre authored Mar 24, 2021
2 parents 0ecbc27 + ce183c5 commit 36e413c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 14 additions & 29 deletions tests/DatabaseMysqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

use Nano\NanoBaseTest;

/**
* Requires MySQL server running on 0.0.0.0:3306
*/
class DatabaseMysqlTest extends NanoBaseTest
{
/**
Expand All @@ -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()
Expand All @@ -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']);
}
}

0 comments on commit 36e413c

Please sign in to comment.