Skip to content
Snippets Groups Projects
Commit ee0b3f6d authored by Matt Glaman's avatar Matt Glaman Committed by Matt Glaman
Browse files

Issue #3094423 by mglaman: Use data providers to simplify IndexResourceTest

parent c8f42380
No related branches found
No related tags found
No related merge requests found
......@@ -65,103 +65,112 @@ final class IndexResourceTest extends BrowserTestBase {
/**
* Tests the results contain the index values.
*
* @param array $query
* The URL query params.
* @param int $expected_count
* The expected document count.
* @param array $expected_ids
* The expected entity IDs.
* @param array $expected_links_keys
* The expected pagination link keys.
*
* @dataProvider noQueryDataProvider
* @dataProvider paginationDataProvider
* @dataProvider fulltextDataProvider
*/
public function testCollection() {
$url = Url::fromRoute('jsonapi_search_api.index_database_search_index');
public function testCollection(array $query, int $expected_count, array $expected_ids, array $expected_links_keys): void {
$request_options = [];
$request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
$url = Url::fromRoute('jsonapi_search_api.index_database_search_index', [], [
'query' => $query,
]);
$response = $this->request('GET', $url, $request_options);
$this->assertSame(200, $response->getStatusCode(), var_export(Json::decode((string) $response->getBody()), TRUE));
$response_document = Json::decode((string) $response->getBody());
$this->assertCount(count($this->entities), $response_document['data'], var_export($response_document, TRUE));
$this->assertSame(array_keys($this->entities), array_map(static function (array $data) {
$this->assertCount($expected_count, $response_document['data'], var_export($response_document, TRUE));
$this->assertSame($expected_ids, array_map(static function (array $data) {
return $data['attributes']['drupal_internal__id'];
}, $response_document['data']));
foreach ($expected_links_keys as $links_key) {
$this->assertArrayHasKey($links_key, $response_document['links'], var_export($response_document['links'], TRUE));
}
}
/**
* Tests the results contain the index values.
* No query data provider.
*
* @return \Generator
* The data.
*/
public function testCollectionPagination() {
$request_options = [];
$request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
public function noQueryDataProvider(): \Generator {
yield [
[],
5,
[1, 2, 3, 4, 5],
[],
];
}
$url = Url::fromRoute('jsonapi_search_api.index_database_search_index', [], [
'query' => [
/**
* Pagination data provider.
*
* @return \Generator
* The data.
*/
public function paginationDataProvider(): \Generator {
yield [
[
'page' => [
'limit' => 2,
'offset' => 0,
],
],
]);
$response = $this->request('GET', $url, $request_options);
$this->assertSame(200, $response->getStatusCode(), var_export(Json::decode((string) $response->getBody()), TRUE));
$response_document = Json::decode((string) $response->getBody());
$this->assertCount(2, $response_document['data'], var_export($response_document, TRUE));
$this->assertSame([1, 2], array_map(static function (array $data) {
return $data['attributes']['drupal_internal__id'];
}, $response_document['data']));
$this->assertArrayHasKey('next', $response_document['links']);
$this->assertArrayHasKey('last', $response_document['links']);
$url = Url::fromRoute('jsonapi_search_api.index_database_search_index', [], [
'query' => [
2,
[1, 2],
['next', 'last'],
];
yield [
[
'page' => [
'limit' => 2,
'offset' => 2,
],
],
]);
$response = $this->request('GET', $url, $request_options);
$this->assertSame(200, $response->getStatusCode(), var_export(Json::decode((string) $response->getBody()), TRUE));
$response_document = Json::decode((string) $response->getBody());
$this->assertCount(2, $response_document['data'], var_export($response_document, TRUE));
$this->assertSame([3, 4], array_map(static function (array $data) {
return $data['attributes']['drupal_internal__id'];
}, $response_document['data']));
$this->assertArrayHasKey('next', $response_document['links']);
$this->assertArrayHasKey('last', $response_document['links']);
$this->assertArrayHasKey('prev', $response_document['links']);
$this->assertArrayHasKey('first', $response_document['links']);
2,
[3, 4],
['next', 'last', 'prev', 'first'],
];
}
/**
* Tests the results contain the index values.
* Fulltext data provider.
*
* @return \Generator
* The data.
*/
public function testCollectionFulltext() {
$url = Url::fromRoute('jsonapi_search_api.index_database_search_index', [], [
'query' => [
public function fulltextDataProvider(): \Generator {
yield [
[
'filter' => [
'fulltext' => 'föö',
],
],
]);
$request_options = [];
$request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
$response = $this->request('GET', $url, $request_options);
$this->assertSame(200, $response->getStatusCode(), var_export(Json::decode((string) $response->getBody()), TRUE));
$response_document = Json::decode((string) $response->getBody());
$this->assertCount(1, $response_document['data'], var_export($response_document, TRUE));
$this->assertSame([1], array_map(static function (array $data) {
return $data['attributes']['drupal_internal__id'];
}, $response_document['data']));
$url = Url::fromRoute('jsonapi_search_api.index_database_search_index', [], [
'query' => [
1,
[1],
[],
];
yield [
[
'filter' => [
'fulltext' => 'foo',
],
],
]);
$request_options = [];
$request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
$response = $this->request('GET', $url, $request_options);
$this->assertSame(200, $response->getStatusCode(), var_export(Json::decode((string) $response->getBody()), TRUE));
$response_document = Json::decode((string) $response->getBody());
$this->assertCount(4, $response_document['data'], var_export($response_document, TRUE));
$this->assertSame([1, 2, 4, 5], array_map(static function (array $data) {
return $data['attributes']['drupal_internal__id'];
}, $response_document['data']));
4,
[1, 2, 4, 5],
[],
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment