Skip to content

Commit 2125387

Browse files
committed
Upgrade to Pest 4 and refactor tests for PHPUnit 12
- Replace getMockBuilder() with createMock() (PHPUnit 12 compat) - Remove mockChunkedUploadClient and skipped chunked upload tests - Replace willReturnOnConsecutiveCalls/throwException with callback - Drop PHP 8.1/8.2 from CI matrices (Pest 4 requires PHP 8.3+)
1 parent 41856f0 commit 2125387

File tree

5 files changed

+38
-133
lines changed

5 files changed

+38
-133
lines changed

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
strategy:
1212
fail-fast: true
1313
matrix:
14-
php: [8.2, '8.3', '8.4', '8.5']
14+
php: ['8.3', '8.4', '8.5']
1515
stability: [lowest, highest]
1616

1717
name: PHP ${{ matrix.php }} - ${{ matrix.stability }} deps

.github/workflows/static-analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212

1313
strategy:
1414
matrix:
15-
php-version: ['8.2', '8.3', '8.4', '8.5']
15+
php-version: ['8.3', '8.4', '8.5']
1616

1717
steps:
1818
- name: Checkout code

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
},
3333
"require-dev": {
3434
"laravel/pint": "^1.10.1",
35-
"pestphp/pest": "^2.6.3",
35+
"pestphp/pest": "^4.0",
3636
"phpstan/phpstan": "^1.10.16"
3737
},
3838
"autoload": {

tests/ClientTest.php

Lines changed: 33 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@
141141
});
142142

143143
it('can download a file', function () {
144-
$expectedResponse = $this->getMockBuilder(StreamInterface::class)
145-
->getMock();
144+
$expectedResponse = $this->createMock(StreamInterface::class);
146145
$expectedResponse->expects($this->once())
147146
->method('isReadable')
148147
->willReturn(true);
@@ -165,8 +164,7 @@
165164
});
166165

167166
it('can download a folder as zip', function () {
168-
$expectedResponse = $this->getMockBuilder(StreamInterface::class)
169-
->getMock();
167+
$expectedResponse = $this->createMock(StreamInterface::class);
170168
$expectedResponse->expects($this->once())
171169
->method('isReadable')
172170
->willReturn(true);
@@ -231,8 +229,7 @@
231229
});
232230

233231
it('can get a thumbnail', function () {
234-
$expectedResponse = $this->getMockBuilder(StreamInterface::class)
235-
->getMock();
232+
$expectedResponse = $this->createMock(StreamInterface::class);
236233

237234
$mockGuzzle = $this->mockGuzzleRequest(
238235
$expectedResponse,
@@ -404,38 +401,6 @@
404401
->and($uploadSessionCursor->offset)->toBe(32);
405402
});
406403

407-
it('can upload a file string chunked', function () {
408-
$content = 'chunk0chunk1chunk2rest';
409-
$mockClient = $this->mockChunkedUploadClient($content, 6);
410-
411-
expect($mockClient->uploadChunked('Homework/math/answers.txt', $content, 'add', 6))
412-
->toBe(['name' => 'answers.txt']);
413-
})->skip('Must fix method "mockChunkedUploadClient".');
414-
415-
it('can upload a file resource chunked', function () {
416-
$content = 'chunk0chunk1chunk2rest';
417-
$resource = fopen('php://memory', 'r+');
418-
fwrite($resource, $content);
419-
rewind($resource);
420-
421-
$mockClient = $this->mockChunkedUploadClient($content, 6);
422-
423-
expect($mockClient->uploadChunked('Homework/math/answers.txt', $resource, 'add', 6))
424-
->toBe(['name' => 'answers.txt']);
425-
})->skip('Must fix method "mockChunkedUploadClient".');
426-
427-
it('can upload a tiny file chunked', function () {
428-
$content = 'smallerThenChunkSize';
429-
$resource = fopen('php://memory', 'r+');
430-
fwrite($resource, $content);
431-
rewind($resource);
432-
433-
$mockClient = $this->mockChunkedUploadClient($content, 21);
434-
435-
expect($mockClient->uploadChunked('Homework/math/answers.txt', $resource, 'add', 21))
436-
->toBe(['name' => 'answers.txt']);
437-
})->skip('Must fix method "mockChunkedUploadClient".');
438-
439404
it('can finish an upload session', function () {
440405
$mockGuzzle = $this->mockGuzzleRequest(
441406
json_encode([
@@ -531,15 +496,13 @@
531496
});
532497

533498
test('content endpoint request can throw exception', function () {
534-
$mockGuzzle = $this->getMockBuilder(GuzzleClient::class)
535-
->onlyMethods(['request'])
536-
->getMock();
499+
$mockGuzzle = $this->createMock(GuzzleClient::class);
537500
$mockGuzzle->expects($this->once())
538501
->method('request')
539502
->willThrowException(new ClientException(
540503
'there was an error',
541-
$this->getMockBuilder(RequestInterface::class)->getMock(),
542-
$this->getMockBuilder(ResponseInterface::class)->getMock(),
504+
$this->createMock(RequestInterface::class),
505+
$this->createMock(ResponseInterface::class),
543506
));
544507

545508
$client = new Client('test_token', $mockGuzzle);
@@ -552,16 +515,14 @@
552515
'getToken' => 'test_token',
553516
]);
554517

555-
$mockGuzzle = $this->getMockBuilder(GuzzleClient::class)
556-
->onlyMethods(['request'])
557-
->getMock();
518+
$mockGuzzle = $this->createMock(GuzzleClient::class);
558519

559520
$mockGuzzle->expects($this->exactly(2))
560521
->method('request')
561522
->willThrowException($e = new ClientException(
562523
'there was an error',
563-
$this->getMockBuilder(RequestInterface::class)->getMock(),
564-
$this->getMockBuilder(ResponseInterface::class)->getMock(),
524+
$this->createMock(RequestInterface::class),
525+
$this->createMock(ResponseInterface::class),
565526
));
566527

567528
$tokenProvider->expects($this->once())
@@ -575,21 +536,17 @@
575536
})->throws(ClientException::class);
576537

577538
test('rpc endpoint request can throw exception with 400 status code', function () {
578-
$mockResponse = $this->getMockBuilder(ResponseInterface::class)
579-
->getMock();
580-
$mockResponse->expects($this->any())
581-
->method('getStatusCode')
582-
->willReturn(400);
539+
$mockResponse = $this->createConfiguredMock(ResponseInterface::class, [
540+
'getStatusCode' => 400,
541+
]);
583542

584-
$mockGuzzle = $this->getMockBuilder(GuzzleClient::class)
585-
->onlyMethods(['request'])
586-
->getMock();
543+
$mockGuzzle = $this->createMock(GuzzleClient::class);
587544

588545
$mockGuzzle->expects($this->once())
589546
->method('request')
590547
->willThrowException(new ClientException(
591548
'there was an error',
592-
$this->getMockBuilder(RequestInterface::class)->getMock(),
549+
$this->createMock(RequestInterface::class),
593550
$mockResponse,
594551
));
595552

@@ -606,24 +563,18 @@
606563
'error_summary' => 'Human readable error code',
607564
];
608565

609-
$mockResponse = $this->getMockBuilder(ResponseInterface::class)
610-
->getMock();
611-
$mockResponse->expects($this->any())
612-
->method('getStatusCode')
613-
->willReturn(409);
614-
$mockResponse->expects($this->any())
615-
->method('getBody')
616-
->willReturn($this->createStreamFromString(json_encode($body)));
566+
$mockResponse = $this->createConfiguredMock(ResponseInterface::class, [
567+
'getStatusCode' => 409,
568+
'getBody' => $this->createStreamFromString(json_encode($body)),
569+
]);
617570

618-
$mockGuzzle = $this->getMockBuilder(GuzzleClient::class)
619-
->onlyMethods(['request'])
620-
->getMock();
571+
$mockGuzzle = $this->createMock(GuzzleClient::class);
621572

622573
$mockGuzzle->expects($this->once())
623574
->method('request')
624575
->willThrowException(new ClientException(
625576
'there was an error',
626-
$this->getMockBuilder(RequestInterface::class)->getMock(),
577+
$this->createMock(RequestInterface::class),
627578
$mockResponse,
628579
));
629580

@@ -649,15 +600,13 @@
649600
'getBody' => $this->createStreamFromString(json_encode($body)),
650601
]);
651602

652-
$mockGuzzle = $this->getMockBuilder(GuzzleClient::class)
653-
->onlyMethods(['request'])
654-
->getMock();
603+
$mockGuzzle = $this->createMock(GuzzleClient::class);
655604

656605
$mockGuzzle->expects($this->exactly(2))
657606
->method('request')
658607
->willThrowException($e = new ClientException(
659608
'there was an error',
660-
$this->getMockBuilder(RequestInterface::class)->getMock(),
609+
$this->createMock(RequestInterface::class),
661610
$mockResponse,
662611
));
663612

@@ -697,22 +646,25 @@
697646
'getBody' => $this->createStreamFromString(json_encode($successBody)),
698647
]);
699648

700-
$mockGuzzle = $this->getMockBuilder(GuzzleClient::class)
701-
->onlyMethods(['request'])
702-
->getMock();
649+
$mockGuzzle = $this->createMock(GuzzleClient::class);
703650

704651
$e = new ClientException(
705652
'there was an error',
706-
$this->getMockBuilder(RequestInterface::class)->getMock(),
653+
$this->createMock(RequestInterface::class),
707654
$errorResponse,
708655
);
709656

657+
$callCount = 0;
710658
$mockGuzzle->expects($this->exactly(2))
711659
->method('request')
712-
->willReturnOnConsecutiveCalls(
713-
$this->throwException($e),
714-
$successResponse,
715-
);
660+
->willReturnCallback(function () use (&$callCount, $e, $successResponse) {
661+
$callCount++;
662+
if ($callCount === 1) {
663+
throw $e;
664+
}
665+
666+
return $successResponse;
667+
});
716668

717669
$tokenProvider->expects($this->once())
718670
->method('refresh')

tests/TestCase.php

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Psr\Http\Message\ResponseInterface;
1010
use Psr\Http\Message\StreamInterface;
1111
use Spatie\Dropbox\Client;
12-
use Spatie\Dropbox\UploadSessionCursor;
1312

1413
abstract class TestCase extends BaseTestCase
1514
{
@@ -18,8 +17,7 @@ abstract class TestCase extends BaseTestCase
1817
*/
1918
public function mockGuzzleRequest(string|StreamInterface|null $expectedResponse, string $expectedEndpoint, array $expectedParams): MockObject&GuzzleClient
2019
{
21-
$mockResponse = $this->getMockBuilder(ResponseInterface::class)
22-
->getMock();
20+
$mockResponse = $this->createMock(ResponseInterface::class);
2321

2422
if ($expectedResponse) {
2523
if (is_string($expectedResponse)) {
@@ -33,9 +31,7 @@ public function mockGuzzleRequest(string|StreamInterface|null $expectedResponse,
3331
}
3432
}
3533

36-
$mockGuzzle = $this->getMockBuilder(GuzzleClient::class)
37-
->onlyMethods(['request'])
38-
->getMock();
34+
$mockGuzzle = $this->createMock(GuzzleClient::class);
3935
$mockGuzzle->expects($this->once())
4036
->method('request')
4137
->with('POST', $expectedEndpoint, $expectedParams)
@@ -44,49 +40,6 @@ public function mockGuzzleRequest(string|StreamInterface|null $expectedResponse,
4440
return $mockGuzzle;
4541
}
4642

47-
public function mockChunkedUploadClient(string $content, int $chunkSize): MockObject&Client
48-
{
49-
$chunks = str_split($content, $chunkSize);
50-
51-
$mockClient = $this->getMockBuilder(Client::class)
52-
->setConstructorArgs(['test_token'])
53-
->setMethodsExcept(['uploadChunked', 'upload'])
54-
->getMock();
55-
56-
$mockClient->expects($this->once())
57-
->method('uploadSessionStart')
58-
->with(array_shift($chunks))
59-
->willReturn(new UploadSessionCursor('mockedSessionId', $chunkSize));
60-
61-
$mockClient->expects($this->once())
62-
->method('uploadSessionFinish')
63-
->with('', $this->anything(), 'Homework/math/answers.txt', 'add')
64-
->willReturn(['name' => 'answers.txt']);
65-
66-
$remainingChunks = count($chunks);
67-
$offset = $chunkSize;
68-
69-
if ($remainingChunks) {
70-
$withs = [];
71-
$returns = [];
72-
73-
foreach ($chunks as $chunk) {
74-
$offset += $chunkSize;
75-
$withs[] = [$chunk, $this->anything()];
76-
$returns[] = new UploadSessionCursor('mockedSessionId', $offset);
77-
}
78-
79-
$mockClient->expects($this->exactly($remainingChunks))
80-
->method('uploadSessionAppend')
81-
->withConsecutive(...$withs)
82-
->willReturn(...$returns);
83-
}
84-
85-
\assert($mockClient instanceof Client);
86-
87-
return $mockClient;
88-
}
89-
9043
public function createStreamFromString(string $content): Stream
9144
{
9245
$resource = fopen('php://memory', 'r+');

0 commit comments

Comments
 (0)