Skip to content

Commit

Permalink
possible fix for this path would confict ...
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Jun 4, 2019
1 parent 2016779 commit 7f54aa1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
22 changes: 16 additions & 6 deletions src/Converter.php
Expand Up @@ -2,15 +2,11 @@
namespace Formapro\Yadm;

use function Formapro\Values\array_get;
use function Formapro\Values\array_path_set;

class Converter
{
/**
* @param array $diff
*
* @return array
*/
public static function convertJsonPatchToMongoUpdate(array $diff, array $values)
public static function convertJsonPatchToMongoUpdate(array $diff, array $values): array
{
$update = ['$set' => [], '$unset' => [], '$push' => []];

Expand Down Expand Up @@ -71,6 +67,20 @@ public static function convertJsonPatchToMongoUpdate(array $diff, array $values)
}
}

// if (false == empty($update['$set'])) {
// foreach (array_keys($update['$set']) as $setPath) {
// $matches = [];
// if (preg_match('/(\d+)/', $setPath, $matches)) {
// list ($newSetPath) = explode(".$matches[1]", $setPath, 2);
//
// unset($update['$set'][$setPath]);
// $update['$set'][$newSetPath] = array_get($newSetPath, null, $values);
//
// $arrayFullReset[$newSetPath] = true;
// }
// }
// }

foreach (array_keys($arrayFullReset) as $arrayResetPath) {
foreach (array_keys($update['$set']) as $setPath) {
if ($setPath === $arrayResetPath) {
Expand Down
32 changes: 27 additions & 5 deletions tests/ChangesCollectorTest.php
Expand Up @@ -181,7 +181,7 @@ public function testShouldTrackArrayValueChangedToStringValue()
], $collector->changes(get_values($obj), $collector->getOriginalValues($obj)));
}

public function testShouldFoo()
public function testShouldSetUnsetValues()
{
$obj = $this->createPersistedObject([
'aKey' => 'aVal',
Expand All @@ -203,15 +203,37 @@ public function testShouldFoo()
], $collector->changes(get_values($obj), $collector->getOriginalValues($obj)));
}

public function testShouldReplaceOneObjectByAnother()
{
$obj = $this->createPersistedObject([
'aKey' => ['aCollection' => [['foo' => 'fooVal'], ['bar' => 'barVal']]],
]);

$collector = new ChangesCollector();
$collector->register($obj, get_values($obj));

$newSubObj = new TestObject();
$values = ['foo' => 'aNewFooVal'];
set_values($newSubObj, $values);

$obj->setObject('aKey.aCollection.0', $newSubObj);

$this->assertChangesEquals([
'$set' => [
'aKey.aCollection' => [
0 => ['foo' => 'aNewFooVal'],
1 => ['bar' => 'barVal']
]
]
], $collector->changes(get_values($obj), $collector->getOriginalValues($obj)));
}

private function assertChangesEquals(array $expected, array $actual): void
{
self::assertEquals($expected, $actual, json_encode($actual, JSON_PRETTY_PRINT));
}

/**
* @return object
*/
private function createPersistedObject(array $values = [])
private function createPersistedObject(array $values = []): TestObject
{
$obj = new TestObject();
set_values($obj, $values);
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/FunctionalTest.php
Expand Up @@ -24,7 +24,7 @@ abstract class FunctionalTest extends TestCase
*/
protected function setUpMongoClient()
{
$uri = getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/';
$uri = getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/yadm_test';

$this->client = new Client($uri);
$this->database = $this->client->selectDatabase('yadm_test');
Expand All @@ -40,7 +40,7 @@ protected function setUpMongoClient()

protected function getCollectionFactory(): CollectionFactory
{
$uri = getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/';
$uri = getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/yadm_test';

return new CollectionFactory(new ClientProvider($uri), $uri);
}
Expand Down

0 comments on commit 7f54aa1

Please sign in to comment.