Skip to content

Change logic for converting GET-params to a filename #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ Please see the following table for some examples.
| | | /path/to/fixtures/example.com/api/articles/1.mock |
| http://example.com/api/articles | POST | /path/to/fixtures/example.com/api/articles.post.mock |
| | | /path/to/fixtures/example.com/api/articles.mock |
| http://example.com/api/comments?query=json | GET | /path/to/fixtures/example.com/api/comments.query-json.get.mock |
| | | /path/to/fixtures/example.com/api/comments.query-json.mock |
| http://example.com/api/comments?query=json | GET | /path/to/fixtures/example.com/api/comments.query=json.get.mock |
| | | /path/to/fixtures/example.com/api/comments.query=json.mock |
| | | /path/to/fixtures/example.com/api/comments.get.mock |
| | | /path/to/fixtures/example.com/api/comments.mock |
| http://example.com/api/comments?query=json&order=id | GET | /path/to/fixtures/example.com/api/comments.order-id-query-json.get.mock |
| | | /path/to/fixtures/example.com/api/comments.order-id-query-json.mock |
| http://example.com/api/comments?query=json&order=id | GET | /path/to/fixtures/example.com/api/comments.order=id&query=json.get.mock |
| | | /path/to/fixtures/example.com/api/comments.order=id&query=json.mock |
| | | /path/to/fixtures/example.com/api/comments.get.mock |
| | | /path/to/fixtures/example.com/api/comments.mock |

Expand Down
18 changes: 9 additions & 9 deletions src/ResponseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,22 +225,22 @@ protected function getMethodFromRequest(RequestInterface $request): string

/**
* @param \Psr\Http\Message\RequestInterface $request
* @param string $separator
* @param string $replacement
*
* @return string
*/
protected function getQueryFromRequest(RequestInterface $request, $separator = '-'): string
protected function getQueryFromRequest(RequestInterface $request, $replacement = '-'): string
{
$query = urldecode($request->getUri()->getQuery());
$parts = array_map(
function (string $part) use ($separator) {
return str_replace('=', $separator, $part);
},
explode('&', $query)
);
$parts = explode('&', $query);
sort($parts);
$query = implode('&', $parts);

return Stringy::create(implode($separator, $parts))->slugify($separator);
return (string)Stringy::create(str_replace(['\\', '/', '?', ':', '*', '"', '>', '<', '|'], $replacement, $query))
->toAscii()
->delimit($replacement)
->removeLeft($replacement)
->removeRight($replacement);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions tests/ResponseBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,18 @@ public function getResponses(): array
['http://example.com/api/articles', 'GET', 'example.com/api/articles.mock'],
// Nested
['http://example.com/api/articles/1', 'GET', 'example.com/api/articles/1.mock'],
// With query
['http://example.com/api/comments?query=json', 'GET', 'example.com/api/comments.query-json.mock'],
// With simple query
['http://example.com/api/comments?query=json', 'GET', 'example.com/api/comments.query=json.mock'],
// With complex query
['http://example.com/api/comments?query=json&foo[]=bar&foo[]=baz', 'GET', 'example.com/api/comments.foo[]=bar&foo[]=baz&query=json.mock'],
// With query fallback
['http://example.com/api/comments?foo=bar', 'GET', 'example.com/api/comments.mock'],
// With method
['http://example.com/api/people', 'GET', 'example.com/api/people.get.mock'],
// With method fallback
['http://example.com/api/people', 'POST', 'example.com/api/people.mock'],
// With query and method
['http://example.com/api/tags?query=json', 'POST', 'example.com/api/tags.query-json.post.mock'],
['http://example.com/api/tags?query=json', 'POST', 'example.com/api/tags.query=json.post.mock'],
// With query and method fallback
['http://example.com/api/tags?foo=bar', 'GET', 'example.com/api/tags.mock'],
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
comments-foo-bar-foo-baz-query-json