AdminGeneratorTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\Generator;
  11. use Sonata\AdminBundle\Generator\AdminGenerator;
  12. use Sonata\AdminBundle\Model\ModelManagerInterface;
  13. use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
  14. use Symfony\Component\Filesystem\Filesystem;
  15. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  16. /**
  17. * @author Marek Stipek <mario.dweller@seznam.cz>
  18. */
  19. class AdminGeneratorTest extends PHPUnit_Framework_TestCase
  20. {
  21. /** @var AdminGenerator */
  22. private $adminGenerator;
  23. /** @var BundleInterface|\PHPUnit_Framework_MockObject_MockObject */
  24. private $bundleMock;
  25. /** @var string */
  26. private $bundlePath;
  27. /**
  28. * {@inheritdoc}
  29. */
  30. protected function setUp()
  31. {
  32. $this->adminGenerator = new AdminGenerator(
  33. $this->createModelManagerMock(),
  34. __DIR__.'/../../Resources/skeleton'
  35. );
  36. $this->bundleMock = $this->createBundleMock();
  37. $this->bundlePath = $this->bundleMock->getPath();
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected function tearDown()
  43. {
  44. $filesystem = new Filesystem();
  45. $filesystem->remove($this->bundlePath);
  46. }
  47. public function testGenerate()
  48. {
  49. $this->adminGenerator->generate($this->bundleMock, 'ModelAdmin', 'Model');
  50. $file = $this->adminGenerator->getFile();
  51. $this->assertSame('Sonata\AdminBundle\Tests\Fixtures\Admin\ModelAdmin', $this->adminGenerator->getClass());
  52. $this->assertSame('ModelAdmin.php', basename($file));
  53. $this->assertFileEquals(__DIR__.'/../Fixtures/Admin/ModelAdmin.php', $file);
  54. $this->expectException('\RuntimeException', 'already exists');
  55. $this->adminGenerator->generate($this->bundleMock, 'ModelAdmin', 'Model');
  56. }
  57. /**
  58. * @return ModelManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  59. */
  60. private function createModelManagerMock()
  61. {
  62. $modelManagerMock = $this->getMockForAbstractClass('Sonata\AdminBundle\Model\ModelManagerInterface');
  63. $modelManagerMock
  64. ->expects($this->any())
  65. ->method('getExportFields')
  66. ->with('Model')
  67. ->will($this->returnValue(array('foo', 'bar', 'baz')))
  68. ;
  69. return $modelManagerMock;
  70. }
  71. /**
  72. * @return BundleInterface|\PHPUnit_Framework_MockObject_MockObject
  73. */
  74. private function createBundleMock()
  75. {
  76. $bundleMock = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\Bundle\BundleInterface');
  77. $bundleMock
  78. ->expects($this->any())
  79. ->method('getNamespace')
  80. ->will($this->returnValue('Sonata\AdminBundle\Tests\Fixtures'))
  81. ;
  82. $bundleMock
  83. ->expects($this->any())
  84. ->method('getPath')
  85. ->will($this->returnValue(sprintf('%s/%s', sys_get_temp_dir(), lcg_value())))
  86. ;
  87. return $bundleMock;
  88. }
  89. }