Skip to content

Commit 77c44db

Browse files
committed
extend spy format
1 parent e71df31 commit 77c44db

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/Plugins/Spy.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,20 @@ private function getKey(Entity $instance, Space $space)
6262

6363
public function getChanges()
6464
{
65-
return (object) [
66-
'create' => array_values($this->create),
67-
'update' => array_values($this->update),
68-
'remove' => array_values($this->remove),
69-
];
65+
$result = (object) [];
66+
67+
foreach(['create', 'update', 'remove'] as $action) {
68+
$data = [];
69+
foreach($this->$action as $key => $row) {
70+
list($space) = explode(':', $key);
71+
if(!array_key_exists($space, $data)) {
72+
$data[$space][] = $row;
73+
}
74+
}
75+
$result->$action = $data;
76+
}
77+
78+
return $result;
7079
}
7180

7281
public function hasChanges()

tests/SpyTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function test()
4747
$this->assertCount(0, $changes->create);
4848
$this->assertCount(0, $changes->update);
4949
$this->assertCount(1, $changes->remove);
50-
$this->assertSame(array_values($changes->remove)[0]->id, $nekufa->id);
50+
$this->assertSame($changes->remove['person'][0]->id, $nekufa->id);
5151
$this->assertTrue($spy->hasChanges());
5252

5353
$spy->reset();
@@ -66,7 +66,7 @@ public function test()
6666
$this->assertCount(0, $changes->update);
6767
$this->assertCount(0, $changes->remove);
6868

69-
$this->assertSame([$vasya], array_values($changes->create));
69+
$this->assertSame([$vasya], $changes->create['person']);
7070

7171
$vasya->email = 'vasya@ya.ru';
7272
$mapper->save($vasya);
@@ -76,7 +76,7 @@ public function test()
7676
$this->assertCount(1, $changes->create);
7777
$this->assertCount(0, $changes->update);
7878
$this->assertCount(0, $changes->remove);
79-
$this->assertSame([$vasya], array_values($changes->create));
79+
$this->assertSame([$vasya], array_values($changes->create['person']));
8080

8181
$mapper->remove($vasya);
8282
$changes = $spy->getChanges();

0 commit comments

Comments
 (0)