From 9ba0c01705df523c7e9f84e1233a34e71d819301 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 12 Oct 2020 17:23:23 +0300 Subject: [PATCH 1/4] Allow overriding saver id only via id parameter --- src/Xhgui/Saver/MongoSaver.php | 4 ++-- src/Xhgui/Saver/PdoSaver.php | 4 ++-- src/Xhgui/Saver/SaverInterface.php | 2 +- tests/Saver/MongoTest.php | 2 +- tests/bootstrap.php | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Xhgui/Saver/MongoSaver.php b/src/Xhgui/Saver/MongoSaver.php index 1dd38cc4e..214c67b4b 100644 --- a/src/Xhgui/Saver/MongoSaver.php +++ b/src/Xhgui/Saver/MongoSaver.php @@ -18,7 +18,7 @@ public function __construct(MongoCollection $collection) $this->_collection = $collection; } - public function save(array $data): string + public function save(array $data, string $id = null): string { // build 'request_ts' and 'request_date' from 'request_ts_micro' $ts = $data['meta']['request_ts_micro']; @@ -36,7 +36,7 @@ public function save(array $data): string 'request_date' => date('Y-m-d', $sec), ]; - $id = $data['_id'] ?? new MongoId(); + $id = $id ?? new MongoId(); $a = [ '_id' => $id, 'meta' => $meta, diff --git a/src/Xhgui/Saver/PdoSaver.php b/src/Xhgui/Saver/PdoSaver.php index d32212842..3242bd7c7 100644 --- a/src/Xhgui/Saver/PdoSaver.php +++ b/src/Xhgui/Saver/PdoSaver.php @@ -16,7 +16,7 @@ public function __construct(PdoRepository $db) $this->db = $db; } - public function save(array $data): string + public function save(array $data, string $id = null): string { $main = $data['profile']['main()']; @@ -25,7 +25,7 @@ public function save(array $data): string $sec = $ts['sec']; $usec = $ts['usec']; - $id = $data['_id'] ?? Util::generateId(); + $id = $id ?? Util::generateId(); $this->db->saveProfile([ 'id' => $id, 'profile' => json_encode($data['profile']), diff --git a/src/Xhgui/Saver/SaverInterface.php b/src/Xhgui/Saver/SaverInterface.php index 77d52f513..681fa85e3 100644 --- a/src/Xhgui/Saver/SaverInterface.php +++ b/src/Xhgui/Saver/SaverInterface.php @@ -7,5 +7,5 @@ interface SaverInterface /** * Returns id of the saved profile */ - public function save(array $data): string; + public function save(array $data, string $id = null): string; } diff --git a/tests/Saver/MongoTest.php b/tests/Saver/MongoTest.php index aa33095e8..76e31409d 100644 --- a/tests/Saver/MongoTest.php +++ b/tests/Saver/MongoTest.php @@ -22,7 +22,7 @@ public function testSave() $saver = new MongoSaver($collection); foreach ($data as $profile) { - $saver->save($profile); + $saver->save($profile, $profile['_id'] ?? null); } } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index c0913eaf5..552cbcdf8 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -32,6 +32,6 @@ function loadFixture(SaverInterface $saver, $file) if (isset($record['_id'])) { $record['_id'] = new MongoId($record['_id']); } - $saver->save($record); + $saver->save($record, $record['_id'] ?? null); } } From 61e2ccc9d7d353d76e2a69157fcd805ec3a6b485 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 19 Oct 2020 21:14:25 +0300 Subject: [PATCH 2/4] Fix spelling of Successful --- src/Xhgui/Controller/RunController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Xhgui/Controller/RunController.php b/src/Xhgui/Controller/RunController.php index 9c32730b1..6c8497301 100644 --- a/src/Xhgui/Controller/RunController.php +++ b/src/Xhgui/Controller/RunController.php @@ -167,7 +167,7 @@ public function deleteSubmit(Request $request) { $id = $request->post('id'); // Don't call profilers->delete() unless $id is set, - // otherwise it will turn the null into a MongoId and return "Sucessful". + // otherwise it will turn the null into a MongoId and return "Successful". if (!is_string($id) || !strlen($id)) { // Form checks this already, // only reachable by handcrafted or malformed requests. From ef354dd4b12790ae301a8dfc492cc88f89f1dd2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 19 Oct 2020 21:14:10 +0300 Subject: [PATCH 3/4] Fix mongo save requiring MongoId as an _id for valid value --- src/Xhgui/Saver/MongoSaver.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Xhgui/Saver/MongoSaver.php b/src/Xhgui/Saver/MongoSaver.php index 214c67b4b..805a94682 100644 --- a/src/Xhgui/Saver/MongoSaver.php +++ b/src/Xhgui/Saver/MongoSaver.php @@ -36,15 +36,14 @@ public function save(array $data, string $id = null): string 'request_date' => date('Y-m-d', $sec), ]; - $id = $id ?? new MongoId(); $a = [ - '_id' => $id, + '_id' => new MongoId($id), 'meta' => $meta, 'profile' => $data['profile'], ]; $this->_collection->insert($a, ['w' => 0]); - return (string)$id; + return (string)$a['_id']; } } From 2edde9ef285afe4c5c783950c02b9552f22981c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 19 Oct 2020 21:17:09 +0300 Subject: [PATCH 4/4] Drop un-needed convert to mongo objects The needed conversion is done on mongo saver --- tests/bootstrap.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 552cbcdf8..a990a7b24 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -25,13 +25,6 @@ function loadFixture(SaverInterface $saver, $file) { $data = json_decode(file_get_contents($file), true); foreach ($data as $record) { - if (isset($record['meta']['request_time'])) { - $time = strtotime($record['meta']['request_time']); - $record['meta']['request_time'] = new MongoDate($time); - } - if (isset($record['_id'])) { - $record['_id'] = new MongoId($record['_id']); - } $saver->save($record, $record['_id'] ?? null); } }