|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Store\Test\Unit\Block\Store; |
| 8 | + |
| 9 | +use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection; |
| 10 | + |
| 11 | +class SwitcherTest extends \PHPUnit_Framework_TestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var \Magento\Store\Block\Store\Switcher |
| 15 | + */ |
| 16 | + protected $model; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject |
| 20 | + */ |
| 21 | + protected $scopeConfigMock; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 25 | + */ |
| 26 | + protected $storeManagerMock; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var \Magento\Store\Model\StoreFactory|\PHPUnit_Framework_MockObject_MockObject |
| 30 | + */ |
| 31 | + protected $storeFactoryMock; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var \Magento\Store\Model\GroupFactory|\PHPUnit_Framework_MockObject_MockObject |
| 35 | + */ |
| 36 | + protected $storeGroupFactoryMock; |
| 37 | + |
| 38 | + protected function setUp() |
| 39 | + { |
| 40 | + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); |
| 41 | + $this->scopeConfigMock = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface') |
| 42 | + ->disableOriginalConstructor() |
| 43 | + ->setMethods([]) |
| 44 | + ->getMock(); |
| 45 | + $this->storeManagerMock = $this->getMockBuilder('Magento\Store\Model\StoreManagerInterface') |
| 46 | + ->disableOriginalConstructor() |
| 47 | + ->setMethods([]) |
| 48 | + ->getMock(); |
| 49 | + $this->storeFactoryMock = $this->getMockBuilder('Magento\Store\Model\StoreFactory') |
| 50 | + ->disableOriginalConstructor() |
| 51 | + ->setMethods(['create']) |
| 52 | + ->getMock(); |
| 53 | + $this->storeGroupFactoryMock = $this->getMockBuilder('Magento\Store\Model\GroupFactory') |
| 54 | + ->disableOriginalConstructor() |
| 55 | + ->setMethods([]) |
| 56 | + ->getMock(); |
| 57 | + $this->loadMocks(); |
| 58 | + $this->model = $objectManager->getObject( |
| 59 | + 'Magento\Store\Block\Store\Switcher', |
| 60 | + [ |
| 61 | + 'scopeConfig' => $this->scopeConfigMock, |
| 62 | + 'storeManager' => $this->storeManagerMock, |
| 63 | + 'storeFactory' => $this->storeFactoryMock, |
| 64 | + 'storeGroupFactory' => $this->storeGroupFactoryMock, |
| 65 | + ] |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + public function testGetStoreCount() |
| 70 | + { |
| 71 | + $this->scopeConfigMock->expects($this->any())->method('getValue')->willReturn('en_US'); |
| 72 | + $this->assertEquals(1, $this->model->getStoreCount()); |
| 73 | + } |
| 74 | + |
| 75 | + public function testGetStoreCountWithNotEqualLocale() |
| 76 | + { |
| 77 | + $this->scopeConfigMock->expects($this->any())->method('getValue')->willReturn('de_DE'); |
| 78 | + $this->assertEquals(0, $this->model->getStoreCount()); |
| 79 | + } |
| 80 | + |
| 81 | + protected function loadMocks() |
| 82 | + { |
| 83 | + $storeMock = $this->getMockBuilder('Magento\Store\Model\Store') |
| 84 | + ->disableOriginalConstructor() |
| 85 | + ->setMethods(['getLocaleCode', 'isActive', 'getId', 'getGroupId', 'getCollection']) |
| 86 | + ->getMock(); |
| 87 | + $groupMock = $this->getMockBuilder('Magento\Store\Model\Group') |
| 88 | + ->disableOriginalConstructor() |
| 89 | + ->setMethods([]) |
| 90 | + ->getMock(); |
| 91 | + /** @var AbstractCollection|\PHPUnit_Framework_MockObject_MockObject */ |
| 92 | + $storeCollectionMock = |
| 93 | + $this->getMockBuilder('Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection') |
| 94 | + ->disableOriginalConstructor() |
| 95 | + ->setMethods(['addWebsiteFilter', 'load']) |
| 96 | + ->getMockForAbstractClass(); |
| 97 | + /** @var AbstractCollection|\PHPUnit_Framework_MockObject_MockObject */ |
| 98 | + $groupCollectionMock = |
| 99 | + $this->getMockBuilder('Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection') |
| 100 | + ->disableOriginalConstructor() |
| 101 | + ->setMethods(['addWebsiteFilter', 'load']) |
| 102 | + ->getMockForAbstractClass(); |
| 103 | + $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock); |
| 104 | + $this->storeFactoryMock->expects($this->any())->method('create')->willReturn($storeMock); |
| 105 | + $this->storeGroupFactoryMock->expects($this->any())->method('create')->willReturn($groupMock); |
| 106 | + $storeMock->expects($this->any())->method('getCollection')->willReturn($storeCollectionMock); |
| 107 | + $groupMock->expects($this->any())->method('getCollection')->willReturn($groupCollectionMock); |
| 108 | + $groupMock->expects($this->any())->method('getId')->willReturn(1); |
| 109 | + $storeMock->expects($this->any())->method('isActive')->willReturn(true); |
| 110 | + $storeMock->expects($this->atLeastOnce())->method('getLocaleCode')->willReturn('en_US'); |
| 111 | + $storeMock->expects($this->any())->method('getGroupId')->willReturn(1); |
| 112 | + $storeMock->expects($this->any())->method('setLocaleCode'); |
| 113 | + $storeMock->expects($this->any())->method('getId')->willReturn(1); |
| 114 | + $storeCollectionMock->expects($this->any())->method('addWebsiteFilter')->willReturn([$storeMock]); |
| 115 | + $groupCollectionMock->expects($this->any())->method('addWebsiteFilter')->willReturn([$groupMock]); |
| 116 | + } |
| 117 | +} |
0 commit comments