AdminExtractorTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. public function setUp()
  39. {
  40. if (!interface_exists('JMS\TranslationBundle\Translation\ExtractorInterface')) {
  41. $this->markTestSkipped('JMS Translator Bundle does not exist');
  42. }
  43. $this->fooAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  44. $this->barAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  45. // php 5.3 BC
  46. $fooAdmin = $this->fooAdmin;
  47. $barAdmin = $this->barAdmin;
  48. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  49. $container->expects($this->any())
  50. ->method('get')
  51. ->will($this->returnCallback(function ($id) use ($fooAdmin, $barAdmin) {
  52. switch ($id) {
  53. case 'foo_admin':
  54. return $fooAdmin;
  55. case 'bar_admin':
  56. return $barAdmin;
  57. }
  58. return;
  59. }));
  60. $logger = $this->getMock('Psr\Log\LoggerInterface');
  61. $this->pool = new Pool($container, '', '');
  62. $this->pool->setAdminServiceIds(array('foo_admin', 'bar_admin'));
  63. $this->adminExtractor = new AdminExtractor($this->pool, $logger);
  64. $this->adminExtractor->setLogger($logger);
  65. }
  66. public function testExtractEmpty()
  67. {
  68. $catalogue = $this->adminExtractor->extract();
  69. $this->assertInstanceOf('JMS\TranslationBundle\Model\MessageCatalogue', $catalogue);
  70. $this->assertFalse($catalogue->has(new Message('foo', 'foo_admin_domain')));
  71. }
  72. public function testExtract()
  73. {
  74. // php 5.3 BC
  75. $translator = $this->adminExtractor;
  76. $tester = $this;
  77. $this->fooAdmin->expects($this->any())
  78. ->method('getShow')
  79. ->will($this->returnCallback(function () use ($translator, $tester) {
  80. $tester->assertEquals('foo', $translator->trans('foo', array(), 'foo_admin_domain'));
  81. $tester->assertEquals('foo', $translator->transChoice('foo', 1, array(), 'foo_admin_domain'));
  82. return;
  83. }));
  84. $catalogue = $this->adminExtractor->extract();
  85. $this->assertTrue($catalogue->has(new Message('foo', 'foo_admin_domain')));
  86. $this->assertFalse($catalogue->has(new Message('nonexistent', 'foo_admin_domain')));
  87. $this->assertInstanceOf('JMS\TranslationBundle\Model\Message', $catalogue->get('foo', 'foo_admin_domain'));
  88. $message = $catalogue->get('foo', 'foo_admin_domain');
  89. $this->assertSame('foo', $message->getId());
  90. $this->assertSame('foo_admin_domain', $message->getDomain());
  91. }
  92. public function testExtractWithException()
  93. {
  94. $this->setExpectedException('RuntimeException', 'Foo throws exception');
  95. $this->fooAdmin->expects($this->any())
  96. ->method('getShow')
  97. ->will($this->returnCallback(function () {
  98. throw new \RuntimeException('Foo throws exception');
  99. }));
  100. $this->adminExtractor->extract();
  101. }
  102. }