summaryrefslogtreecommitdiff
path: root/tests/Unit/Database/DbTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Unit/Database/DbTest.php')
-rw-r--r--tests/Unit/Database/DbTest.php125
1 files changed, 41 insertions, 84 deletions
diff --git a/tests/Unit/Database/DbTest.php b/tests/Unit/Database/DbTest.php
index 63607cad..ca6ac52c 100644
--- a/tests/Unit/Database/DbTest.php
+++ b/tests/Unit/Database/DbTest.php
@@ -3,45 +3,45 @@
namespace Engelsystem\Test\Unit\Database;
use Engelsystem\Database\Db;
+use Illuminate\Database\Capsule\Manager as CapsuleManager;
+use Illuminate\Database\Connection as DatabaseConnection;
use PDO;
-use PDOStatement;
use PHPUnit\Framework\TestCase;
-use ReflectionObject;
-use Throwable;
+use PHPUnit_Framework_MockObject_MockObject as MockObject;
class DbTest extends TestCase
{
/**
- * @covers \Engelsystem\Database\Db::connect()
+ * @covers \Engelsystem\Database\Db::setDbManager()
+ * @covers \Engelsystem\Database\Db::connection()
*/
- public function testConnect()
+ public function testSetDbManager()
{
- $result = Db::connect('mysql:host=localhost;dbname=someTestDatabaseThatDoesNotExist;charset=utf8');
- $this->assertFalse($result);
-
- $result = Db::connect('sqlite::memory:');
- $this->assertTrue($result);
- }
-
- /**
- * @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);
+ /** @var MockObject|Pdo $pdo */
+ $pdo = $this->getMockBuilder(Pdo::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ /** @var MockObject|CapsuleManager $dbManager */
+ $dbManager = $this->getMockBuilder(CapsuleManager::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ /** @var MockObject|DatabaseConnection $dbManager */
+ $databaseConnection = $this->getMockBuilder(DatabaseConnection::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $dbManager
+ ->expects($this->atLeastOnce())
+ ->method('getConnection')
+ ->willReturn($databaseConnection);
+ $databaseConnection
+ ->expects($this->atLeastOnce())
+ ->method('getPdo')
+ ->willReturn($pdo);
+
+ Db::setDbManager($dbManager);
+ $this->assertEquals($pdo, Db::getPdo());
+ $this->assertEquals($databaseConnection, Db::connection());
}
/**
@@ -77,11 +77,8 @@ class DbTest extends TestCase
*/
public function testInsert()
{
- $count = Db::insert("INSERT INTO test_data (id, data) VALUES (5, 'Some random text'), (6, 'another text')");
- $this->assertEquals(2, $count);
-
- $count = Db::insert('INSERT INTO test_data(id, data) VALUES (:id, :alias)', ['id' => 7, 'alias' => 'Blafoo']);
- $this->assertEquals(1, $count);
+ $result = Db::insert("INSERT INTO test_data (id, data) VALUES (5, 'Some random text'), (6, 'another text')");
+ $this->assertTrue($result);
}
/**
@@ -109,42 +106,6 @@ class DbTest extends TestCase
}
/**
- * @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()
*/
public function testGetPdo()
@@ -154,29 +115,25 @@ class DbTest extends TestCase
}
/**
- * @covers \Engelsystem\Database\Db::getStm()
- */
- public function testGetStm()
- {
- $stm = Db::getStm();
- $this->assertInstanceOf(PDOStatement::class, $stm);
- }
-
- /**
* Setup in memory database
*/
protected function setUp()
{
- Db::connect('sqlite::memory:');
+ $dbManager = new CapsuleManager();
+ $dbManager->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
+ $dbManager->setAsGlobal();
+ $dbManager->bootEloquent();
+
+ Db::setDbManager($dbManager);
Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- Db::query(
+ Db::connection()->statement(
'
CREATE TABLE test_data(
id INT PRIMARY KEY 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("
INSERT INTO test_data (id, data)
VALUES