ExporterTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Tests\Filter;
  11. use Sonata\AdminBundle\Export\Exporter;
  12. use Exporter\Source\SourceIteratorInterface;
  13. use Exporter\Source\ArraySourceIterator;
  14. use Symfony\Component\HttpFoundation\Response;
  15. class ExporterTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @expectedException RuntimeException
  19. */
  20. public function testFilter()
  21. {
  22. $source = $this->getMock('Exporter\Source\SourceIteratorInterface');
  23. $exporter = new Exporter();
  24. $exporter->getResponse('foo', 'foo', $source);
  25. }
  26. /**
  27. * @dataProvider getGetResponseTests
  28. */
  29. public function testGetResponse($format, $filename, $contentType)
  30. {
  31. $source = new ArraySourceIterator(array(
  32. array('foo' => 'bar')
  33. ));
  34. $exporter = new Exporter();
  35. $response = $exporter->getResponse($format, $filename, $source);
  36. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  37. $this->assertEquals($contentType, $response->headers->get('Content-Type'));
  38. // Quotes does not appear on some sonata versions.
  39. $this->assertRegExp('/attachment; filename="?'.$filename.'"?/', $response->headers->get('Content-Disposition'));
  40. }
  41. public function getGetResponseTests()
  42. {
  43. return array(
  44. array('json', 'foo.json', 'application/json'),
  45. array('xml', 'foo.xml', 'text/xml'),
  46. array('xls', 'foo.xls', 'application/vnd.ms-excel'),
  47. array('csv', 'foo.csv', 'text/csv'),
  48. );
  49. }
  50. }