AdminExtractorTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. $this->pool->expects($this->any())
  69. ->method('getAdminServiceIds')
  70. ->will($this->returnValue(array('foo_admin', 'bar_admin')));
  71. $this->pool->expects($this->any())
  72. ->method('getContainer')
  73. ->will($this->returnValue($container));
  74. $this->pool->expects($this->any())
  75. ->method('getAdminGroups')
  76. ->will($this->returnValue(array('group' => array(
  77. 'label_catalogue' => 'admin_domain',
  78. ))));
  79. $this->adminExtractor = new AdminExtractor($this->pool, $logger);
  80. $this->adminExtractor->setLogger($logger);
  81. $this->breadcrumbsBuilder = $this->getMock('Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface');
  82. $this->adminExtractor->setBreadcrumbsBuilder($this->breadcrumbsBuilder);
  83. }
  84. public function testExtractEmpty()
  85. {
  86. $catalogue = $this->adminExtractor->extract();
  87. $this->assertInstanceOf('JMS\TranslationBundle\Model\MessageCatalogue', $catalogue);
  88. $this->assertFalse($catalogue->has(new Message('foo', 'foo_admin_domain')));
  89. }
  90. public function testExtract()
  91. {
  92. // php 5.3 BC
  93. $translator = $this->adminExtractor;
  94. $tester = $this;
  95. $this->fooAdmin->expects($this->any())
  96. ->method('getShow')
  97. ->will($this->returnCallback(function () use ($translator, $tester) {
  98. $tester->assertEquals('foo', $translator->trans('foo', array(), 'foo_admin_domain'));
  99. $tester->assertEquals('foo', $translator->transChoice('foo', 1, array(), 'foo_admin_domain'));
  100. return;
  101. }));
  102. $this->fooAdmin->expects($this->any())
  103. ->method('getLabel')
  104. ->willReturn('foo_label');
  105. $this->fooAdmin->expects($this->any())
  106. ->method('getTranslationDomain')
  107. ->willReturn('foo_admin_domain');
  108. $catalogue = $this->adminExtractor->extract();
  109. $this->assertCount(2, $catalogue->getDomains());
  110. $this->assertTrue($catalogue->has(new Message('foo', 'foo_admin_domain')));
  111. $this->assertFalse($catalogue->has(new Message('nonexistent', 'foo_admin_domain')));
  112. $this->assertInstanceOf('JMS\TranslationBundle\Model\Message', $catalogue->get('foo', 'foo_admin_domain'));
  113. $message = $catalogue->get('foo', 'foo_admin_domain');
  114. $this->assertSame('foo', $message->getId());
  115. $this->assertSame('foo_admin_domain', $message->getDomain());
  116. $this->assertTrue($catalogue->has(new Message('group', 'admin_domain')));
  117. $this->assertTrue($catalogue->has(new Message('foo_label', 'foo_admin_domain')));
  118. }
  119. public function testExtractWithException()
  120. {
  121. $this->setExpectedException('RuntimeException', 'Foo throws exception');
  122. $this->fooAdmin->expects($this->any())
  123. ->method('getShow')
  124. ->will($this->returnCallback(function () {
  125. throw new \RuntimeException('Foo throws exception');
  126. }));
  127. $this->adminExtractor->extract();
  128. }
  129. public function testExtractCallsBreadcrumbs()
  130. {
  131. $this->breadcrumbsBuilder->expects($this->exactly(2 * 6))
  132. ->method('getBreadcrumbs');
  133. $this->adminExtractor->extract();
  134. }
  135. }