AdminExporterTest.php 2.1 KB

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