Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CountryTest.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Directory\Model\ResourceModel\Country\CollectionFactory;
12 
16 class CountryTest extends \PHPUnit\Framework\TestCase
17 {
21  private $collection;
22 
26  private $helper;
27 
31  private $objectManager;
32 
33  protected function setUp()
34  {
35  $this->objectManager = new ObjectManager($this);
36 
37  $collectionFactory = $this->getCollectionFactoryMock();
38 
39  $this->helper = $this->objectManager->getObject(Country::class, [
40  'factory' => $collectionFactory
41  ]);
42  }
43 
47  public function testGetCountries()
48  {
49  $this->collection->expects(static::once())
50  ->method('toOptionArray')
51  ->willReturn([
52  ['value' => 'US', 'label' => 'United States'],
53  ['value' => 'UK', 'label' => 'United Kingdom'],
54  ]);
55 
56  $this->helper->getCountries();
57 
58  $this->collection->expects(static::never())
59  ->method('toOptionArray');
60 
61  $this->helper->getCountries();
62  }
63 
67  protected function getCollectionFactoryMock()
68  {
69  $this->collection = $this->getMockBuilder(Collection::class)
70  ->disableOriginalConstructor()
71  ->setMethods(['addFieldToFilter', 'loadData', 'toOptionArray', '__wakeup'])
72  ->getMock();
73 
74  $this->collection->expects(static::any())
75  ->method('addFieldToFilter')
76  ->willReturnSelf();
77 
78  $this->collection->expects(static::any())
79  ->method('loadData')
80  ->willReturnSelf();
81 
82  $collectionFactory = $this->getMockBuilder(CollectionFactory::class)
83  ->disableOriginalConstructor()
84  ->setMethods(['create'])
85  ->getMock();
86 
87  $collectionFactory->expects(static::once())
88  ->method('create')
89  ->willReturn($this->collection);
90 
91  return $collectionFactory;
92  }
93 }