Skip to content

Commit

Permalink
Added filename match type to download assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
mickedplay committed May 28, 2024
1 parent 3d59d90 commit 1136154
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,10 @@ public function assertLocation($uri)
* Assert that the response offers a file download.
*
* @param string|null $filename
* @param string $matchType
* @return $this
*/
public function assertDownload($filename = null)
public function assertDownload($filename = null, $matchType = 'exact')
{
$contentDisposition = explode(';', $this->headers->get('content-disposition', ''));

Expand All @@ -358,13 +359,23 @@ public function assertDownload($filename = null)
if (! isset($contentDisposition[1])) {
PHPUnit::fail($message);
} else {
PHPUnit::assertSame(
$filename,
isset(explode('=', $contentDisposition[1])[1])
? trim(explode('=', $contentDisposition[1])[1], " \"'")
: '',
$message
);
$actual = isset(explode('=', $contentDisposition[1])[1])
? trim(explode('=', $contentDisposition[1])[1], " \"'")
: '';

switch($matchType) {
case 'exact':
PHPUnit::assertSame($filename, $actual, $message);
break;
case 'startsWith':
PHPUnit::assertStringStartsWith($filename, $actual, $message);
break;
case 'endsWith':
PHPUnit::assertStringEndsWith($filename, $actual, $message);
break;
default:
throw new \InvalidArgumentException('Invalid match type: ' . $matchType);
}

return $this;
}
Expand Down

0 comments on commit 1136154

Please sign in to comment.