Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InitialTranslationSourceTest.php
Go to the documentation of this file.
1 <?php
7 
14 use Magento\Translation\Model\ResourceModel\TranslateFactory;
16 
22 class InitialTranslationSourceTest extends \PHPUnit\Framework\TestCase
23 {
27  private $translationFactory;
28 
32  private $translation;
33 
37  private $storeManager;
38 
42  private $store;
43 
47  private $connection;
48 
52  private $select;
53 
57  private $deploymentConfigMock;
58 
62  private $source;
63 
64  public function setUp()
65  {
66  $this->translationFactory = $this->getMockBuilder(TranslateFactory::class)
67  ->disableOriginalConstructor()
68  ->setMethods(['create'])
69  ->getMock();
70  $this->translation = $this->getMockBuilder(Translate::class)
71  ->disableOriginalConstructor()
72  ->getMock();
73  $this->storeManager = $this->getMockBuilder(StoreManager::class)
74  ->disableOriginalConstructor()
75  ->getMock();
76  $this->store = $this->getMockBuilder(Store::class)
77  ->disableOriginalConstructor()
78  ->getMock();
79  $this->connection = $this->getMockBuilder(AdapterInterface::class)
80  ->disableOriginalConstructor()
81  ->getMock();
82  $this->select = $this->getMockBuilder(Select::class)
83  ->disableOriginalConstructor()
84  ->getMock();
85  $this->deploymentConfigMock = $this->getMockBuilder(DeploymentConfig::class)
86  ->disableOriginalConstructor()
87  ->getMock();
88 
89  $this->source = new InitialTranslationSource(
90  $this->translationFactory,
91  $this->storeManager,
92  $this->deploymentConfigMock
93  );
94  }
95 
96  public function testGet()
97  {
98  $this->deploymentConfigMock->expects($this->once())
99  ->method('isDbAvailable')
100  ->willReturn(true);
101  $this->translationFactory->expects($this->once())
102  ->method('create')
103  ->willReturn($this->translation);
104  $this->translation->expects($this->atLeastOnce())
105  ->method('getConnection')
106  ->willReturn($this->connection);
107  $this->connection->expects($this->once())
108  ->method('select')
109  ->willReturn($this->select);
110  $this->translation->expects($this->once())
111  ->method('getMainTable')
112  ->willReturn('main_table.translate');
113  $this->select->expects($this->once())
114  ->method('from')
115  ->with('main_table.translate', ['string', 'translate', 'store_id', 'locale'])
116  ->willReturnSelf();
117  $this->select->expects($this->once())
118  ->method('order')
119  ->with('store_id')
120  ->willReturnSelf();
121  $this->connection->expects($this->once())
122  ->method('fetchAll')
123  ->with($this->select)
124  ->willReturn([
125  [
126  'store_id' => 2,
127  'locale' => 'en_US',
128  'string' => 'hello',
129  'translate' => 'bonjour'
130  ]
131  ]);
132  $this->storeManager->expects($this->once())
133  ->method('getStore')
134  ->with(2)
135  ->willReturn($this->store);
136  $this->store->expects($this->once())
137  ->method('getCode')
138  ->willReturn('myStore');
139 
140  $this->assertEquals(
141  [
142  'en_US' => [
143  'myStore' => [
144  'hello' => 'bonjour'
145  ]
146  ]
147  ],
148  $this->source->get()
149  );
150  }
151 
152  public function testGetWithoutAvailableDb()
153  {
154  $this->deploymentConfigMock->expects($this->once())
155  ->method('isDbAvailable')
156  ->willReturn(false);
157  $this->assertEquals([], $this->source->get());
158  }
159 }