-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
317 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,7 @@ | |
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Test\\": "test" | ||
"Tests\\": "tests" | ||
}, | ||
"files": [ | ||
"./src/dev.php" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
|