Skip to content

Commit

Permalink
project: implement binding for statObject() (#15)
Browse files Browse the repository at this point in the history
* Update phpstan

* project: implement binding for statObject()
  • Loading branch information
Erikvv committed Nov 17, 2021
1 parent 9eab60c commit e391944
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pipeline {
stage('PHPStan') {
agent {
docker {
image 'phpstan/phpstan:0.12.89'
image 'ghcr.io/phpstan/phpstan:1.0.2'
args '--mount type=volume,source=phpstan-cache,destination=/tmp/phpstan ' +
'--user root:root ' +
"--entrypoint='' "
Expand Down
40 changes: 29 additions & 11 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
parameters:
level: 6
paths:
- src
- test
ignoreErrors:
- '/Call to an undefined method FFI::.*/'
- '/Access to an undefined property FFI\\CData::.*/'
-
message: '/Binary operation "-" between string&numeric and string results in an error./'
paths:
- test/ListObjectsTest.php
level: 6
paths:
- src
- test
ignoreErrors:
- '/Call to an undefined method FFI::.*/'
- '/Access to an undefined property FFI\\CData::.*/'
-
message: '/Binary operation "-" between numeric-string and string results in an error./'
paths:
- test/ListObjectsTest.php
-
message: "#^Binary operation \"\\-\" between int and string results in an error\\.$#"
count: 1
path: test/StatObjectTest.php
-
message: "#^Property Storj\\\\Uplink\\\\Access\\:\\:\\$scope is never read, only written\\.$#"
count: 1
path: src/Access.php

-
message: "#^Property Storj\\\\Uplink\\\\EncryptionKey\\:\\:\\$scope is never read, only written\\.$#"
count: 1
path: src/EncryptionKey.php

-
message: "#^Property Storj\\\\Uplink\\\\Project\\:\\:\\$scope is never read, only written\\.$#"
count: 1
path: src/Project.php
2 changes: 1 addition & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Config

public function __construct()
{
$this->tempDirectory = $tempDirectory ?? sys_get_temp_dir();
$this->tempDirectory = sys_get_temp_dir();
}

public function withUserAgent(string $userAgent): self
Expand Down
8 changes: 1 addition & 7 deletions src/EncryptionKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@

class EncryptionKey
{
/**
* With libuplink.so and header files loaded
*/
private FFI $ffi;

/**
* The associated C struct of type UplinkEncryptionKey
*/
Expand All @@ -26,9 +21,8 @@ class EncryptionKey
/**
* @internal
*/
public function __construct(FFI $ffi, CData $cEncryptionKey, Scope $scope)
public function __construct(CData $cEncryptionKey, Scope $scope)
{
$this->ffi = $ffi;
$this->cEncryptionKey = $cEncryptionKey;
$this->scope = $scope;
}
Expand Down
14 changes: 14 additions & 0 deletions src/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,20 @@ public function deleteObject(string $bucketName, string $objectKey): ?ObjectInfo
return new ObjectInfo($objectResult->object, true, true);
}

/**
* Return information about an object at the specific key.
* Can not be used to confirm the existence of a prefix.
*/
public function statObject(string $bucketName, string $objectKey): ObjectInfo
{
$objectResult = $this->ffi->uplink_stat_object($this->cProject, $bucketName, $objectKey);
$scope = Scope::exit(fn() => $this->ffi->uplink_free_object_result($objectResult));

Util::throwIfErrorResult($objectResult);

return new ObjectInfo($objectResult->object, true, true);
}

/**
* @param string $bucketName
* @return ObjectInfo[]|Generator<ObjectInfo>
Expand Down
1 change: 0 additions & 1 deletion src/Uplink.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ public function deriveEncryptionKey(string $passphrase, string $salt): Encryptio
Util::throwIfErrorResult($encryptionKeyResult);

return new EncryptionKey(
$this->ffi,
$encryptionKeyResult->encryption_key,
$scope
);
Expand Down
74 changes: 74 additions & 0 deletions test/StatObjectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Storj\Uplink\Test;

use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use Storj\Uplink\Exception\Object\ObjectNotFound;

class StatObjectTest extends TestCase
{
public static function setUpBeforeClass(): void
{
$project = Util::emptyAccess()->openProject();
$project->createBucket('phpunit');

$upload = $project->uploadObject('phpunit', 'StatPrefix/StatObject');
$upload->setCustomMetadata(['myhouse' => 'myrules']);
$upload->write(random_bytes(55));
$upload->commit();
}

public function testStatObject(): void
{
$objectInfo = Util::project()->statObject('phpunit', 'StatPrefix/StatObject');

self::assertEquals('StatPrefix/StatObject', $objectInfo->getKey());
self::assertEquals(['myhouse' => 'myrules'], $objectInfo->getCustomMetadata());
self::assertFalse($objectInfo->isPrefix());

$systemMetaData = $objectInfo->getSystemMetadata();

self::assertEquals(55, $systemMetaData->getContentLength());
self::assertEquals(null, $systemMetaData->getExpires());
self::assertLessThan(
60,
abs(time() - $systemMetaData->getCreated()->format('U'))
);
}

/**
* Stat on a prefix is not implemented in Uplink
*/
public function testStatPrefixWithSlash(): void
{
$this->expectException(ObjectNotFound::class);

$objectInfo = Util::project()->statObject('phpunit', 'StatPrefix/');

// code not reached
self::assertEquals('StatPrefix/', $objectInfo->getKey());
self::assertTrue($objectInfo->isPrefix());
}

/**
* Stat on a prefix is not implemented in Uplink
*/
public function testStatPrefixWithoutSlash(): void
{
$this->expectException(ObjectNotFound::class);

$objectInfo = Util::project()->statObject('phpunit', 'StatPrefix');

// code not reached
self::assertEquals('StatPrefix/', $objectInfo->getKey());
self::assertTrue($objectInfo->isPrefix());
}

public function testStatNonExistentObject(): void
{
self::expectException(ObjectNotFound::class);

$objectInfo = Util::project()->statObject('phpunit', 'OtherObject');
}
}

0 comments on commit e391944

Please sign in to comment.