Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
J
jsonapi_search_api-3487310
Manage
Activity
Members
Labels
Plan
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Issue forks
jsonapi_search_api-3487310
Commits
ee0b3f6d
Commit
ee0b3f6d
authored
5 years ago
by
Matt Glaman
Committed by
Matt Glaman
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/src/Functional/IndexResourceTest.php
+71
-62
71 additions, 62 deletions
tests/src/Functional/IndexResourceTest.php
with
71 additions
and
62 deletions
tests/src/Functional/IndexResourceTest.php
+
71
−
62
View file @
ee0b3f6d
...
...
@@ -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
],
[],
];
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment