Skip to content

Commit

Permalink
Merge #516
Browse files Browse the repository at this point in the history
516: [DX] Add support for PHPUnit 10.1 r=brunoocasali a=stloyd

# Pull Request

## Related issue
Fixes n/a

## What does this PR do?

Update PHPUnit to version 10.1 & adjust tests, it's an internal tool so no BC breaks.

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: Joseph Bielawski <[email protected]>
  • Loading branch information
meili-bors[bot] and stloyd authored Jun 7, 2023
2 parents 1087cfa + d6661f4 commit 37a82b0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 78 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"http-interop/http-factory-guzzle": "Factory for guzzlehttp/guzzle"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^9.5 || ^10.1",
"friendsofphp/php-cs-fixer": "^3.0",
"guzzlehttp/guzzle": "^7.1",
"http-interop/http-factory-guzzle": "^1.0",
Expand Down
4 changes: 3 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
bootstrap="vendor/autoload.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
Expand Down
8 changes: 4 additions & 4 deletions tests/Endpoints/DocumentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ public function testAddDocumentsCsvInBatchesWithDelimiter(): void
->method('addDocumentsCsv')
->willReturnCallback(function (string $param) use ($matcher): void {
// withConsecutive has no replacement https://github.com/sebastianbergmann/phpunit/issues/4026
// @phpstan-ignore-next-line
switch ($matcher->numberOfInvocations()) {
case 1:
$this->assertEquals($param, ["id;title\n888221515;Young folks", null, ';']);
Expand Down Expand Up @@ -803,7 +802,6 @@ public function testUpdateDocumentsCsvInBatchesWithDelimiter(): void
->method('updateDocumentsCsv')
->willReturnCallback(function (string $param) use ($matcher): void {
// withConsecutive has no replacement https://github.com/sebastianbergmann/phpunit/issues/4026
// @phpstan-ignore-next-line
switch ($matcher->numberOfInvocations()) {
case 1:
$this->assertEquals($param, ["id;title\n888221515;Young folks", null, ';']);
Expand Down Expand Up @@ -872,7 +870,7 @@ public function testDeletingDocumentWithInvalidId($documentId): void
$index->deleteDocument($documentId);
}

public function invalidDocumentIds(): array
public static function invalidDocumentIds(): array
{
return [
'documentId as null' => [null],
Expand All @@ -888,9 +886,11 @@ public function invalidDocumentIds(): array
private function findDocumentWithId($documents, $documentId)
{
foreach ($documents as $document) {
if ($document['id'] == $documentId) {
if ($document['id'] === $documentId) {
return $document;
}
}

return null;
}
}
90 changes: 25 additions & 65 deletions tests/Http/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,29 +211,24 @@ public function testClientHasDefaultUserAgent(): void
$httpClient = $this->createHttpClientMock(200, '{}');
$reqFactory = $this->createMock(RequestFactoryInterface::class);
$requestStub = $this->createMock(RequestInterface::class);

/* @phpstan-ignore-next-line */
$requestStub->expects($this->any())
$requestStub->expects($this->exactly(2))
->method('withAddedHeader')
->withConsecutive(
[
$this->equalTo('User-Agent'),
$this->equalTo(Meilisearch::qualifiedVersion()),
],
[
$this->equalTo('Authorization'),
$this->equalTo('Bearer masterKey'),
],
)
->willReturnOnConsecutiveCalls($requestStub, $requestStub);

$reqFactory->expects($this->any())
->willReturnCallback(function ($name, $value) use ($requestStub) {
if ('User-Agent' === $name) {
$this->assertSame(Meilisearch::qualifiedVersion(), $value);
} elseif ('Authorization' === $name) {
$this->assertSame('Bearer masterKey', $value);
}

return $requestStub;
});
$reqFactory->expects($this->once())
->method('createRequest')
->willReturn($requestStub);

$client = new \Meilisearch\Client('http://localhost:7070', 'masterKey', $httpClient, $reqFactory);

$client->health();
$this->assertTrue($client->isHealthy());
}

public function testClientHasCustomUserAgent(): void
Expand All @@ -242,59 +237,24 @@ public function testClientHasCustomUserAgent(): void
$httpClient = $this->createHttpClientMock(200, '{}');
$reqFactory = $this->createMock(RequestFactoryInterface::class);
$requestStub = $this->createMock(RequestInterface::class);

/* @phpstan-ignore-next-line */
$requestStub->expects($this->any())
$requestStub->expects($this->exactly(2))
->method('withAddedHeader')
->withConsecutive(
[
$this->equalTo('User-Agent'),
$this->equalTo($customAgent.';'.Meilisearch::qualifiedVersion()),
],
[
$this->equalTo('Authorization'),
$this->equalTo('Bearer masterKey'),
],
)
->willReturnOnConsecutiveCalls($requestStub, $requestStub);

$reqFactory->expects($this->any())
->willReturnCallback(function ($name, $value) use ($requestStub, $customAgent) {
if ('User-Agent' === $name) {
$this->assertSame($customAgent.';'.Meilisearch::qualifiedVersion(), $value);
} elseif ('Authorization' === $name) {
$this->assertSame('Bearer masterKey', $value);
}

return $requestStub;
});
$reqFactory->expects($this->once())
->method('createRequest')
->willReturn($requestStub);

$client = new \Meilisearch\Client('http://localhost:7070', 'masterKey', $httpClient, $reqFactory, [$customAgent]);

$client->health();
}

public function testClientHasEmptyCustomUserAgentArray(): void
{
$httpClient = $this->createHttpClientMock(200, '{}');
$reqFactory = $this->createMock(RequestFactoryInterface::class);
$requestStub = $this->createMock(RequestInterface::class);

/* @phpstan-ignore-next-line */
$requestStub->expects($this->any())
->method('withAddedHeader')
->withConsecutive(
[
$this->equalTo('User-Agent'),
$this->equalTo(Meilisearch::qualifiedVersion()),
],
[
$this->equalTo('Authorization'),
$this->equalTo('Bearer masterKey'),
],
)
->willReturnOnConsecutiveCalls($requestStub, $requestStub);

$reqFactory->expects($this->any())
->method('createRequest')
->willReturn($requestStub);

$client = new \Meilisearch\Client('http://localhost:7070', 'masterKey', $httpClient, $reqFactory, []);

$client->health();
$this->assertTrue($client->isHealthy());
}

public function testParseResponseReturnsNullForNoContent(): void
Expand All @@ -318,7 +278,7 @@ public function testParseResponseReturnsNullForNoContent(): void
$this->assertNull($result);
}

public function provideStatusCodes(): iterable
public static function provideStatusCodes(): iterable
{
yield [200];
yield [300];
Expand Down
22 changes: 15 additions & 7 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected function tearDown(): void
$tasks = [];

foreach ($indexes as $index) {
array_push($tasks, $index->delete()['taskUid']);
$tasks[] = $index->delete()['taskUid'];
}

$this->client->waitForTasks($tasks);
Expand All @@ -75,9 +75,13 @@ public function assertFinitePagination(array $response): void
$validBody = ['hitsPerPage', 'totalHits', 'totalPages', 'page', 'processingTimeMs', 'query', 'hits'];

foreach ($validBody as $value) {
$this->assertTrue(
\in_array($value, $currentKeys, true),
'Not a valid finite pagination response, since the "'.$value.'" key is not present in: ['.implode(', ', $currentKeys).']'
$this->assertContains(
$value,
$currentKeys,
'Not a valid finite pagination response, since the "'.$value.'" key is not present in: ['.implode(
', ',
$currentKeys
).']'
);
}
}
Expand All @@ -88,9 +92,13 @@ public function assertEstimatedPagination(array $response): void
$validBody = ['offset', 'limit', 'estimatedTotalHits', 'processingTimeMs', 'query', 'hits'];

foreach ($validBody as $value) {
$this->assertTrue(
\in_array($value, $currentKeys, true),
'Not a valid estimated pagination response, since the "'.$value.'" key is not present in: ['.implode(', ', $currentKeys).']'
$this->assertContains(
$value,
$currentKeys,
'Not a valid estimated pagination response, since the "'.$value.'" key is not present in: ['.implode(
', ',
$currentKeys
).']'
);
}
}
Expand Down

0 comments on commit 37a82b0

Please sign in to comment.