Skip to content

Commit b92ad9e

Browse files
committed
fix tests
1 parent e1cb1da commit b92ad9e

File tree

1 file changed

+52
-42
lines changed

1 file changed

+52
-42
lines changed

tests/Unit/CloudinaryStorageAdapterTest.php

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
uses(ProphecyTrait::class);
1414

15-
function createApiResponse(array $data): ApiResponse
15+
function createApiResponse(array $data, int $statusCode = 200): ApiResponse
1616
{
17-
return new ApiResponse($data, ['headers' => [], 'statusCode' => 200]);
17+
return new ApiResponse($data, ['headers' => [], 'statusCode' => $statusCode]);
1818
}
1919

2020
beforeEach(function () {
@@ -25,69 +25,78 @@ function createApiResponse(array $data): ApiResponse
2525
$this->cloudinary->uploadApi()->willReturn($this->uploadApi->reveal());
2626
$this->cloudinary->adminApi()->willReturn($this->adminApi->reveal());
2727

28-
$this->adapter = new CloudinaryStorageAdapter($this->cloudinary->reveal(), 'prefix');
28+
$this->adapter = new CloudinaryStorageAdapter($this->cloudinary->reveal());
2929
});
3030

3131
it('can copy a file', function () {
3232
$this->uploadApi->explicit(
33-
Argument::exact('source'),
34-
Argument::that(fn ($args) => $args['public_id'] === 'destination' && $args['type'] === 'upload')
33+
Argument::exact('./source'),
34+
Argument::any(),
3535
)->willReturn(createApiResponse(['public_id' => 'destination']))->shouldBeCalled();
3636

3737
$this->adapter->copy('source.jpg', 'destination.jpg', new Config);
3838
});
3939

4040
it('can delete a file', function () {
4141
$this->uploadApi->destroy(
42-
Argument::exact('test-file'),
43-
Argument::exact(['resource_type' => 'image'])
42+
Argument::exact('Fixtures/test-file'),
43+
Argument::any()
4444
)->willReturn(createApiResponse(['result' => 'ok']))->shouldBeCalled();
4545

46-
$this->adapter->delete('test-file.jpg');
46+
$this->adapter->delete('Fixtures/test-file.jpg');
4747
});
4848

4949
it('throws exception on delete failure', function () {
50-
$this->uploadApi->destroy('test-file', ['resource_type' => 'image'])
51-
->willReturn(createApiResponse(['result' => 'error', 'error' => 'Failed']))
52-
->shouldBeCalled();
50+
$this->uploadApi->destroy(
51+
Argument::exact('Fixtures/test-file'),
52+
Argument::any()
53+
)->willReturn(createApiResponse(['result' => 'error', 'error' => 'Failed']))->shouldBeCalled();
5354

54-
expect(fn () => $this->adapter->delete('test-file.jpg'))
55+
expect(fn () => $this->adapter->delete('Fixtures/test-file.jpg'))
5556
->toThrow(UnableToDeleteFile::class);
5657
});
5758

5859
it('can delete a directory', function () {
5960
$this->adminApi->deleteAssetsByPrefix(
60-
Argument::exact('test-dir')
61+
Argument::exact('Fixtures/test-dir')
6162
)->willReturn(createApiResponse(['result' => 'ok']))->shouldBeCalled();
6263

63-
$this->adapter->deleteDirectory('test-dir');
64+
$this->adapter->deleteDirectory('Fixtures/test-dir');
6465
});
6566

6667
it('can check if file exists', function () {
6768
$this->adminApi->asset(
68-
Argument::exact('test-file'),
69-
Argument::exact(['resource_type' => 'image'])
70-
)->willReturn(createApiResponse(['public_id' => 'test-file']))->shouldBeCalled();
69+
Argument::exact('Fixtures/test-file'),
70+
Argument::any()
71+
)->willReturn(createApiResponse([
72+
'public_id' => 'Fixtures/test-file',
73+
'bytes' => 1234,
74+
'secure_url' => 'https://example.com/test-file',
75+
]))->shouldBeCalled();
7176

72-
expect($this->adapter->fileExists('test-file.jpg'))->toBeTrue();
77+
expect($this->adapter->fileExists('Fixtures/test-file.jpg'))->toBeTrue();
7378
});
7479

7580
it('handles non-existent files', function () {
7681
$this->adminApi->asset(
77-
Argument::exact('test-file'),
78-
Argument::exact(['resource_type' => 'image'])
82+
Argument::exact('Fixtures/test-file'),
83+
Argument::any()
7984
)->willThrow(new Exception('Not found'))->shouldBeCalled();
8085

81-
expect($this->adapter->fileExists('test-file.jpg'))->toBeFalse();
86+
expect($this->adapter->fileExists('Fixtures/test-file.jpg'))->toBeFalse();
8287
});
8388

