From 089707e88f63cd1a554a8457b12e8838f6c245ea Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Sat, 5 Mar 2016 16:10:40 +0000 Subject: [PATCH] =?UTF-8?q?dbjson=20:=20the=20keys=20are=20the=20=5Fid=20(?= =?UTF-8?q?no=20more=20N=C2=B2=20searches)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2649 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- Tests/dbjsonTest.php | 118 +++++++++++++++++++++++-------------------- dbjson.php | 2 +- 2 files changed, 64 insertions(+), 56 deletions(-) diff --git a/Tests/dbjsonTest.php b/Tests/dbjsonTest.php index 8853b97..2829f14 100644 --- a/Tests/dbjsonTest.php +++ b/Tests/dbjsonTest.php @@ -48,14 +48,14 @@ class test_dbjson extends PHPUnit_Framework_TestCase // Return all the keys (filter = array ()) $dbjson = new dbjson ("dbjson://".dbfile); $res = $dbjson->filter ("collection", array ()); - $this->assertSame ($res, array (0,1,2,3)); + $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 ($res, array (0,1,2)); + $this->assertSame (array_keys ($res), array (0,1,2)); } public function test_filter3 () { @@ -63,7 +63,7 @@ class test_dbjson extends PHPUnit_Framework_TestCase $dbjson = new dbjson ("dbjson://".dbfile); $res = $dbjson->filter ("collection", array ("key1"=>"val3", "key2"=>"val2")); - $this->assertSame ($res, array (2)); + $this->assertSame (count ($res), 1); } public function test_filter4 () { @@ -72,7 +72,7 @@ class test_dbjson extends PHPUnit_Framework_TestCase $dbjson = new dbjson ("dbjson://".dbfile); $res = $dbjson->filter ("collection",array ("key1"=>array ("val3", "=="), "key2"=>array ("val2", "=="))); - $this->assertSame ($res, array (2)); + $this->assertSame (count ($res), 1); } public function test_filter5 () { @@ -81,7 +81,7 @@ class test_dbjson extends PHPUnit_Framework_TestCase $dbjson = new dbjson ("dbjson://".dbfile); $res = $dbjson->filter ("collection", array ("key1"=>array ("val3", "<="), "key2"=>array ("val2", "=="))); - $this->assertSame ($res, array (0,1,2)); + $this->assertSame (array_keys ($res), array (0,1,2)); } public function test_filter6 () { @@ -90,7 +90,7 @@ class test_dbjson extends PHPUnit_Framework_TestCase $dbjson = new dbjson ("dbjson://".dbfile); $res = $dbjson->filter ("collection", array ("key1"=>array ("val3", "<="), "key2"=>array ("val2", ">"))); - $this->assertSame ($res, array (3)); + $this->assertSame (count ($res), 1); } public function test_find1 () @@ -98,10 +98,11 @@ class test_dbjson extends PHPUnit_Framework_TestCase $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[3]["_id"]); - $this->assertSame ($res, array (3=>array ("key1"=>"val3", - "key2"=>"val4"))); + unset ($res[0]["_id"]); + $this->assertSame ($res, array (0=>array ("key1"=>"val3", + "key2"=>"val4"))); } public function test_find2 () { @@ -109,10 +110,11 @@ class test_dbjson extends PHPUnit_Framework_TestCase $res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="), "key2"=>array ("val2", ">")), "*"); + $res = array_values ($res); // ["_id"] is random : skip the test - unset ($res[3]["_id"]); - $this->assertSame ($res, array (3=>array ("key1"=>"val3", - "key2"=>"val4"))); + unset ($res[0]["_id"]); + $this->assertSame ($res, array (0=>array ("key1"=>"val3", + "key2"=>"val4"))); } public function test_find3 () { @@ -130,8 +132,9 @@ class test_dbjson extends PHPUnit_Framework_TestCase "key2"=>array ("val2", ">")), array ("key1")); // ["_id"] is random : skip the test - unset ($res[3]["_id"]); - $this->assertSame ($res, array (3=>array ("key1"=>"val3"))); + $res = array_values ($res); + unset ($res[0]["_id"]); + $this->assertSame ($res, array (0=>array ("key1"=>"val3"))); } public function test_find5 () { @@ -139,10 +142,11 @@ class test_dbjson extends PHPUnit_Framework_TestCase $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[3]["_id"]); - $this->assertSame ($res, array (3=>array ("key1"=>"val3", - "key2"=>"val4"))); + unset ($res[0]["_id"]); + $this->assertSame ($res, array (0=>array ("key1"=>"val3", + "key2"=>"val4"))); } public function test_find6 () { @@ -150,6 +154,7 @@ class test_dbjson extends PHPUnit_Framework_TestCase $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"]); @@ -167,13 +172,14 @@ class test_dbjson extends PHPUnit_Framework_TestCase $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"))); + 1=>array ("key2"=>"val2"), + 2=>array ("key2"=>"val2"))); } public function test_find8 () { @@ -181,6 +187,7 @@ class test_dbjson extends PHPUnit_Framework_TestCase $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"))); @@ -200,11 +207,12 @@ class test_dbjson extends PHPUnit_Framework_TestCase $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 (1=>array ("key2"=>"val2"), - 2=>array ("key2"=>"val2"))); + $this->assertSame ($res, array (0=>array ("key2"=>"val2"), + 1=>array ("key2"=>"val2"))); } public function test_deleteMany1 () @@ -230,10 +238,10 @@ class test_dbjson extends PHPUnit_Framework_TestCase public function test_find11 () { $dbjson = new dbjson ("dbjson://".dbfile); - $res = $dbjson->find ("collection"); + $res = array_values ($dbjson->find ("collection")); // ["_id"] is random : skip the test - unset ($res[3]["_id"]); - $this->assertSame ($res, array (3=>array ("key1"=>"val3", "key2"=>"val4"))); + unset ($res[0]["_id"]); + $this->assertSame ($res, array (0=>array ("key1"=>"val3", "key2"=>"val4"))); } public function test_replace1 () @@ -245,10 +253,10 @@ class test_dbjson extends PHPUnit_Framework_TestCase public function test_find12 () { $dbjson = new dbjson ("dbjson://".dbfile); - $res = $dbjson->find ("collection"); + $res = array_values ($dbjson->find ("collection")); // ["_id"] is random : skip the test - unset ($res[3]["_id"]); - $this->assertSame ($res, array (3=>array ("key2"=>"val5"))); + unset ($res[0]["_id"]); + $this->assertSame ($res, array (0=>array ("key2"=>"val5"))); } public function test_update1 () @@ -261,10 +269,10 @@ class test_dbjson extends PHPUnit_Framework_TestCase public function test_find13 () { $dbjson = new dbjson ("dbjson://".dbfile); - $res = $dbjson->find ("collection"); + $res = array_values ($dbjson->find ("collection")); // ["_id"] is random : skip the test - unset ($res[3]["_id"]); - $this->assertSame ($res, array (3=>array ("key2"=>"val6", + unset ($res[0]["_id"]); + $this->assertSame ($res, array (0=>array ("key2"=>"val6", "key5"=>"val5"))); } public function test_insertOne3 () @@ -278,14 +286,14 @@ class test_dbjson extends PHPUnit_Framework_TestCase public function test_find14 () { $dbjson = new dbjson ("dbjson://".dbfile); - $res = $dbjson->find ("collection"); + $res = array_values ($dbjson->find ("collection")); // ["_id"] is random : skip the test - unset ($res[3]["_id"]); - unset ($res[4]["_id"]); - $this->assertSame ($res, array (3=>array ("key2"=>"val6", - "key5"=>"val5"), - 4=>array ("key1"=>"val1", - "key2"=>"val2"))); + 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 () @@ -298,13 +306,13 @@ class test_dbjson extends PHPUnit_Framework_TestCase public function test_find15 () { $dbjson = new dbjson ("dbjson://".dbfile); - $res = $dbjson->find ("collection"); + $res = array_values ($dbjson->find ("collection")); // ["_id"] is random : skip the test - unset ($res[3]["_id"]); - unset ($res[4]["_id"]); - $this->assertSame ($res, array (3=>array ("key2"=>"val7", + unset ($res[0]["_id"]); + unset ($res[1]["_id"]); + $this->assertSame ($res, array (0=>array ("key2"=>"val7", "key5"=>"val8"), - 4=>array ("key1"=>"val1", + 1=>array ("key1"=>"val1", "key2"=>"val7", "key5"=>"val8"))); } @@ -321,12 +329,12 @@ class test_dbjson extends PHPUnit_Framework_TestCase public function test_find16 () { $dbjson = new dbjson ("dbjson://".dbfile); - $res = $dbjson->find ("collection"); + $res = array_values ($dbjson->find ("collection")); // ["_id"] is random : skip the test - unset ($res[3]["_id"]); - unset ($res[4]["_id"]); - $this->assertSame ($res, array (3=>array ("key5"=>"val7"), - 4=>array ("key1"=>"val1", + unset ($res[0]["_id"]); + unset ($res[1]["_id"]); + $this->assertSame ($res, array (0=>array ("key5"=>"val7"), + 1=>array ("key1"=>"val1", "key5"=>"val7"))); } @@ -337,15 +345,15 @@ class test_dbjson extends PHPUnit_Framework_TestCase $dbjson2 = new dbjson ("dbjson://".dbfile); $dbjson1->insertOne ("collection", array ("key1"=>"val1", "key2"=>"val2")); - $res = $dbjson2->find ("collection"); + $res = array_values ($dbjson2->find ("collection")); // ["_id"] is random : skip the test - unset ($res[3]["_id"]); - unset ($res[4]["_id"]); - unset ($res[5]["_id"]); - $this->assertSame ($res, array (3=>array ("key5"=>"val7"), - 4=>array ("key1"=>"val1", + 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"), - 5=>array ("key1"=>"val1", + 2=>array ("key1"=>"val1", "key2"=>"val2"))); } } diff --git a/dbjson.php b/dbjson.php index 05a5f22..ab3c1ee 100644 --- a/dbjson.php +++ b/dbjson.php @@ -243,7 +243,7 @@ class dbjson if (count ($keys) === 0) return 0; reset ($keys); - $key = key ($keys); + $key = reset ($keys); unset ($this->db[$collection]["content"][$key]); $this->writeDB (); $this->lockUN ();