AdminExtractorTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\Translator\Extractor\JMSTranslatorBundle;
  11. use JMS\TranslationBundle\Model\Message;
  12. use Sonata\AdminBundle\Admin\AdminInterface;
  13. use Sonata\AdminBundle\Admin\Pool;
  14. use Sonata\AdminBundle\Translator\Extractor\JMSTranslatorBundle\AdminExtractor;
  15. /**
  16. * Test for AdminExtractor.
  17. *
  18. * @author Andrej Hudec <pulzarraider@gmail.com>
  19. */
  20. class AdminExtractorTest extends \PHPUnit_Framework_TestCase
  21. {
  22. /**
  23. * @var AdminExtractor
  24. */
  25. private $adminExtractor;
  26. /**
  27. * @var Pool
  28. */
  29. private $pool;
  30. /**
  31. * @var AdminInterface
  32. */
  33. private $fooAdmin;
  34. /**
  35. * @var AdminInterface
  36. */
  37. private $barAdmin;
  38. /**
  39. * @var BreadcrumbsBuilderInterface
  40. */
  41. private $breadcrumbsBuilder;
  42. public function setUp()
  43. {
  44. if (!interface_exists('JMS\TranslationBundle\Translation\ExtractorInterface')) {
  45. $this->markTestSkipped('JMS Translator Bundle does not exist');
  46. }
  47. $this->fooAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  48. $this->barAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  49. // php 5.3 BC
  50. $fooAdmin = $this->fooAdmin;
  51. $barAdmin = $this->barAdmin;
  52. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  53. $container->expects($this->any())
  54. ->method('get')
  55. ->will($this->returnCallback(function ($id) use ($fooAdmin, $barAdmin) {
  56. switch ($id) {
  57. case 'foo_admin':
  58. return $fooAdmin;
  59. case 'bar_admin':
  60. return $barAdmin;
  61. }
  62. return;
  63. }));
  64. $logger = $this->getMock('Psr\Log\LoggerInterface');
  65. $this->pool = $this->getMockBuilder('Sonata\AdminBundle\Admin\Pool')
  66. ->disableOriginalConstructor()
  67. ->getMock()
  68. ;
  69. $this->pool->expects($this->any())
  70. ->method('getAdminServiceIds')
  71. ->will($this->returnValue(array('foo_admin', 'bar_admin')));
  72. $this->pool->expects($this->any())
  73. ->method('getContainer')
  74. ->will($this->returnValue($container));
  75. $this->adminExtractor = new AdminExtractor($this->pool, $logger);
  76. $this->adminExtractor->setLogger($logger);
  77. $this->breadcrumbsBuilder = $this->getMock('Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface');
  78. $this->adminExtractor->setBreadcrumbsBuilder($this->breadcrumbsBuilder);
  79. }
  80. public function testExtractEmpty()
  81. {
  82. $catalogue = $this->adminExtractor->extract();
  83. $this->assertInstanceOf('JMS\TranslationBundle\Model\MessageCatalogue', $catalogue);
  84. $this->assertFalse($catalogue->has(new Message('foo', 'foo_admin_domain')));
  85. }
  86. public function testExtract()
  87. {
  88. // php 5.3 BC
  89. $translator = $this->adminExtractor;
  90. $tester = $this;
  91. $this->fooAdmin->expects($this->any())
  92. ->method('getShow')
  93. ->will($this->returnCallback(function () use ($translator, $tester) {
  94. $tester->assertEquals('foo', $translator->trans('foo', array(), 'foo_admin_domain'));
  95. $tester->assertEquals('foo', $translator->transChoice('foo', 1, array(), 'foo_admin_domain'));
  96. return;
  97. }));
  98. $catalogue = $this->adminExtractor->extract();
  99. $this->assertTrue($catalogue->has(new Message('foo', 'foo_admin_domain')));
  100. $this->assertFalse($catalogue->has(new Message('nonexistent', 'foo_admin_domain')));
  101. $this->assertInstanceOf('JMS\TranslationBundle\Model\Message', $catalogue->get('foo', 'foo_admin_domain'));
  102. $message = $catalogue->get('foo', 'foo_admin_domain');
  103. $this->assertSame('foo', $message->getId());
  104. $this->assertSame('foo_admin_domain', $message->getDomain());
  105. }
  106. public function testExtractWithException()
  107. {
  108. $this->setExpectedException('RuntimeException', 'Foo throws exception');
  109. $this->fooAdmin->expects($this->any())
  110. ->method('getShow')
  111. ->will($this->returnCallback(function () {
  112. throw new \RuntimeException('Foo throws exception');
  113. }));
  114. $this->adminExtractor->extract();
  115. }
  116. public function testExtractCallsBreadcrumbs()
  117. {
  118. $this->breadcrumbsBuilder->expects($this->exactly(2 * 6))
  119. ->method('getBreadcrumbs');
  120. $this->adminExtractor->extract();
  121. }
  122. }