StoreManagerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Test\Unit\Model;
  7. use Magento\Framework\App\DeploymentConfig;
  8. class StoreManagerTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Store\Model\StoreManager
  12. */
  13. protected $model;
  14. /**
  15. * @var \Magento\Store\Api\StoreRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $storeRepositoryMock;
  18. /**
  19. * @var \Magento\Store\Api\StoreResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $storeResolverMock;
  22. protected function setUp()
  23. {
  24. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  25. $this->storeRepositoryMock = $this->getMockBuilder(\Magento\Store\Api\StoreRepositoryInterface::class)
  26. ->disableOriginalConstructor()
  27. ->setMethods([])
  28. ->getMock();
  29. $this->storeResolverMock = $this->getMockBuilder(\Magento\Store\Api\StoreResolverInterface::class)
  30. ->disableOriginalConstructor()
  31. ->setMethods([])
  32. ->getMock();
  33. $this->model = $objectManager->getObject(
  34. \Magento\Store\Model\StoreManager::class,
  35. [
  36. 'storeRepository' => $this->storeRepositoryMock,
  37. 'storeResolver' => $this->storeResolverMock
  38. ]
  39. );
  40. }
  41. public function testGetStoreEmptyParameter()
  42. {
  43. $storeId = 1;
  44. $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
  45. ->disableOriginalConstructor()
  46. ->setMethods([])
  47. ->getMock();
  48. $this->storeResolverMock->expects($this->any())->method('getCurrentStoreId')->willReturn($storeId);
  49. $this->storeRepositoryMock->expects($this->atLeastOnce())
  50. ->method('getById')
  51. ->with($storeId)
  52. ->willReturn($storeMock);
  53. $this->assertInstanceOf(\Magento\Store\Api\Data\StoreInterface::class, $this->model->getStore());
  54. $this->assertEquals($storeMock, $this->model->getStore());
  55. }
  56. public function testGetStoreStringParameter()
  57. {
  58. $storeId = 'store_code';
  59. $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
  60. ->disableOriginalConstructor()
  61. ->setMethods([])
  62. ->getMock();
  63. $this->storeRepositoryMock->expects($this->atLeastOnce())
  64. ->method('get')
  65. ->with($storeId)
  66. ->willReturn($storeMock);
  67. $actualStore = $this->model->getStore($storeId);
  68. $this->assertInstanceOf(\Magento\Store\Api\Data\StoreInterface::class, $actualStore);
  69. $this->assertEquals($storeMock, $actualStore);
  70. }
  71. public function testGetStoreObjectStoreParameter()
  72. {
  73. $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
  74. ->disableOriginalConstructor()
  75. ->setMethods([])
  76. ->getMock();
  77. $actualStore = $this->model->getStore($storeMock);
  78. $this->assertInstanceOf(\Magento\Store\Api\Data\StoreInterface::class, $actualStore);
  79. $this->assertEquals($storeMock, $actualStore);
  80. }
  81. /**
  82. * @dataProvider getStoresDataProvider
  83. */
  84. public function testGetStores($storesList, $withDefault, $codeKey, $expectedStores)
  85. {
  86. $this->storeRepositoryMock->expects($this->any())->method('getList')->willReturn($storesList);
  87. $this->assertEquals($expectedStores, $this->model->getStores($withDefault, $codeKey));
  88. }
  89. /**
  90. * @return array
  91. */
  92. public function getStoresDataProvider()
  93. {
  94. $defaultStoreMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
  95. ->disableOriginalConstructor()
  96. ->setMethods([])
  97. ->getMock();
  98. $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
  99. ->disableOriginalConstructor()
  100. ->setMethods([])
  101. ->getMock();
  102. $defaultStoreMock->expects($this->any())->method('getId')->willReturn(0);
  103. $defaultStoreMock->expects($this->any())->method('getCode')->willReturn('default');
  104. $storeMock->expects($this->any())->method('getId')->willReturn(1);
  105. $storeMock->expects($this->any())->method('getCode')->willReturn('first_store');
  106. return [
  107. 'withoutDefaultAndId' => [
  108. 'storesList' => [$defaultStoreMock, $storeMock],
  109. 'withDefault' => false,
  110. 'codeKey' => false,
  111. 'expectedStores' => [1 => $storeMock]
  112. ],
  113. 'withoutDefaultAndCodeKey' => [
  114. 'storesList' => [$defaultStoreMock,$storeMock],
  115. 'withDefault' => false,
  116. 'codeKey' => true,
  117. 'expectedStores' => ['first_store' => $storeMock]
  118. ],
  119. 'withDefaultAndId' => [
  120. 'storesList' => [$defaultStoreMock,$storeMock],
  121. 'withDefault' => true,
  122. 'codeKey' => false,
  123. 'expectedStores' => [0 => $defaultStoreMock, 1 => $storeMock]
  124. ],
  125. 'withDefaultAndCodeKey' => [
  126. 'storesList' => [$defaultStoreMock,$storeMock],
  127. 'withDefault' => true,
  128. 'codeKey' => true,
  129. 'expectedStores' => ['default' => $defaultStoreMock, 'first_store' => $storeMock]
  130. ],
  131. ];
  132. }
  133. }