PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection::getRowCount PHP Method

getRowCount() public method

Returns the number of rows in the given table. You can specify an optional where clause to return a subset of the table.
public getRowCount ( string $tableName, string $whereClause = NULL )
$tableName string
$whereClause string
    public function getRowCount($tableName, $whereClause = NULL)
    {
        $query = "SELECT COUNT(*) FROM " . $this->quoteSchemaObject($tableName);
        if (isset($whereClause)) {
            $query .= " WHERE {$whereClause}";
        }
        return (int) $this->connection->query($query)->fetchColumn();
    }

Usage Example

 public function testRowCountForTableWithTwoRowsReturnsTwo()
 {
     $this->db->exec('INSERT INTO test (field1) VALUES (\'foobar\')');
     $this->db->exec('INSERT INTO test (field1) VALUES (\'foobarbaz\')');
     $conn = new PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection($this->db);
     $this->assertEquals(2, $conn->getRowCount('test'));
 }