Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CountryWithWebsitesTest.php
Go to the documentation of this file.
1 <?php
8 
15 
16 class CountryWithWebsitesTest extends \PHPUnit\Framework\TestCase
17 {
21  private $countriesFactoryMock;
22 
26  private $allowedCountriesMock;
27 
31  private $storeManagerMock;
32 
36  private $countryByWebsite;
37 
41  private $shareConfigMock;
42 
43  public function setUp()
44  {
45  $this->countriesFactoryMock =
46  $this->getMockBuilder(\Magento\Directory\Model\ResourceModel\Country\CollectionFactory::class)
47  ->setMethods(['create'])
48  ->disableOriginalConstructor()
49  ->getMock();
50  $this->allowedCountriesMock = $this->getMockBuilder(AllowedCountries::class)
51  ->disableOriginalConstructor()
52  ->getMock();
53  $eavCollectionFactoryMock =
54  $this->getMockBuilder(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory::class)
55  ->disableOriginalConstructor()
56  ->getMock();
57  $optionsFactoryMock =
58  $this->getMockBuilder(\Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory::class)
59  ->disableOriginalConstructor()
60  ->getMock();
61  $this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
62  $this->shareConfigMock = $this->getMockBuilder(Share::class)
63  ->disableOriginalConstructor()
64  ->getMock();
65  $this->countryByWebsite = new CountryWithWebsites(
66  $eavCollectionFactoryMock,
67  $optionsFactoryMock,
68  $this->countriesFactoryMock,
69  $this->allowedCountriesMock,
70  $this->storeManagerMock,
71  $this->shareConfigMock
72  );
73  }
74 
75  public function testGetAllOptions()
76  {
77  $website1 = $this->createMock(WebsiteInterface::class);
78  $website2 = $this->createMock(WebsiteInterface::class);
79 
80  $website1->expects($this->atLeastOnce())
81  ->method('getId')
82  ->willReturn(1);
83  $website2->expects($this->atLeastOnce())
84  ->method('getId')
85  ->willReturn(2);
86  $this->storeManagerMock->expects($this->once())
87  ->method('getWebsites')
88  ->willReturn([$website1, $website2]);
89  $collectionMock = $this->getMockBuilder(AbstractDb::class)
90  ->disableOriginalConstructor()
91  ->getMock();
92 
93  $this->allowedCountriesMock->expects($this->exactly(2))
94  ->method('getAllowedCountries')
95  ->withConsecutive(
96  ['website', 1],
97  ['website', 2]
98  )
99  ->willReturnMap([
100  ['website', 1, ['AM' => 'AM']],
101  ['website', 2, ['AM' => 'AM', 'DZ' => 'DZ']]
102  ]);
103  $this->countriesFactoryMock->expects($this->once())
104  ->method('create')
105  ->willReturn($collectionMock);
106  $collectionMock->expects($this->once())
107  ->method('addFieldToFilter')
108  ->with('country_id', ['in' => ['AM' => 'AM', 'DZ' => 'DZ']])
109  ->willReturnSelf();
110  $collectionMock->expects($this->once())
111  ->method('toOptionArray')
112  ->willReturn([
113  ['value' => 'AM', 'label' => 'UZ']
114  ]);
115 
116  $this->assertEquals([
117  ['value' => 'AM', 'label' => 'UZ', 'website_ids' => [1, 2]]
118  ], $this->countryByWebsite->getAllOptions());
119  }
120 }