clock = new \App\Framework\DateTime\SystemClock(); $this->connection = Mockery::mock(ConnectionInterface::class); $this->repository = new DatabaseAssetRepository($this->connection); }); it('saves asset to database', function () { $asset = AssetTestHelpers::createAsset($this->clock); $this->connection->shouldReceive('execute') ->once() ->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class)) ->andReturn(1); $this->repository->save($asset); }); it('finds asset by id', function () { $assetId = AssetId::generate($this->clock); $row = [ 'id' => $assetId->toString(), 'bucket' => 'media', 'key' => 'orig/2025/01/15/test.jpg', 'mime' => 'image/jpeg', 'bytes' => 1024, 'sha256' => Hash::create('test', HashAlgorithm::SHA256)->toString(), 'meta' => json_encode(['width' => 1920, 'height' => 1080]), 'created_at' => '2025-01-15 10:00:00', ]; $result = Mockery::mock(ResultInterface::class); $result->shouldReceive('fetch') ->once() ->andReturn($row); $this->connection->shouldReceive('query') ->once() ->andReturn($result); $found = $this->repository->findById($assetId); expect($found)->toBeInstanceOf(Asset::class); expect($found->id->equals($assetId))->toBeTrue(); }); it('finds asset by SHA256 hash', function () { $hash = Hash::create('test-content', HashAlgorithm::SHA256); $row = [ 'id' => AssetId::generate($this->clock)->toString(), 'bucket' => 'media', 'key' => 'orig/2025/01/15/test.jpg', 'mime' => 'image/jpeg', 'bytes' => 1024, 'sha256' => $hash->toString(), 'meta' => json_encode([]), 'created_at' => '2025-01-15 10:00:00', ]; $result = Mockery::mock(ResultInterface::class); $result->shouldReceive('fetch') ->once() ->andReturn($row); $this->connection->shouldReceive('query') ->once() ->andReturn($result); $found = $this->repository->findBySha256($hash); expect($found)->toBeInstanceOf(Asset::class); expect($found->sha256->equals($hash))->toBeTrue(); }); it('finds assets by bucket', function () { $bucket = \App\Framework\Storage\ValueObjects\BucketName::fromString('media'); $row = [ 'id' => AssetId::generate($this->clock)->toString(), 'bucket' => 'media', 'key' => 'orig/2025/01/15/test.jpg', 'mime' => 'image/jpeg', 'bytes' => 1024, 'sha256' => Hash::create('test', HashAlgorithm::SHA256)->toString(), 'meta' => json_encode([]), 'created_at' => '2025-01-15 10:00:00', ]; $result = Mockery::mock(ResultInterface::class); $result->shouldReceive('fetchAll') ->once() ->andReturn([$row]); $this->connection->shouldReceive('query') ->once() ->andReturn($result); $found = $this->repository->findByBucket($bucket); expect($found)->toBeArray(); expect($found)->toHaveCount(1); }); it('deletes asset', function () { $assetId = AssetId::generate($this->clock); $this->connection->shouldReceive('execute') ->once() ->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class)) ->andReturn(1); $this->repository->delete($assetId); }); });