InitialTranslationSourceTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Translation\Test\Unit\Model\Source;
  7. use Magento\Framework\App\DeploymentConfig;
  8. use Magento\Framework\DB\Adapter\AdapterInterface;
  9. use Magento\Framework\DB\Select;
  10. use Magento\Store\Model\Store;
  11. use Magento\Store\Model\StoreManager;
  12. use Magento\Translation\Model\ResourceModel\Translate;
  13. use Magento\Translation\Model\ResourceModel\TranslateFactory;
  14. use Magento\Translation\Model\Source\InitialTranslationSource;
  15. /**
  16. * @covers \Magento\Translation\Model\Source\InitialTranslationSource
  17. * @package Magento\Translation\Test\Unit\Model\Source
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class InitialTranslationSourceTest extends \PHPUnit\Framework\TestCase
  21. {
  22. /**
  23. * @var TranslateFactory|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $translationFactory;
  26. /**
  27. * @var Translate|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $translation;
  30. /**
  31. * @var StoreManager|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $storeManager;
  34. /**
  35. * @var Store|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $store;
  38. /**
  39. * @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $connection;
  42. /**
  43. * @var Select|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $select;
  46. /**
  47. * @var DeploymentConfig | \PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $deploymentConfigMock;
  50. /**
  51. * @var InitialTranslationSource
  52. */
  53. private $source;
  54. public function setUp()
  55. {
  56. $this->translationFactory = $this->getMockBuilder(TranslateFactory::class)
  57. ->disableOriginalConstructor()
  58. ->setMethods(['create'])
  59. ->getMock();
  60. $this->translation = $this->getMockBuilder(Translate::class)
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $this->storeManager = $this->getMockBuilder(StoreManager::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $this->store = $this->getMockBuilder(Store::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->connection = $this->getMockBuilder(AdapterInterface::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $this->select = $this->getMockBuilder(Select::class)
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $this->deploymentConfigMock = $this->getMockBuilder(DeploymentConfig::class)
  76. ->disableOriginalConstructor()
  77. ->getMock();
  78. $this->source = new InitialTranslationSource(
  79. $this->translationFactory,
  80. $this->storeManager,
  81. $this->deploymentConfigMock
  82. );
  83. }
  84. public function testGet()
  85. {
  86. $this->deploymentConfigMock->expects($this->once())
  87. ->method('isDbAvailable')
  88. ->willReturn(true);
  89. $this->translationFactory->expects($this->once())
  90. ->method('create')
  91. ->willReturn($this->translation);
  92. $this->translation->expects($this->atLeastOnce())
  93. ->method('getConnection')
  94. ->willReturn($this->connection);
  95. $this->connection->expects($this->once())
  96. ->method('select')
  97. ->willReturn($this->select);
  98. $this->translation->expects($this->once())
  99. ->method('getMainTable')
  100. ->willReturn('main_table.translate');
  101. $this->select->expects($this->once())
  102. ->method('from')
  103. ->with('main_table.translate', ['string', 'translate', 'store_id', 'locale'])
  104. ->willReturnSelf();
  105. $this->select->expects($this->once())
  106. ->method('order')
  107. ->with('store_id')
  108. ->willReturnSelf();
  109. $this->connection->expects($this->once())
  110. ->method('fetchAll')
  111. ->with($this->select)
  112. ->willReturn([
  113. [
  114. 'store_id' => 2,
  115. 'locale' => 'en_US',
  116. 'string' => 'hello',
  117. 'translate' => 'bonjour'
  118. ]
  119. ]);
  120. $this->storeManager->expects($this->once())
  121. ->method('getStore')
  122. ->with(2)
  123. ->willReturn($this->store);
  124. $this->store->expects($this->once())
  125. ->method('getCode')
  126. ->willReturn('myStore');
  127. $this->assertEquals(
  128. [
  129. 'en_US' => [
  130. 'myStore' => [
  131. 'hello' => 'bonjour'
  132. ]
  133. ]
  134. ],
  135. $this->source->get()
  136. );
  137. }
  138. public function testGetWithoutAvailableDb()
  139. {
  140. $this->deploymentConfigMock->expects($this->once())
  141. ->method('isDbAvailable')
  142. ->willReturn(false);
  143. $this->assertEquals([], $this->source->get());
  144. }
  145. }