ControllerGeneratorTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\ControllerGenerator;
  12. use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
  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 ControllerGeneratorTest extends PHPUnit_Framework_TestCase
  19. {
  20. /** @var ControllerGenerator */
  21. private $controllerGenerator;
  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->controllerGenerator = new ControllerGenerator(__DIR__.'/../../Resources/skeleton');
  32. $this->bundleMock = $this->createBundleMock();
  33. $this->bundlePath = $this->bundleMock->getPath();
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. protected function tearDown()
  39. {
  40. $filesystem = new Filesystem();
  41. $filesystem->remove($this->bundlePath);
  42. }
  43. public function testGenerate()
  44. {
  45. $this->controllerGenerator->generate($this->bundleMock, 'ModelAdminController');
  46. $file = $this->controllerGenerator->getFile();
  47. $this->assertSame(
  48. 'Sonata\AdminBundle\Tests\Fixtures\Controller\ModelAdminController',
  49. $this->controllerGenerator->getClass()
  50. );
  51. $this->assertSame('ModelAdminController.php', basename($file));
  52. $this->assertFileEquals(__DIR__.'/../Fixtures/Controller/ModelAdminController.php', $file);
  53. $this->expectException('\RuntimeException', 'already exists');
  54. $this->controllerGenerator->generate($this->bundleMock, 'ModelAdminController');
  55. }
  56. /**
  57. * @return BundleInterface|\PHPUnit_Framework_MockObject_MockObject
  58. */
  59. private function createBundleMock()
  60. {
  61. $bundleMock = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\Bundle\BundleInterface');
  62. $bundleMock
  63. ->expects($this->any())
  64. ->method('getNamespace')
  65. ->will($this->returnValue('Sonata\AdminBundle\Tests\Fixtures'))
  66. ;
  67. $bundleMock
  68. ->expects($this->any())
  69. ->method('getPath')
  70. ->will($this->returnValue(sprintf('%s/%s', sys_get_temp_dir(), lcg_value())))
  71. ;
  72. return $bundleMock;
  73. }
  74. }