Skip to content

Commit

Permalink
#10 Added tests for new Statement functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sbuberl committed May 26, 2019
1 parent a87f8cb commit 98d970e
Showing 1 changed file with 74 additions and 7 deletions.
81 changes: 74 additions & 7 deletions tests/StatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class StatementTest extends BaseTest
private $fsql;

private static $columns = [
'personId' => ['type' => 'i', 'auto' => 0, 'default' => 0, 'key' => 'n', 'null' => 1, 'restraint' => []],
'personId' => ['type' => 'i', 'auto' => 1, 'default' => 0, 'key' => 'p', 'null' => 0, 'restraint' => [3, 0, 1, 1, 1, 10000, 0]],
'firstName' => ['type' => 's', 'auto' => 0, 'default' => '', 'key' => 'n', 'null' => 1, 'restraint' => []],
'lastName' => ['type' => 's', 'auto' => 0, 'default' => '', 'key' => 'n', 'null' => 1, 'restraint' => []],
'city' => ['type' => 's', 'auto' => 0, 'default' => '', 'key' => 'n', 'null' => 1, 'restraint' => []],
Expand Down Expand Up @@ -102,12 +102,6 @@ public function testExecuteDMLError()
public function testExecuteDML()
{
$table = CachedTable::create($this->fsql->current_schema(), 'customers', self::$columns);
$cursor = $table->getWriteCursor();
foreach (self::$entries as $entry) {
$cursor->appendRow($entry);
}
$table->commit();

$statement = new Statement($this->fsql);
$statement->prepare("INSERT INTO customers (personId, firstName, lastName, city, zip) VALUES (1, 'John', 'Smith', 'Los Angelos', 75677)");
$passed = $statement->execute();
Expand Down Expand Up @@ -514,6 +508,79 @@ public function testNumRows()
$this->assertEquals(12, $result);
}

public function testParamCountNoParams()
{
$table = CachedTable::create($this->fsql->current_schema(), 'customers', self::$columns);
$cursor = $table->getWriteCursor();
foreach (self::$entries as $entry) {
$cursor->appendRow($entry);
}
$table->commit();

$statement = new Statement($this->fsql);
$statement->prepare("SELECT firstName, lastName, city FROM customers");
$result = $statement->param_count();
$this->assertEquals(0, $result);
}

public function testParamCount()
{
$table = CachedTable::create($this->fsql->current_schema(), 'customers', self::$columns);
$cursor = $table->getWriteCursor();
foreach (self::$entries as $entry) {
$cursor->appendRow($entry);
}
$table->commit();

$statement = new Statement($this->fsql);
$statement->prepare("SELECT firstName, lastName, city FROM customers WHERE personId = ? OR lastName = ? OR zip = ?");
$result = $statement->param_count();
$this->assertEquals(3, $result);
}

public function testAffectedRows()
{
$table = CachedTable::create($this->fsql->current_schema(), 'customers', self::$columns);
$statement = new Statement($this->fsql);
$statement->prepare("INSERT INTO customers (personId, firstName, lastName, city, zip) VALUES (1, 'John', 'Smith', 'Los Angelos', 75677)");
$passed = $statement->execute();
$affectedRows = $statement->affected_rows();
$this->assertTrue($affectedRows === 1);
}

public function testInsertId()
{
$table = CachedTable::create($this->fsql->current_schema(), 'customers', self::$columns);
$statement = new Statement($this->fsql);
$statement->prepare("INSERT INTO customers (firstName, lastName, city, zip) VALUES ('John', 'Smith', 'Los Angelos', 75677)");
$passed = $statement->execute();
$insertId = $statement->insert_id();
$this->assertTrue($insertId === 3);
}

public function testFieldCountNoPrepare()
{
$table = CachedTable::create($this->fsql->current_schema(), 'customers', self::$columns);
$statement = new Statement($this->fsql);
$passed = $statement->field_count();
$this->assertTrue($passed === false);
$this->assertEquals($statement->error(), "Unable to perform a field_count without a prepare");
}

public function testFieldCount()
{
$table = CachedTable::create($this->fsql->current_schema(), 'customers', self::$columns);
$statement = new Statement($this->fsql);
$statement->prepare("SELECT firstName, lastName, city, zip FROM customers WHERE personId = ? OR lastName = ? OR zip = ?");
$id = 5;
$last = 'king';
$zip = 99999;
$statement->bind_param('isd', $id, $last, $zip);
$results = $statement->execute();
$fieldCount = $statement->field_count();
$this->assertTrue($fieldCount === 4);
}

// public function testEnvPrepare()
// {
// $dbName = 'db1';
Expand Down

0 comments on commit 98d970e

Please sign in to comment.