DictionaryTest.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Test\Unit\Language;
  7. use Magento\Framework\App\Language\Dictionary;
  8. use Magento\Framework\Filesystem\DriverPool;
  9. class DictionaryTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Framework\App\Language\Dictionary
  13. */
  14. private $model;
  15. /**
  16. * @var \PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $readFactory;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $componentRegistrar;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $configFactory;
  27. protected function setUp()
  28. {
  29. $this->readFactory = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadFactory::class);
  30. $this->componentRegistrar = $this->createMock(\Magento\Framework\Component\ComponentRegistrar::class);
  31. $this->configFactory = $this->getMockBuilder(\Magento\Framework\App\Language\ConfigFactory::class)
  32. ->setMethods(['create'])
  33. ->disableOriginalConstructor()
  34. ->getMock();
  35. $this->model = new Dictionary($this->readFactory, $this->componentRegistrar, $this->configFactory);
  36. }
  37. public function testDictionaryGetter()
  38. {
  39. $csvFileName = 'abc.csv';
  40. $data = [['one', '1'], ['two', '2']];
  41. $expected = [];
  42. foreach ($data as $item) {
  43. $expected[$item[0]] = $item[1];
  44. }
  45. $file = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\File\ReadInterface::class);
  46. for ($i = 0, $count = count($data); $i < $count; $i++) {
  47. $file->expects($this->at($i))->method('readCsv')->will($this->returnValue($data[$i]));
  48. }
  49. $file->expects($this->at($i))->method('readCsv')->will($this->returnValue(false));
  50. $readMock = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
  51. $readMock->expects($this->any())->method('readFile')->willReturnMap([
  52. ['language.xml', $readMock],
  53. [$csvFileName, $file],
  54. ]);
  55. $readMock->expects($this->any())->method('openFile')->willReturn($file);
  56. $readMock->expects($this->any())->method('isExist')->willReturn(true);
  57. $readMock->expects($this->any())->method('search')->willReturn([$csvFileName]);
  58. $this->componentRegistrar->expects($this->once())->method('getPaths')->willReturn(['foo/en_us']);
  59. $this->componentRegistrar->expects($this->once())->method('getPath')->willReturn('foo/en_us');
  60. $this->readFactory->expects($this->any())->method("create")->willReturn($readMock);
  61. $languageConfig = $this->createMock(\Magento\Framework\App\Language\Config::class);
  62. $languageConfig->expects($this->any())->method('getCode')->will($this->returnValue('en_US'));
  63. $languageConfig->expects($this->any())->method('getVendor')->will($this->returnValue('foo'));
  64. $languageConfig->expects($this->any())->method('getPackage')->will($this->returnValue('en_us'));
  65. $languageConfig->expects($this->any())->method('getSortOrder')->will($this->returnValue(0));
  66. $languageConfig->expects($this->any())->method('getUses')->will($this->returnValue([]));
  67. $this->configFactory->expects($this->any())->method('create')->willReturn($languageConfig);
  68. $result = $this->model->getDictionary("en_US");
  69. $this->assertSame($expected, $result);
  70. }
  71. }