-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathArrayDiffTest.php
More file actions
118 lines (95 loc) · 3.27 KB
/
ArrayDiffTest.php
File metadata and controls
118 lines (95 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace DBDiff\Tests\Unit;
use PHPUnit\Framework\TestCase;
use DBDiff\DB\Data\ArrayDiff;
use Diff\DiffOp\DiffOpAdd;
use Diff\DiffOp\DiffOpRemove;
/**
* Tests that ArrayDiff works correctly with plain PHP arrays
* (verifies the fix for Bug #4 where Collection objects were passed
* to array_merge).
*/
class ArrayDiffTest extends TestCase
{
protected function tearDown(): void
{
\DBDiff\Params\ParamsFactory::reset();
}
/**
* Minimal stub that mimics TableIterator but returns plain arrays.
*/
private function makeIterator(array $batches): object
{
return new class($batches) {
private array $batches;
private int $index = 0;
public function __construct(array $batches)
{
$this->batches = $batches;
}
public function hasNext(): bool
{
return $this->index < count($this->batches);
}
public function next(int $size): array
{
return $this->batches[$this->index++] ?? [];
}
};
}
public function testIdenticalDataProducesNoDiff(): void
{
$rows = [['id' => 1, 'name' => 'Alice'], ['id' => 2, 'name' => 'Bob']];
\DBDiff\Params\ParamsFactory::set(new \stdClass());
$diff = new ArrayDiff(
['id'],
$this->makeIterator([$rows]),
$this->makeIterator([$rows])
);
$result = $diff->getDiff('test_table');
$this->assertEmpty($result, 'Identical data should produce no diff');
}
public function testNewRowDetected(): void
{
$source = [['id' => 1, 'name' => 'Alice'], ['id' => 2, 'name' => 'Bob']];
$target = [['id' => 1, 'name' => 'Alice']];
\DBDiff\Params\ParamsFactory::set(new \stdClass());
$diff = new ArrayDiff(
['id'],
$this->makeIterator([$source]),
$this->makeIterator([$target])
);
$result = $diff->getDiff('test_table');
$this->assertCount(1, $result);
$this->assertInstanceOf(DiffOpAdd::class, $result[0]['diff']);
}
public function testDeletedRowDetected(): void
{
$source = [['id' => 1, 'name' => 'Alice']];
$target = [['id' => 1, 'name' => 'Alice'], ['id' => 2, 'name' => 'Bob']];
\DBDiff\Params\ParamsFactory::set(new \stdClass());
$diff = new ArrayDiff(
['id'],
$this->makeIterator([$source]),
$this->makeIterator([$target])
);
$result = $diff->getDiff('test_table');
$this->assertCount(1, $result);
$this->assertInstanceOf(DiffOpRemove::class, $result[0]['diff']);
}
public function testChangedRowDetected(): void
{
$source = [['id' => 1, 'name' => 'Alice Updated']];
$target = [['id' => 1, 'name' => 'Alice']];
\DBDiff\Params\ParamsFactory::set(new \stdClass());
$diff = new ArrayDiff(
['id'],
$this->makeIterator([$source]),
$this->makeIterator([$target])
);
$result = $diff->getDiff('test_table');
$this->assertCount(1, $result);
$this->assertIsArray($result[0]['diff']);
$this->assertArrayHasKey('name', $result[0]['diff']);
}
}