417 lines
15 KiB
PHP
417 lines
15 KiB
PHP
<?php
|
|
|
|
/** DomFramework - Tests
|
|
* @package domframework
|
|
* @author Dominique Fournier <dominique@fournier38.fr>
|
|
* @license BSD
|
|
*/
|
|
|
|
namespace Domframework\Tests;
|
|
|
|
use Domframework\Dbjson;
|
|
|
|
/** Test the Dbjson database */
|
|
class DbjsonTest extends \PHPUnit_Framework_TestCase
|
|
{
|
|
public function test_insertOne1()
|
|
{
|
|
// Document #0
|
|
define("dbfile", "/tmp/dbjson-" . time());
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->insertOne(
|
|
"collection",
|
|
array ("key1" => "val1", "key2" => "val2")
|
|
);
|
|
$this->assertSame($res, 1);
|
|
}
|
|
|
|
public function test_insertOne2()
|
|
{
|
|
// Document #1
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->insertOne(
|
|
"collection",
|
|
array ("key1" => "val1", "key2" => "val2")
|
|
);
|
|
$this->assertSame($res, 1);
|
|
}
|
|
|
|
public function test_insertMany1()
|
|
{
|
|
// Error : Invalid array provided (not array of array)
|
|
$this->setExpectedException("Exception");
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->insertMany(
|
|
"collection",
|
|
array ("key1" => "val1", "key2" => "val2")
|
|
);
|
|
}
|
|
|
|
public function test_insertMany2()
|
|
{
|
|
// Document #2 and #3
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->insertMany(
|
|
"collection",
|
|
array (array ("key1" => "val3", "key2" => "val2"),
|
|
array ("key1" => "val3", "key2" => "val4"))
|
|
);
|
|
$this->assertSame($res, 2);
|
|
}
|
|
|
|
public function test_filter1()
|
|
{
|
|
// Return all the keys (filter = array ())
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->filter("collection", array ());
|
|
$this->assertSame(array_keys($res), array (0,1,2,3));
|
|
}
|
|
public function test_filter2()
|
|
{
|
|
// Return the keys where filter = array ("key2"=>"val2"))
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->filter("collection", array ("key2" => "val2"));
|
|
$this->assertSame(array_keys($res), array (0,1,2));
|
|
}
|
|
public function test_filter3()
|
|
{
|
|
// Return the keys where filter = array ("key1"=>"val3","key2"=>"val2"))
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->filter("collection", array ("key1" => "val3",
|
|
"key2" => "val2"));
|
|
$this->assertSame(count($res), 1);
|
|
}
|
|
public function test_filter4()
|
|
{
|
|
// Return the keys where filter = array ("key1"=>array ("val3", "=="),
|
|
// "key2"=>array ("val2", "=="))
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->filter("collection", array ("key1" => array ("val3", "=="),
|
|
"key2" => array ("val2", "==")));
|
|
$this->assertSame(count($res), 1);
|
|
}
|
|
public function test_filter5()
|
|
{
|
|
// Return the keys where filter = array ("key1"=>array ("val3", "<="),
|
|
// "key2"=>array ("val2", "=="))
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->filter("collection", array ("key1" => array ("val3", "<="),
|
|
"key2" => array ("val2", "==")));
|
|
$this->assertSame(array_keys($res), array (0,1,2));
|
|
}
|
|
public function test_filter6()
|
|
{
|
|
// Return the keys where filter = array ("key1"=>array ("val3", "<="),
|
|
// "key2"=>array ("val2", ">"))
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->filter("collection", array ("key1" => array ("val3", "<="),
|
|
"key2" => array ("val2", ">")));
|
|
$this->assertSame(count($res), 1);
|
|
}
|
|
|
|
public function test_find1()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->find("collection", array ("key1" => array ("val3", "<="),
|
|
"key2" => array ("val2", ">")));
|
|
$res = array_values($res);
|
|
// ["_id"] is random : skip the test
|
|
unset($res[0]["_id"]);
|
|
$this->assertSame($res, array (0 => array ("key1" => "val3",
|
|
"key2" => "val4")));
|
|
}
|
|
public function test_find2()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->find(
|
|
"collection",
|
|
array ("key1" => array ("val3", "<="),
|
|
"key2" => array ("val2", ">")),
|
|
"*"
|
|
);
|
|
$res = array_values($res);
|
|
// ["_id"] is random : skip the test
|
|
unset($res[0]["_id"]);
|
|
$this->assertSame($res, array (0 => array ("key1" => "val3",
|
|
"key2" => "val4")));
|
|
}
|
|
public function test_find3()
|
|
{
|
|
// Exception : fields not an array
|
|
$this->setExpectedException("Exception");
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->find(
|
|
"collection",
|
|
array ("key1" => array ("val3", "<="),
|
|
"key2" => array ("val2", ">")),
|
|
"key1"
|
|
);
|
|
}
|
|
public function test_find4()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->find(
|
|
"collection",
|
|
array ("key1" => array ("val3", "<="),
|
|
"key2" => array ("val2", ">")),
|
|
array ("key1")
|
|
);
|
|
// ["_id"] is random : skip the test
|
|
$res = array_values($res);
|
|
unset($res[0]["_id"]);
|
|
$this->assertSame($res, array (0 => array ("key1" => "val3")));
|
|
}
|
|
public function test_find5()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->find(
|
|
"collection",
|
|
array ("key1" => array ("val3", "<="),
|
|
"key2" => array ("val2", ">")),
|
|
array ("key1", "key2")
|
|
);
|
|
$res = array_values($res);
|
|
// ["_id"] is random : skip the test
|
|
unset($res[0]["_id"]);
|
|
$this->assertSame($res, array (0 => array ("key1" => "val3",
|
|
"key2" => "val4")));
|
|
}
|
|
public function test_find6()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->find(
|
|
"collection",
|
|
array ("key1" => array ("val3", "<="),
|
|
"key2" => array ("val2", "==")),
|
|
array ("key1", "key2")
|
|
);
|
|
$res = array_values($res);
|
|
// ["_id"] is random : skip the test
|
|
unset($res[0]["_id"]);
|
|
unset($res[1]["_id"]);
|
|
unset($res[2]["_id"]);
|
|
$this->assertSame($res, array (0 => array ("key1" => "val1",
|
|
"key2" => "val2"),
|
|
1 => array ("key1" => "val1",
|
|
"key2" => "val2"),
|
|
2 => array ("key1" => "val3",
|
|
"key2" => "val2")));
|
|
}
|
|
public function test_find7()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->find(
|
|
"collection",
|
|
array ("key1" => array ("val3", "<="),
|
|
"key2" => array ("val2", "==")),
|
|
array ("key2")
|
|
);
|
|
$res = array_values($res);
|
|
// ["_id"] is random : skip the test
|
|
unset($res[0]["_id"]);
|
|
unset($res[1]["_id"]);
|
|
unset($res[2]["_id"]);
|
|
$this->assertSame($res, array (0 => array ("key2" => "val2"),
|
|
1 => array ("key2" => "val2"),
|
|
2 => array ("key2" => "val2")));
|
|
}
|
|
public function test_find8()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->find(
|
|
"collection",
|
|
array ("key1" => array ("val3", "<="),
|
|
"key2" => array ("val2", "==")),
|
|
array ("key2"),
|
|
1
|
|
);
|
|
$res = array_values($res);
|
|
// ["_id"] is random : skip the test
|
|
unset($res[0]["_id"]);
|
|
$this->assertSame($res, array (0 => array ("key2" => "val2")));
|
|
}
|
|
|
|
public function test_deleteOne1()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->deleteOne(
|
|
"collection",
|
|
array ("key1" => array ("val3", "<="),
|
|
"key2" => array ("val2",
|
|
"=="))
|
|
);
|
|
$this->assertSame($res, 1);
|
|
}
|
|
public function test_find9()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->find(
|
|
"collection",
|
|
array ("key1" => array ("val3", "<="),
|
|
"key2" => array ("val2", "==")),
|
|
array ("key2")
|
|
);
|
|
$res = array_values($res);
|
|
// ["_id"] is random : skip the test
|
|
unset($res[0]["_id"]);
|
|
unset($res[1]["_id"]);
|
|
$this->assertSame($res, array (0 => array ("key2" => "val2"),
|
|
1 => array ("key2" => "val2")));
|
|
}
|
|
|
|
public function test_deleteMany1()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->deleteMany(
|
|
"collection",
|
|
array ("key1" => array ("val3", "<="),
|
|
"key2" => array ("val2",
|
|
"=="))
|
|
);
|
|
$this->assertSame($res, 2);
|
|
}
|
|
public function test_find10()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->find(
|
|
"collection",
|
|
array ("key1" => array ("val3", "<="),
|
|
"key2" => array ("val2", "==")),
|
|
array ("key2")
|
|
);
|
|
// ["_id"] is random : skip the test
|
|
unset($res[1]["_id"]);
|
|
unset($res[2]["_id"]);
|
|
$this->assertSame($res, array ());
|
|
}
|
|
|
|
public function test_find11()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = array_values($dbjson->find("collection"));
|
|
// ["_id"] is random : skip the test
|
|
unset($res[0]["_id"]);
|
|
$this->assertSame($res, array (0 => array ("key1" => "val3", "key2" => "val4")));
|
|
}
|
|
|
|
public function test_replace1()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->replace("collection", array (), array ("key2" => "val5"));
|
|
$this->assertSame($res, 1);
|
|
}
|
|
public function test_find12()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = array_values($dbjson->find("collection"));
|
|
// ["_id"] is random : skip the test
|
|
unset($res[0]["_id"]);
|
|
$this->assertSame($res, array (0 => array ("key2" => "val5")));
|
|
}
|
|
|
|
public function test_update1()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->update("collection", array (), array ("key2" => "val6",
|
|
"key5" => "val5"));
|
|
$this->assertSame($res, 1);
|
|
}
|
|
public function test_find13()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = array_values($dbjson->find("collection"));
|
|
// ["_id"] is random : skip the test
|
|
unset($res[0]["_id"]);
|
|
$this->assertSame($res, array (0 => array ("key2" => "val6",
|
|
"key5" => "val5")));
|
|
}
|
|
public function test_insertOne3()
|
|
{
|
|
// Document #4
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->insertOne(
|
|
"collection",
|
|
array ("key1" => "val1", "key2" => "val2")
|
|
);
|
|
$this->assertSame($res, 1);
|
|
}
|
|
public function test_find14()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = array_values($dbjson->find("collection"));
|
|
// ["_id"] is random : skip the test
|
|
unset($res[0]["_id"]);
|
|
unset($res[1]["_id"]);
|
|
$this->assertSame($res, array (0 => array ("key2" => "val6",
|
|
"key5" => "val5"),
|
|
1 => array ("key1" => "val1",
|
|
"key2" => "val2")));
|
|
}
|
|
|
|
public function test_update2()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->update("collection", array (), array ("key2" => "val7",
|
|
"key5" => "val8"));
|
|
$this->assertSame($res, 2);
|
|
}
|
|
public function test_find15()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = array_values($dbjson->find("collection"));
|
|
// ["_id"] is random : skip the test
|
|
unset($res[0]["_id"]);
|
|
unset($res[1]["_id"]);
|
|
$this->assertSame($res, array (0 => array ("key2" => "val7",
|
|
"key5" => "val8"),
|
|
1 => array ("key1" => "val1",
|
|
"key2" => "val7",
|
|
"key5" => "val8")));
|
|
}
|
|
|
|
public function test_update3()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = $dbjson->update(
|
|
"collection",
|
|
array (),
|
|
array ("key2" => "val9",
|
|
"key5" => "val7",
|
|
"_unset" => array ("key2"))
|
|
);
|
|
$this->assertSame($res, 2);
|
|
}
|
|
public function test_find16()
|
|
{
|
|
$dbjson = new Dbjson("dbjson://" . dbfile);
|
|
$res = array_values($dbjson->find("collection"));
|
|
// ["_id"] is random : skip the test
|
|
unset($res[0]["_id"]);
|
|
unset($res[1]["_id"]);
|
|
$this->assertSame($res, array (0 => array ("key5" => "val7"),
|
|
1 => array ("key1" => "val1",
|
|
"key5" => "val7")));
|
|
}
|
|
|
|
// Concurrency tests
|
|
public function test_concurrency1()
|
|
{
|
|
$dbjson1 = new Dbjson("dbjson://" . dbfile);
|
|
$dbjson2 = new Dbjson("dbjson://" . dbfile);
|
|
$dbjson1->insertOne(
|
|
"collection",
|
|
array ("key1" => "val1", "key2" => "val2")
|
|
);
|
|
$res = array_values($dbjson2->find("collection"));
|
|
// ["_id"] is random : skip the test
|
|
unset($res[0]["_id"]);
|
|
unset($res[1]["_id"]);
|
|
unset($res[2]["_id"]);
|
|
$this->assertSame($res, array (0 => array ("key5" => "val7"),
|
|
1 => array ("key1" => "val1",
|
|
"key5" => "val7"),
|
|
2 => array ("key1" => "val1",
|
|
"key2" => "val2")));
|
|
}
|
|
}
|