2017-10-31 19:26:48 +01:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-10-31 19:26:48 +01:00
|
|
|
namespace Engelsystem\Test\Unit\Database;
|
|
|
|
|
|
|
|
use Engelsystem\Database\Db;
|
2018-01-14 01:48:50 +01:00
|
|
|
use Illuminate\Database\Capsule\Manager as CapsuleManager;
|
|
|
|
use Illuminate\Database\Connection as DatabaseConnection;
|
2017-10-31 19:26:48 +01:00
|
|
|
use PDO;
|
2019-04-24 10:45:00 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-10-31 19:26:48 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
class DbTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
2018-01-14 13:32:57 +01:00
|
|
|
* @covers \Engelsystem\Database\Db::connection()
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Database\Db::setDbManager()
|
2017-10-31 19:26:48 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSetDbManager(): void
|
2017-10-31 19:26:48 +01:00
|
|
|
{
|
2022-04-16 10:38:28 +02:00
|
|
|
/** @var PDO|MockObject $pdo */
|
|
|
|
$pdo = $this->getMockBuilder(PDO::class)
|
2018-01-14 01:48:50 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var CapsuleManager|MockObject $dbManager */
|
2018-01-14 01:48:50 +01:00
|
|
|
$dbManager = $this->getMockBuilder(CapsuleManager::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var DatabaseConnection|MockObject $dbManager */
|
2018-01-14 01:48:50 +01:00
|
|
|
$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());
|
2018-01-14 13:32:57 +01:00
|
|
|
$this->assertEquals($databaseConnection, Db::connection());
|
2017-10-31 19:26:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Database\Db::select()
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSelect(): void
|
2017-10-31 19:26:48 +01:00
|
|
|
{
|
|
|
|
$return = Db::select('SELECT * FROM test_data');
|
|
|
|
$this->assertTrue(count($return) > 3);
|
|
|
|
|
|
|
|
$return = Db::select('SELECT * FROM test_data WHERE id = ?', [2]);
|
|
|
|
$this->assertCount(1, $return);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Database\Db::selectOne()
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSelectOne(): void
|
2017-10-31 19:26:48 +01:00
|
|
|
{
|
|
|
|
$return = Db::selectOne('SELECT * FROM test_data');
|
|
|
|
$this->assertEquals('Foo', $return['data']);
|
|
|
|
|
|
|
|
$return = Db::selectOne('SELECT * FROM test_data WHERE id = -1');
|
|
|
|
$this->assertEmpty($return);
|
|
|
|
|
|
|
|
$return = Db::selectOne('SELECT * FROM test_data WHERE id = ?', [3]);
|
|
|
|
$return = array_pop($return);
|
2019-04-24 10:45:00 +02:00
|
|
|
$this->assertIsNotArray($return);
|
2017-10-31 19:26:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Database\Db::insert()
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testInsert(): void
|
2017-10-31 19:26:48 +01:00
|
|
|
{
|
2018-01-14 13:32:57 +01:00
|
|
|
$result = Db::insert("INSERT INTO test_data (id, data) VALUES (5, 'Some random text'), (6, 'another text')");
|
|
|
|
$this->assertTrue($result);
|
2017-10-31 19:26:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Database\Db::update()
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testUpdate(): void
|
2017-10-31 19:26:48 +01:00
|
|
|
{
|
|
|
|
$count = Db::update("UPDATE test_data SET data='NOPE' WHERE data LIKE '%Replaceme%'");
|
|
|
|
$this->assertEquals(3, $count);
|
|
|
|
|
|
|
|
$count = Db::update("UPDATE test_data SET data=? WHERE data LIKE '%NOPE%'", ['Some random text!']);
|
|
|
|
$this->assertEquals(3, $count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Database\Db::delete()
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testDelete(): void
|
2017-10-31 19:26:48 +01:00
|
|
|
{
|
|
|
|
$count = Db::delete('DELETE FROM test_data WHERE id=1');
|
|
|
|
$this->assertEquals(1, $count);
|
|
|
|
|
|
|
|
$count = Db::delete('DELETE FROM test_data WHERE data LIKE ?', ['%Replaceme%']);
|
|
|
|
$this->assertEquals(3, $count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Database\Db::getPdo()
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testGetPdo(): void
|
2017-10-31 19:26:48 +01:00
|
|
|
{
|
|
|
|
$pdo = Db::getPdo();
|
|
|
|
$this->assertInstanceOf(PDO::class, $pdo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup in memory database
|
|
|
|
*/
|
2019-04-24 10:45:00 +02:00
|
|
|
protected function setUp(): void
|
2017-10-31 19:26:48 +01:00
|
|
|
{
|
2018-01-14 01:48:50 +01:00
|
|
|
$dbManager = new CapsuleManager();
|
|
|
|
$dbManager->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
|
|
|
|
$dbManager->setAsGlobal();
|
|
|
|
$dbManager->bootEloquent();
|
|
|
|
|
|
|
|
Db::setDbManager($dbManager);
|
2017-10-31 19:26:48 +01:00
|
|
|
Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
2018-01-14 13:32:57 +01:00
|
|
|
Db::connection()->statement(
|
2017-10-31 19:26:48 +01:00
|
|
|
'
|
|
|
|
CREATE TABLE test_data(
|
|
|
|
id INT PRIMARY KEY NOT NULL,
|
|
|
|
data TEXT NOT NULL
|
|
|
|
);
|
2019-04-24 11:01:37 +02:00
|
|
|
'
|
|
|
|
);
|
2018-01-14 13:32:57 +01:00
|
|
|
Db::connection()->statement('CREATE UNIQUE INDEX test_data_id_uindex ON test_data (id);');
|
2017-10-31 19:26:48 +01:00
|
|
|
Db::insert("
|
|
|
|
INSERT INTO test_data (id, data)
|
|
|
|
VALUES
|
|
|
|
(1, 'Foo'),
|
|
|
|
(2, 'Bar'),
|
|
|
|
(3, 'Batz'),
|
|
|
|
(4, 'Lorem ipsum dolor sit'),
|
|
|
|
(10, 'Replaceme ipsum dolor sit amet'),
|
|
|
|
(11, 'Lorem Replaceme dolor sit amet'),
|
|
|
|
(12, 'Lorem ipsum Replaceme sit amet')
|
|
|
|
;");
|
|
|
|
}
|
|
|
|
}
|