AdminGeneratorTest.php 3.2 KB

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