8489
it('can get file size', function () {
8590
$this->adminApi->asset(
86-
Argument::exact('test-file'),
87-
Argument::exact(['resource_type' => 'image'])
88-
)->willReturn(createApiResponse(['bytes' => 1234]))->shouldBeCalled();
91+
Argument::exact('Fixtures/test-file'),
92+
Argument::any()
93+
)->willReturn(createApiResponse([
94+
'public_id' => 'Fixtures/test-file',
95+
'bytes' => 1234,
96+
'secure_url' => 'https://example.com/test-file',
97+
]))->shouldBeCalled();
8998

90-
$size = $this->adapter->fileSize('test-file.jpg');
99+
$size = $this->adapter->fileSize('Fixtures/test-file.jpg');
91100
expect($size->fileSize())->toBe(1234);
92101
});
93102

@@ -96,23 +105,23 @@ function createApiResponse(array $data): ApiResponse
96105
$expectedTimestamp = strtotime($date);
97106

98107
$this->adminApi->asset(
99-
Argument::exact('test-file'),
100-
Argument::exact(['resource_type' => 'image'])
108+
Argument::exact('Fixtures/test-file'),
109+
Argument::any()
101110
)->willReturn(createApiResponse([
102-
'public_id' => 'test-file',
111+
'public_id' => 'Fixtures/test-file',
103112
'created_at' => $date,
104113
'bytes' => 1234,
105114
]))->shouldBeCalled();
106115

107-
$time = $this->adapter->lastModified('test-file.jpg');
116+
$time = $this->adapter->lastModified('Fixtures/test-file.jpg');
108117
expect($time->lastModified())->toBe($expectedTimestamp);
109118
});
110119

111120
it('can list contents', function () {
112121
$response = createApiResponse([
113122
'resources' => [
114123
[
115-
'public_id' => 'test-file',
124+
'public_id' => 'Fixtures/test-file',
116125
'bytes' => 1234,
117126
'created_at' => '2023-01-01',
118127
],
@@ -131,33 +140,33 @@ function createApiResponse(array $data): ApiResponse
131140

132141
it('can read a file', function () {
133142
$this->adminApi->asset(
134-
Argument::exact('test-file'),
135-
Argument::exact(['resource_type' => 'image'])
143+
Argument::exact('Fixtures/test-file'),
144+
Argument::any()
136145
)->willReturn(createApiResponse([
137146
'secure_url' => __DIR__.'/test.jpg',
138147
]))->shouldBeCalled();
139148

140149
// Create a test file
141150
file_put_contents(__DIR__.'/test.jpg', 'test content');
142151

143-
$content = $this->adapter->read('test-file.jpg');
152+
$content = $this->adapter->read('Fixtures/test-file.jpg');
144153
expect($content)->toBe('test content');
145154

146155
unlink(__DIR__.'/test.jpg');
147156
});
148157

149158
it('can read a file as stream', function () {
150159
$this->adminApi->asset(
151-
Argument::exact('test-file'),
152-
Argument::exact(['resource_type' => 'image'])
160+
Argument::exact('Fixtures/test-file'),
161+
Argument::any()
153162
)->willReturn(createApiResponse([
154163
'secure_url' => __DIR__.'/test.jpg',
155164
]))->shouldBeCalled();
156165

157166
// Create a test file
158167
file_put_contents(__DIR__.'/test.jpg', 'test content');
159168

160-
$stream = $this->adapter->readStream('test-file.jpg');
169+
$stream = $this->adapter->readStream('Fixtures/test-file.jpg');
161170
expect($stream)->toBeResource();
162171
expect(stream_get_contents($stream))->toBe('test content');
163172

@@ -171,15 +180,16 @@ function createApiResponse(array $data): ApiResponse
171180

172181
it('can move a file', function () {
173182
$this->uploadApi->explicit(
174-
Argument::exact('source'),
175-
Argument::that(fn ($args) => $args['public_id'] === 'destination' && $args['type'] === 'upload')
183+
Argument::exact('Fixtures/source'),
184+
Argument::any(),
176185
)->willReturn(createApiResponse(['public_id' => 'destination']))->shouldBeCalled();
177186

178-
$this->uploadApi->destroy('source', ['resource_type' => 'image'])
179-
->willReturn(createApiResponse(['result' => 'ok']))
180-
->shouldBeCalled();
187+
$this->uploadApi->destroy(
188+
Argument::exact('Fixtures/source'),
189+
Argument::any()
190+
)->willReturn(createApiResponse(['result' => 'ok']))->shouldBeCalled();
181191

182-
$this->adapter->move('source.jpg', 'destination.jpg', new Config);
192+
$this->adapter->move('Fixtures/source.jpg', 'Fixtures/destination.jpg', new Config);
183193
});
184194

185195
it('can calculate checksum', function () {

0 commit comments

Comments
 (0)