AdminExtractorTest.php 5.5 KB

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