ControllerGeneratorTest.php 2.7 KB

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