Skip to content

Commit fea2fcb

Browse files
committed
test: add unit coverage for call activity merge helpers
1 parent ccfc8fd commit fea2fcb

File tree

2 files changed

+107
-3
lines changed

2 files changed

+107
-3
lines changed

ProcessMaker/Models/CallActivity.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected function completeSubprocess(TokenInterface $token, ExecutionInstanceIn
116116
/**
117117
* Decide which data from the subprocess should be merged based on updated keys.
118118
*/
119-
private function resolveUpdatedData($store, array $allData): array
119+
protected function resolveUpdatedData($store, array $allData): array
120120
{
121121
$updatedKeys = method_exists($store, 'getUpdated')
122122
? $store->getUpdated()
@@ -138,7 +138,7 @@ private function resolveUpdatedData($store, array $allData): array
138138
/**
139139
* Merge keys that exist only in the subprocess data.
140140
*/
141-
private function mergeNewKeys(array $data, array $allData, array $parentData): array
141+
protected function mergeNewKeys(array $data, array $allData, array $parentData): array
142142
{
143143
$newKeys = array_diff(array_keys($allData), array_keys($parentData));
144144
if (empty($newKeys)) {
@@ -151,7 +151,7 @@ private function mergeNewKeys(array $data, array $allData, array $parentData): a
151151
/**
152152
* Merge keys that changed in the subprocess but may not have been tracked.
153153
*/
154-
private function mergeChangedKeys(array $data, array $allData, array $parentData): array
154+
protected function mergeChangedKeys(array $data, array $allData, array $parentData): array
155155
{
156156
$changedKeys = [];
157157
foreach ($allData as $key => $value) {
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace Tests\Unit\ProcessMaker\Models;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use ProcessMaker\Models\CallActivity;
7+
8+
class CallActivityMergeTest extends TestCase
9+
{
10+
private function callActivity(): CallActivity
11+
{
12+
return new class extends CallActivity {
13+
public function callResolveUpdatedData($store, array $allData): array
14+
{
15+
return $this->resolveUpdatedData($store, $allData);
16+
}
17+
18+
public function callMergeNewKeys(array $data, array $allData, array $parentData): array
19+
{
20+
return $this->mergeNewKeys($data, $allData, $parentData);
21+
}
22+
23+
public function callMergeChangedKeys(array $data, array $allData, array $parentData): array
24+
{
25+
return $this->mergeChangedKeys($data, $allData, $parentData);
26+
}
27+
};
28+
}
29+
30+
public function testResolveUpdatedDataReturnsAllWhenUpdatedIsNull(): void
31+
{
32+
$store = new class {
33+
public function getUpdated()
34+
{
35+
return null;
36+
}
37+
};
38+
39+
$callActivity = $this->callActivity();
40+
$all = ['a' => 1, 'b' => 2];
41+
42+
$result = $callActivity->callResolveUpdatedData($store, $all);
43+
44+
$this->assertSame($all, $result);
45+
}
46+
47+
public function testResolveUpdatedDataReturnsEmptyWhenUpdatedIsEmptyArray(): void
48+
{
49+
$store = new class {
50+
public function getUpdated(): array
51+
{
52+
return [];
53+
}
54+
};
55+
56+
$callActivity = $this->callActivity();
57+
$all = ['a' => 1, 'b' => 2];
58+
59+
$result = $callActivity->callResolveUpdatedData($store, $all);
60+
61+
$this->assertSame([], $result);
62+
}
63+
64+
public function testResolveUpdatedDataReturnsOnlyUpdatedKeys(): void
65+
{
66+
$store = new class {
67+
public function getUpdated(): array
68+
{
69+
return ['b'];
70+
}
71+
};
72+
73+
$callActivity = $this->callActivity();
74+
$all = ['a' => 1, 'b' => 2, 'c' => 3];
75+
76+
$result = $callActivity->callResolveUpdatedData($store, $all);
77+
78+
$this->assertSame(['b' => 2], $result);
79+
}
80+
81+
public function testMergeNewKeysAddsKeysAbsentInParent(): void
82+
{
83+
$callActivity = $this->callActivity();
84+
$data = ['a' => 1];
85+
$all = ['a' => 1, 'b' => 2, 'c' => 3];
86+
$parent = ['a' => 1];
87+
88+
$result = $callActivity->callMergeNewKeys($data, $all, $parent);
89+
90+
$this->assertSame(['a' => 1, 'b' => 2, 'c' => 3], $result);
91+
}
92+
93+
public function testMergeChangedKeysAddsDifferencesNotTracked(): void
94+
{
95+
$callActivity = $this->callActivity();
96+
$data = ['a' => 1]; // already tracked
97+
$all = ['a' => 1, 'b' => 99, 'c' => 'new'];
98+
$parent = ['a' => 1, 'b' => 2, 'c' => 'old'];
99+
100+
$result = $callActivity->callMergeChangedKeys($data, $all, $parent);
101+
102+
$this->assertSame(['a' => 1, 'b' => 99, 'c' => 'new'], $result);
103+
}
104+
}

0 commit comments

Comments
 (0)