AdminExporterTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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\Bridge\Exporter;
  11. use Exporter\Exporter;
  12. use Sonata\AdminBundle\Bridge\Exporter\AdminExporter;
  13. use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
  14. class AdminExporterTest extends PHPUnit_Framework_TestCase
  15. {
  16. public function provideExportFormats()
  17. {
  18. return array(
  19. 'no override' => array(array('xls'), array('json', 'xml', 'csv', 'xls'), array('xls')),
  20. 'override in admin' => array(array('csv'), array('csv'), array('xls')),
  21. );
  22. }
  23. /**
  24. * @dataProvider provideExportFormats
  25. */
  26. public function testAdminHasPriorityOverGlobalSettings(array $expectedFormats, array $adminFormats, array $globalFormats)
  27. {
  28. $writers = array();
  29. foreach ($globalFormats as $exportFormat) {
  30. $writer = $this->createMock('Exporter\Writer\TypedWriterInterface');
  31. $writer->expects($this->once())
  32. ->method('getFormat')
  33. ->will($this->returnValue($exportFormat));
  34. $writers[] = $writer;
  35. }
  36. $exporter = new Exporter($writers);
  37. $admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
  38. $admin->expects($this->once())
  39. ->method('getExportFormats')
  40. ->will($this->returnValue($adminFormats));
  41. $adminExporter = new AdminExporter($exporter);
  42. $this->assertSame($expectedFormats, $adminExporter->getAvailableFormats($admin));
  43. }
  44. public function testGetExportFilename()
  45. {
  46. $admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
  47. $admin->expects($this->once())
  48. ->method('getClass')
  49. ->will($this->returnValue('MyProject\AppBundle\Model\MyClass'));
  50. $adminExporter = new AdminExporter(new Exporter());
  51. $this->assertRegexp(
  52. '#export_myclass_\d{4}_\d{2}_\d{2}_\d{2}_\d{2}_\d{2}.csv#',
  53. $adminExporter->getExportFilename($admin, 'csv')
  54. );
  55. }
  56. }