diff --git a/.gitignore b/.gitignore index b70ddad..81612e2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ bin vendor Tests/app/cache +.idea +composer.lock diff --git a/Factory.php b/Factory.php index bf5daa0..b6515c6 100644 --- a/Factory.php +++ b/Factory.php @@ -2,6 +2,8 @@ namespace Liuggio\ExcelBundle; +use Symfony\Component\HttpFoundation\BinaryFileResponse; +use Symfony\Component\HttpFoundation\ResponseHeaderBag; use Symfony\Component\HttpFoundation\StreamedResponse; /** @@ -87,6 +89,38 @@ function () use ($writer) { ); } + /** + * Create a File Response + * + * @param \PHPExcel_Writer_IWriter $writer + * @param string $filename + * @param int $status + * @param array $headers + * + * @return BinaryFileResponse + */ + public function createFileResponse(\PHPExcel_Writer_IWriter $writer, $filename, $status = 200, $headers = array()) + { + $tempFilename = @tempnam(\PHPExcel_Shared_File::sys_get_temp_dir(), 'phpxlstmp'); + $writer->save($tempFilename); + + $response = new BinaryFileResponse( + $tempFilename, + $status, + $headers + ); + + $response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8'); + $response->headers->set('Pragma', 'public'); + $dispositionHeader = $response->headers->makeDisposition( + ResponseHeaderBag::DISPOSITION_ATTACHMENT, + $filename + ); + $response->headers->set('Content-Disposition', $dispositionHeader); + + return $response; + } + /** * Create a PHPExcel Helper HTML Object * diff --git a/Tests/Controller/FakeControllerTest.php b/Tests/Controller/FakeControllerTest.php index fc3c2bf..a528227 100644 --- a/Tests/Controller/FakeControllerTest.php +++ b/Tests/Controller/FakeControllerTest.php @@ -3,6 +3,7 @@ namespace Liuggio\ExcelBundle\Tests\Controller; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -25,6 +26,24 @@ public function testStreamAction() $this->assertNotNull($content, 'Response should not be null'); } + public function testFileAction() + { + $client = static::createClient(); + + $client->request(Request::METHOD_GET, '/fake/file'); + + /** @var BinaryFileResponse $response */ + $response = $client->getResponse(); + $response->sendContent(); + $content = ob_get_contents(); + ob_clean(); + + $this->assertEquals(Response::HTTP_OK, $response->getStatusCode(), $content); + + $this->assertNotEmpty($content, 'Response should not be empty'); + $this->assertNotNull($content, 'Response should not be null'); + } + public function testReaderAction() { $client = static::createClient(); diff --git a/Tests/FactoryTest.php b/Tests/FactoryTest.php index 3a54b01..b075860 100644 --- a/Tests/FactoryTest.php +++ b/Tests/FactoryTest.php @@ -3,6 +3,9 @@ namespace Liuggio\ExcelBundle\Tests; use Liuggio\ExcelBundle\Factory; +use Prophecy\Argument; +use Prophecy\Prophecy\ObjectProphecy; +use Symfony\Component\HttpFoundation\BinaryFileResponse; class FactoryTest extends \PHPUnit_Framework_TestCase { @@ -35,6 +38,23 @@ public function testCreateStreamedResponse() $factory->createStreamedResponse($writer)->sendContent(); } + public function testCreateFileResponse() + { + $filename = 'testfilename'; + /** @var ObjectProphecy|\PHPExcel_Writer_IWriter $writer */ + $writer = $this->prophesize('\PHPExcel_Writer_IWriter'); + $writer->save(Argument::type('string'))->shouldBeCalled(); + + $factory = new Factory(); + $response = $factory->createFileResponse($writer->reveal(), $filename); + + $this->assertNotEmpty($response->getFile()); + $headers = $response->headers; + $this->assertStringStartsWith('text/vnd.ms-excel', $headers->get('Content-Type')); + $this->assertEquals('public', $headers->get('Pragma')); + $this->assertStringStartsWith('attachment; filename=', $headers->get('content-disposition')); + } + public function testCreateHelperHtml() { $factory = new Factory(); diff --git a/Tests/app/Controller/FakeController.php b/Tests/app/Controller/FakeController.php index b3e48f4..5364452 100644 --- a/Tests/app/Controller/FakeController.php +++ b/Tests/app/Controller/FakeController.php @@ -4,6 +4,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\ResponseHeaderBag; class FakeController extends Controller { @@ -24,6 +25,21 @@ public function streamAction() return $response; } + public function fileAction() + { + // create an empty object + $phpExcelObject = $this->createXSLObject(); + + $phpExcelFactory = $this->get('phpexcel'); + // create the writer + $writer = $phpExcelFactory->createWriter($phpExcelObject, 'Excel5'); + $filename = 'xls-'.uniqid().'.xls'; + // create the response + $response = $phpExcelFactory->createFileResponse($writer, $filename); + + return $response; + } + public function storeAction() { // create an empty object diff --git a/Tests/app/routing.yml b/Tests/app/routing.yml index d2e7ac5..ff8c1a8 100644 --- a/Tests/app/routing.yml +++ b/Tests/app/routing.yml @@ -2,6 +2,10 @@ fake_route_stream: path: /fake/stream defaults: { _controller: LiuggioExcelBundle:Fake:stream } +fake_route_file: + path: /fake/file + defaults: { _controller: LiuggioExcelBundle:Fake:file } + fake_route_store: path: /fake/store defaults: { _controller: LiuggioExcelBundle:Fake:store }