Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit aecca69

Browse files
committed
Test that a DownloadResponse can be created from a valid CSV string
1 parent 2088793 commit aecca69

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

src/Response/DownloadResponse.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
namespace Zend\Diactoros\Response;
1111

1212
use Zend\Diactoros\Exception\InvalidArgumentException;
13+
use Zend\Diactoros\Response;
1314

15+
use Zend\Diactoros\Stream;
1416
use function array_keys;
1517
use function array_merge;
1618
use function implode;
@@ -37,6 +39,44 @@ class DownloadResponse extends Response
3739
'pragma'
3840
];
3941

42+
/**
43+
* DownloadResponse constructor.
44+
* @param $body
45+
* @param int $status
46+
* @param string $filename
47+
* @param array $headers
48+
*/
49+
public function __construct($body, int $status = 200, string $filename = '', array $headers = [])
50+
{
51+
$content = new Stream('php://temp', 'wb+');
52+
$content->write($body);
53+
$content->rewind();
54+
55+
$headers = $this->prepareDownloadHeaders($filename, $headers);
56+
57+
parent::__construct($content, $status, $headers);
58+
}
59+
60+
/**
61+
* Get download headers
62+
*
63+
* @param string $filename
64+
* @return array
65+
*/
66+
private function getDownloadHeaders(string $filename): array
67+
{
68+
$headers = [];
69+
$headers['cache-control'] = ['must-revalidate'];
70+
$headers['content-description'] = ['File Transfer'];
71+
$headers['content-disposition'] = [sprintf('attachment; filename=%s', $filename)];
72+
$headers['content-transfer-encoding'] = ['Binary'];
73+
$headers['content-type'] = ['text/csv; charset=utf-8'];
74+
$headers['expires'] = ['0'];
75+
$headers['pragma'] = ['Public'];
76+
77+
return $headers;
78+
}
79+
4080
/**
4181
* Check if the extra headers contain any of the download headers
4282
*
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4+
* @copyright Copyright (c) 2015-2018 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace ZendTest\Diactoros\Response;
11+
12+
use org\bovigo\vfs\vfsStream;
13+
use PHPUnit\Framework\TestCase;
14+
use Zend\Diactoros\Response;
15+
use Zend\Diactoros\Response\DownloadResponse;
16+
17+
class DownloadResponseTest extends TestCase
18+
{
19+
/**
20+
* @var \org\bovigo\vfs\vfsStreamDirectory
21+
*/
22+
private $root;
23+
24+
public function setUp()
25+
{
26+
$validCSVString = <<<EOF
27+
name,address,email,street,city,state,country
28+
Victoria Freeman,1770 Urfe Pass,[email protected],Ipziw Grove,Uzfujnic,SD,KN
29+
Kathryn Reynolds,861 Nircu Mill,[email protected],Kaid Court,Botpupe,CA,WS
30+
Dora Schmidt,703 Laufo Heights,[email protected],Sowdu Key,Pojzudiz,FL,TN
31+
Delia Harrison,1409 Abeeli Loop,[email protected],Dulgi Heights,Eswooke,MN,DZ
32+
Jordan Lane,1409 Zihoce Plaza,[email protected],Acfuh View,Dulwawa,NY,NF
33+
Danny Holmes,502 Fubjib Parkway,[email protected],Apsa Loop,Etaweza,CO,MQ
34+
Clarence Brewer,1085 Jacpa View,[email protected],Baroh Highway,Tikawi,WA,LS
35+
Elmer Cohen,1556 Netjol Heights,[email protected],Dacbap Trail,Kihiguk,UT,WS
36+
EOF;
37+
38+
$directoryStructure = [
39+
'files' => [
40+
'empty.csv' => "",
41+
'valid.csv' => $validCSVString,
42+
'non-readable-file.csv' => vfsStream::newFile('non-readable-file.csv', 0000)
43+
]
44+
];
45+
$this->root = vfsStream::setup('root', null, $directoryStructure);
46+
}
47+
48+
public function testCanCreateResponseFromString()
49+
{
50+
$csvString = file_get_contents($this->root->url() . '/files/valid.csv');
51+
$response = new DownloadResponse($csvString);
52+
$this->assertInstanceOf(Response::class, $response);
53+
$this->assertEquals($csvString, (string) $response->getBody()->getContents());
54+
$this->assertArrayHasKey('cache-control', $response->getHeaders());
55+
}
56+
}

0 commit comments

Comments
 (0)