Skip to content

Commit dfdd799

Browse files
committed
added create file response to factory
ignore phpstorm folder and composer lock
1 parent a028c00 commit dfdd799

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
bin
22
vendor
33
Tests/app/cache
4+
.idea
5+
composer.lock

Factory.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Liuggio\ExcelBundle;
44

5+
use Symfony\Component\HttpFoundation\BinaryFileResponse;
6+
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
57
use Symfony\Component\HttpFoundation\StreamedResponse;
68

79
/**
@@ -87,6 +89,38 @@ function () use ($writer) {
8789
);
8890
}
8991

92+
/**
93+
* Create a File Response
94+
*
95+
* @param \PHPExcel_Writer_IWriter $writer
96+
* @param string $filename
97+
* @param int $status
98+
* @param array $headers
99+
*
100+
* @return BinaryFileResponse
101+
*/
102+
public function createFileResponse(\PHPExcel_Writer_IWriter $writer, $filename, $status = 200, $headers = array())
103+
{
104+
$tempFilename = @tempnam(\PHPExcel_Shared_File::sys_get_temp_dir(), 'phpxlstmp');
105+
$writer->save($tempFilename);
106+
107+
$response = new BinaryFileResponse(
108+
$tempFilename,
109+
$status,
110+
$headers
111+
);
112+
113+
$response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
114+
$response->headers->set('Pragma', 'public');
115+
$dispositionHeader = $response->headers->makeDisposition(
116+
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
117+
$filename
118+
);
119+
$response->headers->set('Content-Disposition', $dispositionHeader);
120+
121+
return $response;
122+
}
123+
90124
/**
91125
* Create a PHPExcel Helper HTML Object
92126
*

Tests/Controller/FakeControllerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Liuggio\ExcelBundle\Tests\Controller;
44

55
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6+
use Symfony\Component\HttpFoundation\BinaryFileResponse;
67
use Symfony\Component\HttpFoundation\Request;
78
use Symfony\Component\HttpFoundation\Response;
89

@@ -25,6 +26,24 @@ public function testStreamAction()
2526
$this->assertNotNull($content, 'Response should not be null');
2627
}
2728

29+
public function testFileAction()
30+
{
31+
$client = static::createClient();
32+
33+
$client->request(Request::METHOD_GET, '/fake/file');
34+
35+
/** @var BinaryFileResponse $response */
36+
$response = $client->getResponse();
37+
$response->sendContent();
38+
$content = ob_get_contents();
39+
ob_clean();
40+
41+
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode(), $content);
42+
43+
$this->assertNotEmpty($content, 'Response should not be empty');
44+
$this->assertNotNull($content, 'Response should not be null');
45+
}
46+
2847
public function testReaderAction()
2948
{
3049
$client = static::createClient();

Tests/FactoryTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Liuggio\ExcelBundle\Tests;
44

55
use Liuggio\ExcelBundle\Factory;
6+
use Prophecy\Argument;
7+
use Prophecy\Prophecy\ObjectProphecy;
8+
use Symfony\Component\HttpFoundation\BinaryFileResponse;
69

710
class FactoryTest extends \PHPUnit_Framework_TestCase
811
{
@@ -35,6 +38,23 @@ public function testCreateStreamedResponse()
3538
$factory->createStreamedResponse($writer)->sendContent();
3639
}
3740

41+
public function testCreateFileResponse()
42+
{
43+
$filename = 'testfilename';
44+
/** @var ObjectProphecy|\PHPExcel_Writer_IWriter $writer */
45+
$writer = $this->prophesize('\PHPExcel_Writer_IWriter');
46+
$writer->save(Argument::type('string'))->shouldBeCalled();
47+
48+
$factory = new Factory();
49+
$response = $factory->createFileResponse($writer->reveal(), $filename);
50+
51+
$this->assertNotEmpty($response->getFile());
52+
$headers = $response->headers;
53+
$this->assertStringStartsWith('text/vnd.ms-excel', $headers->get('Content-Type'));
54+
$this->assertEquals('public', $headers->get('Pragma'));
55+
$this->assertStringStartsWith('attachment; filename=', $headers->get('content-disposition'));
56+
}
57+
3858
public function testCreateHelperHtml()
3959
{
4060
$factory = new Factory();

Tests/app/Controller/FakeController.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
66
use Symfony\Component\HttpFoundation\Response;
7+
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
78

89
class FakeController extends Controller
910
{
@@ -24,6 +25,21 @@ public function streamAction()
2425
return $response;
2526
}
2627

28+
public function fileAction()
29+
{
30+
// create an empty object
31+
$phpExcelObject = $this->createXSLObject();
32+
33+
$phpExcelFactory = $this->get('phpexcel');
34+
// create the writer
35+
$writer = $phpExcelFactory->createWriter($phpExcelObject, 'Excel5');
36+
$filename = 'xls-'.uniqid().'.xls';
37+
// create the response
38+
$response = $phpExcelFactory->createFileResponse($writer, $filename);
39+
40+
return $response;
41+
}
42+
2743
public function storeAction()
2844
{
2945
// create an empty object

Tests/app/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ fake_route_stream:
22
path: /fake/stream
33
defaults: { _controller: LiuggioExcelBundle:Fake:stream }
44

5+
fake_route_file:
6+
path: /fake/file
7+
defaults: { _controller: LiuggioExcelBundle:Fake:file }
8+
59
fake_route_store:
610
path: /fake/store
711
defaults: { _controller: LiuggioExcelBundle:Fake:store }

0 commit comments

Comments
 (0)