Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Jun 16, 2022
1 parent fbfd901 commit 4cfe15f
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"autoload-dev": {
"psr-4": {
"Test\\": "test"
"Tests\\": "tests"
},
"files": [
"./src/dev.php"
Expand Down
20 changes: 20 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- PHPUnit configuration file with new format for PHPUnit 9.3+ -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResult="false"
colors="true"
convertDeprecationsToExceptions="true">
<testsuites>
<testsuite name="stroage test suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory>./src/</directory>
</include>
</coverage>
</phpunit>
9 changes: 7 additions & 2 deletions src/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ public function connect() : void
/** 关闭连接 */
public function close()
{
$this->_driver->close();
if($this->_driver instanceof SQLite3){
$this->_driver->close();
}
$this->_driver = null;
}

Expand All @@ -145,6 +147,7 @@ public function configs() : array
}

/**
* 执行
* @param string $statement
* @return bool
* @throws StorageException
Expand Down Expand Up @@ -174,6 +177,7 @@ public function exec(string $statement): bool
}

/**
* query执行
* @param string $statement
* @param array $map
* @return SQLite3Result|null
Expand Down Expand Up @@ -204,6 +208,7 @@ public function query(string $statement, array $map = []): ?SQLite3Result
}

/**
* 预处理执行
* @param string $statement
* @param array $map
* @return SQLite3Result|null
Expand Down Expand Up @@ -319,7 +324,7 @@ public function create(string $table, array $columns, array $indexSQLs = []): bo
}
}

$command = "CREATE TABLE IF NOT EXISTS {$tableName} (" . implode(', ', $stack) . ');';
$command = "CREATE TABLE IF NOT EXISTS {$tableName} (" . implode(',', $stack) . ');';
foreach ($indexSQLs as $indexSQL){
$command .= $indexSQL;
}
Expand Down
39 changes: 39 additions & 0 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);

namespace Tests;

use PHPUnit\Framework\TestCase;
use WorkBunny\Storage\Driver;

class BaseTestCase extends TestCase
{
protected ?Driver $_driver = null;

/**
* @return void
*/
public function setUp(): void
{
if(!$this->_driver){
$this->_driver = new Driver([
'filename' => ':memory',
'debug' => true
]);
}
parent::setUp();
}

/**
* @return Driver|null
*/
public function driver(): ?Driver
{
return $this->_driver;
}

protected function _expectedQuery(string $expected): string
{
return str_replace(["\r\n", "\r", "\n", "\t", ' '], '', $expected);
}
}
85 changes: 85 additions & 0 deletions tests/CreateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);

namespace Tests;

class CreateTest extends BaseTestCase
{
public function testCreateTable()
{
$this->driver()->create("account", [
"id" => [
"INT",
"PRIMARY KEY",
"NOT NULL",
"AUTOINCREMENT"
],
"name" => [
"VARCHAR(25)",
"NOT NULL",
"UNIQUE"
],
]);

$this->assertEquals(
$this->_expectedQuery(<<<doc
CREATE TABLE IF NOT EXISTS `account` (
`id` INT PRIMARY KEY NOT NULL AUTOINCREMENT,
`name` VARCHAR(25) NOT NULL UNIQUE
);
doc)
,
$this->driver()->last(true)[0]
);
}

public function testCreateTableUseIndex()
{
$this->driver()->create("account", [
"id" => [
"INT",
"PRIMARY KEY",
"NOT NULL",
"AUTOINCREMENT"
],
"name" => [
"VARCHAR(25)",
"NOT NULL",
"UNIQUE"
],
],[
"CREATE INDEX `account_name` ON `account` (`name`);"
]);

$this->assertEquals(
$this->_expectedQuery(<<<doc
CREATE TABLE IF NOT EXISTS `account` (
`id` INT PRIMARY KEY NOT NULL AUTOINCREMENT,
`name` VARCHAR(25) NOT NULL UNIQUE
);
CREATE INDEX `account_name` ON `account` (`name`);
doc)
,
$this->driver()->last(true)[0]
);
}

public function testCreateTableWithStringDefinition()
{
$this->driver()->create("account", [
"id" => "INT PRIMARY KEY NOT NULL AUTOINCREMENT",
"name" => "VARCHAR(25) NOT NULL UNIQUE"
]);

$this->assertEquals(
$this->_expectedQuery(<<<doc
CREATE TABLE IF NOT EXISTS `account` (
`id` INT PRIMARY KEY NOT NULL AUTOINCREMENT,
`name` VARCHAR(25) NOT NULL UNIQUE
);
doc)
,
$this->driver()->last(true)[0]
);
}
}
21 changes: 21 additions & 0 deletions tests/DropTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);

namespace Tests;

class DropTest extends BaseTestCase
{

public function testDrop()
{
$this->driver()->drop("account");

$this->assertEquals(
$this->_expectedQuery(<<<doc
DROP TABLE IF EXISTS `account`
doc)
,
$this->driver()->last(true)[0]
);
}
}
48 changes: 48 additions & 0 deletions tests/InsertTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);

namespace Tests;


class InsertTest extends BaseTestCase
{

public function testInsertSingle()
{

$this->driver()->insert("account", [
"id" => 1,
"name" => "test"
]);

$this->assertEquals(
$this->_expectedQuery(<<<doc
INSERT INTO `account` (`id`, `name`) VALUES (1, 'test')
doc)
,
$this->driver()->last(true)[0]
);
}

public function testInsertMulti()
{
$this->driver()->insert("account", [
[
"id" => 1,
"name" => "test1"
],
[
"id" => 2,
"name" => "test2"
]
]);

$this->assertEquals(
$this->_expectedQuery(<<<doc
INSERT INTO `account` (`id`, `name`) VALUES (1, 'test1'), (2, 'test2')
doc)
,
$this->driver()->last(true)[0]
);
}
}
48 changes: 48 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);

namespace Tests;


class QueryTest extends BaseTestCase
{

public function testInsertSingle()
{

$this->driver()->insert("account", [
"id" => 1,
"name" => "test"
]);

$this->assertEquals(
$this->_expectedQuery(<<<doc
INSERT INTO `account` (`id`, `name`) VALUES (1, 'test')
doc)
,
$this->driver()->last(true)[0]
);
}

public function testInsertMulti()
{
$this->driver()->insert("account", [
[
"id" => 1,
"name" => "test1"
],
[
"id" => 2,
"name" => "test2"
]
]);

$this->assertEquals(
$this->_expectedQuery(<<<doc
INSERT INTO `account` (`id`, `name`) VALUES (1, 'test1'), (2, 'test2')
doc)
,
$this->driver()->last(true)[0]
);
}
}
48 changes: 48 additions & 0 deletions tests/xhprof.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
require_once './../vendor/autoload.php';

ini_set('memory_limit', '-1');

$s = new \WorkBunny\Storage\Driver([
'filename' => ':memory:'
]);

$s->create('main',[
"id" => [
"INT",
"NOT NULL",
"PRIMARY KEY"
],
"name" => [
"VARCHAR(30)",
"NOT NULL"
]
]);
$sql = [];
xhprof(function() use ($s, &$sql){

// $s->action(function () use ($s){
// for ($i = 0; $i < 10000; $i ++){
// $s->insert('main', [
// 'id' => $i,
// 'name' => 'aka_' . $i
// ]);
// }
// });

for ($i = 0; $i < 10000; $i ++){
$data[] = [
'id' => $i,
'name' => 'aka_' . $i
];
}

$s->insert('main', $data);

// $sql = $s->last();
}, true);

dump($sql);


0 comments on commit 4cfe15f

Please sign in to comment.