AdminExtractorTest.php 3.9 KB

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