CountryTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Test\Unit\Helper;
  7. use Magento\Braintree\Helper\Country;
  8. use Magento\Directory\Model\ResourceModel\Country\CollectionFactory;
  9. use Magento\Directory\Model\ResourceModel\Country\Collection;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  11. /**
  12. * Class CountryTest
  13. */
  14. class CountryTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \Magento\Directory\Model\ResourceModel\Country\Collection|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $collection;
  20. /**
  21. * @var \Magento\Braintree\Helper\Country
  22. */
  23. private $helper;
  24. /**
  25. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  26. */
  27. private $objectManager;
  28. protected function setUp()
  29. {
  30. $this->objectManager = new ObjectManager($this);
  31. $collectionFactory = $this->getCollectionFactoryMock();
  32. $this->helper = $this->objectManager->getObject(Country::class, [
  33. 'factory' => $collectionFactory
  34. ]);
  35. }
  36. /**
  37. * @covers \Magento\Braintree\Helper\Country::getCountries
  38. */
  39. public function testGetCountries()
  40. {
  41. $this->collection->expects(static::once())
  42. ->method('toOptionArray')
  43. ->willReturn([
  44. ['value' => 'US', 'label' => 'United States'],
  45. ['value' => 'UK', 'label' => 'United Kingdom'],
  46. ]);
  47. $this->helper->getCountries();
  48. $this->collection->expects(static::never())
  49. ->method('toOptionArray');
  50. $this->helper->getCountries();
  51. }
  52. /**
  53. * Create mock for country collection factory
  54. */
  55. protected function getCollectionFactoryMock()
  56. {
  57. $this->collection = $this->getMockBuilder(Collection::class)
  58. ->disableOriginalConstructor()
  59. ->setMethods(['addFieldToFilter', 'loadData', 'toOptionArray', '__wakeup'])
  60. ->getMock();
  61. $this->collection->expects(static::any())
  62. ->method('addFieldToFilter')
  63. ->willReturnSelf();
  64. $this->collection->expects(static::any())
  65. ->method('loadData')
  66. ->willReturnSelf();
  67. $collectionFactory = $this->getMockBuilder(CollectionFactory::class)
  68. ->disableOriginalConstructor()
  69. ->setMethods(['create'])
  70. ->getMock();
  71. $collectionFactory->expects(static::once())
  72. ->method('create')
  73. ->willReturn($this->collection);
  74. return $collectionFactory;
  75. }
  76. }