Added first few test cases

This commit is contained in:
Chris 2020-07-13 23:24:30 +02:00
parent 422e6be3f4
commit 42d43c947f
2 changed files with 60 additions and 0 deletions

View File

@ -78,6 +78,7 @@ class LiteDb
call_user_func($upgradeHandler, $this, $this->version); call_user_func($upgradeHandler, $this, $this->version);
// Write the new version number to the database // Write the new version number to the database
$this->dbSetMeta(self::META_SCHEMA_VERSION, $version); $this->dbSetMeta(self::META_SCHEMA_VERSION, $version);
$this->version = $version;
} }
// Refresh the database version, for debug purposes // Refresh the database version, for debug purposes
@ -179,6 +180,16 @@ class LiteDb
return $this->stores[$storeName]; return $this->stores[$storeName];
} }
public function hasStore(string $storeName): bool
{
return array_key_exists($storeName, $this->storeMeta);
}
public function getVersion(): int
{
return $this->version;
}
/** /**
* *
* @param string * @param string

49
tests/LiteDbTest.php Normal file
View File

@ -0,0 +1,49 @@
<?php
namespace NoccyLabs\LiteDb;
class LiteDbText extends \PhpUnit\Framework\TestCase
{
public function testCreatingNewDatabase()
{
if (file_exists("/tmp/litedbtest.db"))
unlink("/tmp/litedbtest.db");
$db = new LiteDb("/tmp/litedbtest.db", 1, function ($db, $v) {
$db->createStore("test");
});
$this->assertTrue($db->hasStore("test"));
$this->assertFalse($db->hasStore("test2"));
$this->assertEquals(1, $db->getVersion());
}
public function testUpgradingDatabase()
{
$db = new LiteDb("/tmp/litedbtest.db", 2, function ($db, $v) {
$db->createStore("test2");
});
$this->assertTrue($db->hasStore("test"));
$this->assertTrue($db->hasStore("test2"));
$this->assertEquals(2, $db->getVersion());
}
public function testUsingIndexes()
{
$db = new LiteDb("/tmp/litedbtest.db", 3, function ($db, $v) {
$db->createStore("test3")
->addIndex("foo", [ 'unique'=>true, 'primary'=>true ]);
});
$this->assertTrue($db->hasStore("test3"));
$this->assertEquals(3, $db->getVersion());
$store = $db->getStore("test3");
$this->assertInstanceOf(ObjectStore::class, $store);
$store->add([ 'foo'=>42, 'bar'=>314 ]);
$record = $store->get(42);
$this->assertEquals(['foo'=>42, 'bar'=>314], $record);
}
}