|
|
@ -6,16 +6,14 @@ use Engelsystem\Database\Db;
|
|
|
|
use Illuminate\Database\Capsule\Manager as CapsuleManager;
|
|
|
|
use Illuminate\Database\Capsule\Manager as CapsuleManager;
|
|
|
|
use Illuminate\Database\Connection as DatabaseConnection;
|
|
|
|
use Illuminate\Database\Connection as DatabaseConnection;
|
|
|
|
use PDO;
|
|
|
|
use PDO;
|
|
|
|
use PDOStatement;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use PHPUnit_Framework_MockObject_MockObject as MockObject;
|
|
|
|
use PHPUnit_Framework_MockObject_MockObject as MockObject;
|
|
|
|
use ReflectionObject;
|
|
|
|
|
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DbTest extends TestCase
|
|
|
|
class DbTest extends TestCase
|
|
|
|
{
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Database\Db::setDbManager()
|
|
|
|
* @covers \Engelsystem\Database\Db::setDbManager()
|
|
|
|
|
|
|
|
* @covers \Engelsystem\Database\Db::connection()
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function testSetDbManager()
|
|
|
|
public function testSetDbManager()
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -43,27 +41,7 @@ class DbTest extends TestCase
|
|
|
|
|
|
|
|
|
|
|
|
Db::setDbManager($dbManager);
|
|
|
|
Db::setDbManager($dbManager);
|
|
|
|
$this->assertEquals($pdo, Db::getPdo());
|
|
|
|
$this->assertEquals($pdo, Db::getPdo());
|
|
|
|
}
|
|
|
|
$this->assertEquals($databaseConnection, Db::connection());
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @covers \Engelsystem\Database\Db::query()
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function testQuery()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$stm = Db::query('SELECT * FROM test_data');
|
|
|
|
|
|
|
|
$this->assertEquals('00000', $stm->errorCode());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$stm = Db::query('SELECT * FROM test_data WHERE id = ?', [4]);
|
|
|
|
|
|
|
|
$this->assertEquals('00000', $stm->errorCode());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @covers \Engelsystem\Database\Db::unprepared()
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function testUnprepared()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$return = Db::unprepared('SELECT * FROM test_data WHERE id = 3');
|
|
|
|
|
|
|
|
$this->assertTrue($return);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -99,11 +77,8 @@ class DbTest extends TestCase
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function testInsert()
|
|
|
|
public function testInsert()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$count = Db::insert("INSERT INTO test_data (id, data) VALUES (5, 'Some random text'), (6, 'another text')");
|
|
|
|
$result = Db::insert("INSERT INTO test_data (id, data) VALUES (5, 'Some random text'), (6, 'another text')");
|
|
|
|
$this->assertEquals(2, $count);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
|
|
|
|
$count = Db::insert('INSERT INTO test_data(id, data) VALUES (:id, :alias)', ['id' => 7, 'alias' => 'Blafoo']);
|
|
|
|
|
|
|
|
$this->assertEquals(1, $count);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -130,42 +105,6 @@ class DbTest extends TestCase
|
|
|
|
$this->assertEquals(3, $count);
|
|
|
|
$this->assertEquals(3, $count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @covers \Engelsystem\Database\Db::statement()
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function testStatement()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$return = Db::statement('SELECT * FROM test_data WHERE id = 3');
|
|
|
|
|
|
|
|
$this->assertTrue($return);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$return = Db::statement('SELECT * FROM test_data WHERE id = ?', [2]);
|
|
|
|
|
|
|
|
$this->assertTrue($return);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @covers \Engelsystem\Database\Db::getError()
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function testGetError()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Db::statement('foo');
|
|
|
|
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$error = Db::getError();
|
|
|
|
|
|
|
|
$this->assertTrue(is_array($error));
|
|
|
|
|
|
|
|
$this->assertEquals('near "foo": syntax error', $error[2]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$db = new Db();
|
|
|
|
|
|
|
|
$refObject = new ReflectionObject($db);
|
|
|
|
|
|
|
|
$refProperty = $refObject->getProperty('stm');
|
|
|
|
|
|
|
|
$refProperty->setAccessible(true);
|
|
|
|
|
|
|
|
$refProperty->setValue(null, null);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$error = Db::getError();
|
|
|
|
|
|
|
|
$this->assertEquals([-1, null, null], $error);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Database\Db::getPdo()
|
|
|
|
* @covers \Engelsystem\Database\Db::getPdo()
|
|
|
|
*/
|
|
|
|
*/
|
|
|
@ -175,15 +114,6 @@ class DbTest extends TestCase
|
|
|
|
$this->assertInstanceOf(PDO::class, $pdo);
|
|
|
|
$this->assertInstanceOf(PDO::class, $pdo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @covers \Engelsystem\Database\Db::getStm()
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function testGetStm()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$stm = Db::getStm();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(PDOStatement::class, $stm);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Setup in memory database
|
|
|
|
* Setup in memory database
|
|
|
|
*/
|
|
|
|
*/
|
|
|
@ -196,14 +126,14 @@ class DbTest extends TestCase
|
|
|
|
|
|
|
|
|
|
|
|
Db::setDbManager($dbManager);
|
|
|
|
Db::setDbManager($dbManager);
|
|
|
|
Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
Db::query(
|
|
|
|
Db::connection()->statement(
|
|
|
|
'
|
|
|
|
'
|
|
|
|
CREATE TABLE test_data(
|
|
|
|
CREATE TABLE test_data(
|
|
|
|
id INT PRIMARY KEY NOT NULL,
|
|
|
|
id INT PRIMARY KEY NOT NULL,
|
|
|
|
data TEXT NOT NULL
|
|
|
|
data TEXT NOT NULL
|
|
|
|
);
|
|
|
|
);
|
|
|
|
');
|
|
|
|
');
|
|
|
|
Db::query('CREATE UNIQUE INDEX test_data_id_uindex ON test_data (id);');
|
|
|
|
Db::connection()->statement('CREATE UNIQUE INDEX test_data_id_uindex ON test_data (id);');
|
|
|
|
Db::insert("
|
|
|
|
Db::insert("
|
|
|
|
INSERT INTO test_data (id, data)
|
|
|
|
INSERT INTO test_data (id, data)
|
|
|
|
VALUES
|
|
|
|
VALUES
|
|
|
|