Skip to content

added create file response to factory #118

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bin
vendor
Tests/app/cache
.idea
composer.lock
34 changes: 34 additions & 0 deletions Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Liuggio\ExcelBundle;

use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\StreamedResponse;

/**
Expand Down Expand Up @@ -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
*
Expand Down
19 changes: 19 additions & 0 deletions Tests/Controller/FakeControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down
20 changes: 20 additions & 0 deletions Tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
Expand Down
16 changes: 16 additions & 0 deletions Tests/app/Controller/FakeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;

class FakeController extends Controller
{
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Tests/app/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down