Esempio n. 1
0
<?php

require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc';
$db = MySQLPDOTest::factory();
MySQLPDOTest::createTestTable($db, MySQLPDOTest::detect_transactional_mysql_engine($db));
try {
    if (true !== ($tmp = $db->beginTransaction())) {
        printf("[001] Expecting true, got %s/%s\n", gettype($tmp), $tmp);
    }
    // DDL will issue an implicit commit
    $db->exec(sprintf('DROP TABLE IF EXISTS test_commit'));
    $db->exec(sprintf('CREATE TABLE test_commit(id INT) ENGINE=%s', MySQLPDOTest::detect_transactional_mysql_engine($db)));
    if (true !== ($tmp = $db->commit())) {
        printf("[002] No commit allowed? [%s] %s\n", $db->errorCode(), implode(' ', $db->errorInfo()));
    }
    // pdo_transaction_transitions should check this as well...
    // ... just to be sure the most basic stuff really works we check it again...
    if (1 !== ($tmp = $db->getAttribute(PDO::ATTR_AUTOCOMMIT))) {
        printf("[003] According to the manual we should be back to autocommit mode, got %s/%s\n", gettype($tmp), var_export($tmp, true));
    }
    if (true !== ($tmp = $db->beginTransaction())) {
        printf("[004] Expecting true, got %s/%s\n", gettype($tmp), $tmp);
    }
    $db->exec("INSERT INTO test(id, label) VALUES (100, 'z')");
    if (true !== ($tmp = $db->commit())) {
        printf("[005] No commit allowed? [%s] %s\n", $db->errorCode(), implode(' ', $db->errorInfo()));
    }
    // a weak test without unicode etc. - lets leave that to dedicated tests
    $stmt = $db->query('SELECT id, label FROM test WHERE id = 100');
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    if (!isset($rows[0]['label']) || $rows[0]['label'] != 'z') {
Esempio n. 2
0
<?php

require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc';
$db = MySQLPDOTest::factory();
MySQLPDOTest::createTestTable($db, MySQLPDOTest::detect_transactional_mysql_engine($db));
function find_invalid_int($valid_options)
{
    do {
        $invalid = mt_rand(-10000, 10000);
    } while (in_array($invalid, $valid_options));
    return $invalid;
}
function set_and_get($offset, $db, $attribute, $value)
{
    $value_type = gettype($value);
    try {
        if (!$db->setAttribute($attribute, $value)) {
            printf("[%03d] Cannot set attribute '%s' to value '%s'\n", $offset, $attribute, var_export($tmp, true));
            return false;
        }
        if (gettype($value) != $value_type) {
            printf("[%03d] Call to PDO::setAttribute(int attribute, mixed value) has changed the type of value from %s to %s, test will not work properly\n", $offset, $value_type, gettype($value));
            return false;
        }
        $tmp = $db->getAttribute($attribute);
        if ($tmp !== $value) {
            printf("[%03d] Attribute '%s' was set to '%s'/%s but getAttribute() reports '%s'/%s\n", $offset, $attribute, var_export($value, true), gettype($value), var_export($tmp, true), gettype($tmp));
            return false;
        }
    } catch (PDOException $e) {
        printf("[%03d] %s, [%s] %s\n", $offset, $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
Esempio n. 3
0
<?php

if (getenv('REDIR_TEST_DIR') === false) {
    putenv('REDIR_TEST_DIR=' . dirname(__FILE__) . '/../../pdo/tests/');
}
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
$db = PDOTest::factory();
if ($db->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') {
    require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc';
    $suf = ' ENGINE=' . MySQLPDOTest::detect_transactional_mysql_engine($db);
} else {
    $suf = '';
}
$db->exec('CREATE TABLE test(id INT NOT NULL PRIMARY KEY, val VARCHAR(10))' . $suf);
$db->exec("INSERT INTO test VALUES(1, 'A')");
$db->exec("INSERT INTO test VALUES(2, 'B')");
$db->exec("INSERT INTO test VALUES(3, 'C')");
$delete = $db->prepare('DELETE FROM test');
function countRows($action)
{
    global $db;
    $select = $db->prepare('SELECT COUNT(*) FROM test');
    $select->execute();
    $res = $select->fetchColumn();
    return "Counted {$res} rows after {$action}.\n";
}
echo countRows('insert');
$db->beginTransaction();
$delete->execute();
echo countRows('delete');
$db->rollBack();