Skip to content

Commit f5dd22d

Browse files
removed redundant param from the partial structure
1 parent 484c434 commit f5dd22d

3 files changed

Lines changed: 34 additions & 17 deletions

File tree

src/Database/Database.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4366,14 +4366,12 @@ public function updateDocuments(
43664366
if (!empty($cursor) && $cursor->getCollection() !== $collection->getId()) {
43674367
throw new DatabaseException("Cursor document must be from the same Collection.");
43684368
}
4369-
$attributesToCheckForRequiredValidation = ['$updatedAt'];
43704369
unset($updates['$id']);
43714370
unset($updates['$tenant']);
43724371
if (($updates->getCreatedAt() === null || !$this->preserveDates)) {
43734372
unset($updates['$createdAt']);
43744373
} else {
43754374
$updates['$createdAt'] = $updates->getCreatedAt();
4376-
$attributesToCheckForRequiredValidation[] = '$createdAt';
43774375
}
43784376
if ($this->adapter->getSharedTables()) {
43794377
$updates['$tenant'] = $this->adapter->getTenant();
@@ -4390,7 +4388,7 @@ public function updateDocuments(
43904388
$this->adapter->getMaxDateTime(),
43914389
);
43924390

4393-
if (!$validator->isValid($updates, $attributesToCheckForRequiredValidation)) {
4391+
if (!$validator->isValid($updates)) {
43944392
throw new StructureException($validator->getDescription());
43954393
}
43964394

src/Database/Validator/PartialStructure.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ class PartialStructure extends Structure
1313
* Returns true if valid or false if not.
1414
*
1515
* @param mixed $document
16-
* @param array<int, string> $requiredAttributes optional list of required attributes to check
1716
*
1817
* @return bool
1918
*/
20-
public function isValid($document, array $requiredAttributes = []): bool
19+
public function isValid($document): bool
2120
{
2221
if (!$document instanceof Document) {
2322
$this->message = 'Value must be an instance of Document';
@@ -37,17 +36,14 @@ public function isValid($document, array $requiredAttributes = []): bool
3736
$name = $attribute['$id'] ?? '';
3837
$keys[$name] = $attribute;
3938
}
40-
/**
41-
* @var array<mixed,mixed> $requiredAttributesMap
42-
*/
43-
$requiredAttributesMap = [];
39+
$requiredAttributes = [];
4440
foreach ($this->attributes as $attribute) {
45-
if ($attribute['required'] === true && in_array($attribute['$id'], $requiredAttributes)) {
46-
$requiredAttributesMap[] = $attribute;
41+
if ($attribute['required'] === true && $document->offsetExists($attribute['$id'])) {
42+
$requiredAttributes[] = $attribute;
4743
}
4844
}
4945

50-
if (!$this->checkForAllRequiredValues($structure, $requiredAttributesMap, $keys)) {
46+
if (!$this->checkForAllRequiredValues($structure, $requiredAttributes, $keys)) {
5147
return false;
5248
}
5349
if (!$this->checkForUnknownAttributes($structure, $keys)) {

tests/e2e/Adapter/Scopes/DocumentTests.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5064,7 +5064,7 @@ public function testBulkDocumentDateOperations(): void
50645064
// Test 5: Bulk upsert operations with custom dates
50655065
$database->setPreserveDates(true);
50665066

5067-
// Test 5.1: Bulk upsert with different date configurations
5067+
// Test 6: Bulk upsert with different date configurations
50685068
$upsertDocuments = [
50695069
new Document([
50705070
'$id' => 'upsert1',
@@ -5097,7 +5097,7 @@ public function testBulkDocumentDateOperations(): void
50975097
$upsertResults[] = $doc;
50985098
});
50995099

5100-
// Verify initial upsert state
5100+
// Test 7: Verify initial upsert state
51015101
foreach (['upsert1', 'upsert3'] as $id) {
51025102
$doc = $database->getDocument($collection, $id);
51035103
$this->assertEquals($createDate, $doc->getAttribute('$createdAt'), "createdAt mismatch for $id");
@@ -5114,7 +5114,7 @@ public function testBulkDocumentDateOperations(): void
51145114
$this->assertNotEmpty($doc->getAttribute('$updatedAt'), "updatedAt missing for $id");
51155115
}
51165116

5117-
// Test 5.2: Bulk upsert update with custom dates using updateDocuments
5117+
// Test 8: Bulk upsert update with custom dates using updateDocuments
51185118
$newDate = '2000-04-01T12:00:00.000+00:00';
51195119
$updateUpsertDoc = new Document([
51205120
'string' => 'upsert_updated',
@@ -5139,7 +5139,30 @@ public function testBulkDocumentDateOperations(): void
51395139
$this->assertEquals('upsert_updated', $doc->getAttribute('string'), "string mismatch for $id");
51405140
}
51415141

5142-
// Test 5.3: Bulk upsert operations with createOrUpdateDocuments
5142+
// Test 9: checking by passing null to each
5143+
$updateUpsertDoc = new Document([
5144+
'string' => 'upsert_updated',
5145+
'$createdAt' => null,
5146+
'$updatedAt' => null
5147+
]);
5148+
5149+
$upsertIds = [];
5150+
foreach ($upsertDocuments as $doc) {
5151+
$upsertIds[] = $doc->getId();
5152+
}
5153+
5154+
$countUpsert = $database->updateDocuments($collection, $updateUpsertDoc, [
5155+
Query::equal('$id', $upsertIds)
5156+
]);
5157+
$this->assertEquals(4, $countUpsert);
5158+
5159+
foreach ($upsertIds as $id) {
5160+
$doc = $database->getDocument($collection, $id);
5161+
$this->assertNotEmpty($doc->getAttribute('$createdAt'), "createdAt mismatch for $id");
5162+
$this->assertNotEmpty($doc->getAttribute('$updatedAt'), "updatedAt mismatch for $id");
5163+
}
5164+
5165+
// Test 10: Bulk upsert operations with createOrUpdateDocuments
51435166
$upsertUpdateDocuments = [];
51445167
foreach ($upsertDocuments as $doc) {
51455168
$updatedDoc = clone $doc;
@@ -5161,7 +5184,7 @@ public function testBulkDocumentDateOperations(): void
51615184
$this->assertEquals('upsert_updated_via_upsert', $doc->getAttribute('string'), "string mismatch for upsert update");
51625185
}
51635186

5164-
// Test 5.4: Bulk upsert with preserve dates disabled
5187+
// Test 11: Bulk upsert with preserve dates disabled
51655188
$database->setPreserveDates(false);
51665189

51675190
$customDate = 'should be ignored anyways so no error';

0 commit comments

Comments
 (0)