Skip to content

Commit

Permalink
Add spec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Oct 31, 2024
1 parent cc47ff7 commit a5ee44c
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 15 deletions.
2 changes: 2 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,13 @@
<file src="src/GridFS/CollectionWrapper.php">
<InvalidNullableReturnType>
<code><![CDATA[int]]></code>
<code><![CDATA[int]]></code>
</InvalidNullableReturnType>
<MixedAssignment>
<code><![CDATA[$ids[]]]></code>
</MixedAssignment>
<NullableReturnStatement>
<code><![CDATA[$count]]></code>
<code><![CDATA[$this->filesCollection->updateMany(
['filename' => $filename],
['$set' => ['filename' => $newFilename]],
Expand Down
10 changes: 7 additions & 3 deletions src/GridFS/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,11 @@ public function delete(mixed $id)
*/
public function deleteByName(string $filename): void
{
$this->collectionWrapper->deleteFileAndChunksByFilename($filename);
$count = $this->collectionWrapper->deleteFileAndChunksByFilename($filename);

if ($count === 0) {
throw FileNotFoundException::byFilename($filename);
}
}

/**
Expand Down Expand Up @@ -672,9 +676,9 @@ public function rename(mixed $id, string $newFilename)
*/
public function renameByName(string $filename, string $newFilename): void
{
$matchedCount = $this->collectionWrapper->updateFilenameForFilename($filename, $newFilename);
$count = $this->collectionWrapper->updateFilenameForFilename($filename, $newFilename);

if (! $matchedCount) {
if ($count === 0) {
throw FileNotFoundException::byFilename($filename);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/GridFS/CollectionWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function deleteChunksByFilesId(mixed $id): void
/**
* Delete all GridFS files and chunks for a given filename.
*/
public function deleteFileAndChunksByFilename(string $filename): ?int
public function deleteFileAndChunksByFilename(string $filename): int
{
/** @var iterable<array{_id: mixed}> $files */
$files = $this->findFiles(['filename' => $filename], [
Expand Down
13 changes: 3 additions & 10 deletions tests/GridFS/BucketFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,10 @@ public function testDeleteByName(): void
$this->assertCollectionCount($this->chunksCollection, 0);
}

public function testDeleteByNameShouldIgnoreNonexistentFiles(): void
public function testDeleteByNameShouldRequireFileToExist(): void
{
$this->bucket->uploadFromStream('filename', self::createStream('foobar'));

$this->assertCollectionCount($this->filesCollection, 1);
$this->assertCollectionCount($this->chunksCollection, 1);

$this->bucket->deleteByName('nonexistent-filename');

$this->assertCollectionCount($this->filesCollection, 1);
$this->assertCollectionCount($this->chunksCollection, 1);
$this->expectException(FileNotFoundException::class);
$this->bucket->deleteByName('nonexistent-name');
}

public function testDownloadingFileWithMissingChunk(): void
Expand Down
14 changes: 14 additions & 0 deletions tests/UnifiedSpecTests/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,12 @@ private function executeForBucket(Bucket $bucket)

return $bucket->delete($args['id']);

case 'deleteByName':
assertArrayHasKey('filename', $args);
assertIsString($args['filename']);

return $bucket->deleteByName($args['filename']);

case 'downloadByName':
assertArrayHasKey('filename', $args);
assertIsString($args['filename']);
Expand All @@ -812,6 +818,14 @@ private function executeForBucket(Bucket $bucket)

return null;

case 'renameByName':
assertArrayHasKey('filename', $args);
assertArrayHasKey('newFilename', $args);
assertIsString($args['filename']);
assertIsString($args['newFilename']);

return $bucket->renameByName($args['filename'], $args['newFilename']);

case 'uploadWithId':
assertArrayHasKey('id', $args);
$args['_id'] = $args['id'];
Expand Down
2 changes: 2 additions & 0 deletions tests/UnifiedSpecTests/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,11 @@ final class Util
],
Bucket::class => [
'delete' => ['id'],
'deleteByName' => ['filename'],
'downloadByName' => ['filename', 'revision'],
'download' => ['id'],
'rename' => ['id', 'newFilename'],
'renameByName' => ['filename', 'newFilename'],
'uploadWithId' => ['id', 'filename', 'source', 'chunkSizeBytes', 'disableMD5', 'contentType', 'metadata'],
'upload' => ['filename', 'source', 'chunkSizeBytes', 'disableMD5', 'contentType', 'metadata'],
],
Expand Down

0 comments on commit a5ee44c

Please sign in to comment.