|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Algolia\AlgoliaSearch\Test\Unit; |
| 4 | + |
| 5 | +use Algolia\AlgoliaSearch\Helper\ConfigHelper; |
| 6 | +use Magento\Cookie\Helper\Cookie as CookieHelper; |
| 7 | +use Magento\Customer\Api\GroupExcludedWebsiteRepositoryInterface; |
| 8 | +use Magento\Customer\Model\ResourceModel\Group\Collection as GroupCollection; |
| 9 | +use Magento\Directory\Model\Currency as DirCurrency; |
| 10 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 11 | +use Magento\Framework\App\Config\Storage\WriterInterface; |
| 12 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 13 | +use Magento\Framework\App\ProductMetadataInterface; |
| 14 | +use Magento\Framework\Event\ManagerInterface; |
| 15 | +use Magento\Framework\Locale\Currency; |
| 16 | +use Magento\Framework\Module\ResourceInterface; |
| 17 | +use Magento\Framework\Serialize\SerializerInterface; |
| 18 | +use Magento\Store\Model\StoreManagerInterface; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +class ConfigHelperTestable extends ConfigHelper |
| 22 | +{ |
| 23 | + /** expose protected methods for unit testing */ |
| 24 | + public function serialize(array $value): string |
| 25 | + { |
| 26 | + return parent::serialize($value); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +class ConfigHelperTest extends TestCase |
| 31 | +{ |
| 32 | + protected ConfigHelperTestable $configHelper; |
| 33 | + protected ScopeConfigInterface $configInterface; |
| 34 | + protected WriterInterface $configWriter; |
| 35 | + protected StoreManagerInterface $storeManager; |
| 36 | + protected Currency $currency; |
| 37 | + protected DirCurrency $dirCurrency; |
| 38 | + protected DirectoryList $directoryList; |
| 39 | + protected ResourceInterface $moduleResource; |
| 40 | + protected ProductMetadataInterface $productMetadata; |
| 41 | + protected ManagerInterface $eventManager; |
| 42 | + protected SerializerInterface $serializer; |
| 43 | + protected GroupCollection $groupCollection; |
| 44 | + protected GroupExcludedWebsiteRepositoryInterface $groupExcludedWebsiteRepository; |
| 45 | + protected CookieHelper $cookieHelper; |
| 46 | + |
| 47 | + public function setUp(): void |
| 48 | + { |
| 49 | + $this->configInterface = $this->createMock(ScopeConfigInterface::class); |
| 50 | + $this->configWriter = $this->createMock(WriterInterface::class); |
| 51 | + $this->storeManager = $this->createMock(StoreManagerInterface::class); |
| 52 | + $this->currency = $this->createMock(Currency::class); |
| 53 | + $this->dirCurrency = $this->createMock(DirCurrency::class); |
| 54 | + $this->directoryList = $this->createMock(DirectoryList::class); |
| 55 | + $this->moduleResource = $this->createMock(ResourceInterface::class); |
| 56 | + $this->productMetadata = $this->createMock(ProductMetadataInterface::class); |
| 57 | + $this->eventManager = $this->createMock(ManagerInterface::class); |
| 58 | + $this->serializer = $this->createMock(SerializerInterface::class); |
| 59 | + $this->groupCollection = $this->createMock(GroupCollection::class); |
| 60 | + $this->groupExcludedWebsiteRepository = $this->createMock(GroupExcludedWebsiteRepositoryInterface::class); |
| 61 | + $this->cookieHelper = $this->createMock(CookieHelper::class); |
| 62 | + |
| 63 | + $this->configHelper = new ConfigHelperTestable( |
| 64 | + $this->configInterface, |
| 65 | + $this->configWriter, |
| 66 | + $this->storeManager, |
| 67 | + $this->currency, |
| 68 | + $this->dirCurrency, |
| 69 | + $this->directoryList, |
| 70 | + $this->moduleResource, |
| 71 | + $this->productMetadata, |
| 72 | + $this->eventManager, |
| 73 | + $this->serializer, |
| 74 | + $this->groupCollection, |
| 75 | + $this->groupExcludedWebsiteRepository, |
| 76 | + $this->cookieHelper |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + public function testGetIndexPrefix() |
| 81 | + { |
| 82 | + $testPrefix = 'foo_bar_'; |
| 83 | + $this->configInterface->method('getValue')->willReturn($testPrefix); |
| 84 | + $this->assertEquals($testPrefix, $this->configHelper->getIndexPrefix()); |
| 85 | + } |
| 86 | + |
| 87 | + public function testGetIndexPrefixWhenNull() { |
| 88 | + $this->configInterface->method('getValue')->willReturn(null); |
| 89 | + $this->assertEquals('', $this->configHelper->getIndexPrefix()); |
| 90 | + } |
| 91 | + |
| 92 | + public function testSerializerReturnsString() { |
| 93 | + $this->serializer->method('serialize')->willReturn('{"foo":"bar"}'); |
| 94 | + $array = [ |
| 95 | + 'foo' => 'bar' |
| 96 | + ]; |
| 97 | + $result = $this->configHelper->serialize($array); |
| 98 | + $this->assertEquals('{"foo":"bar"}', $result); |
| 99 | + } |
| 100 | + |
| 101 | + public function testSerializerFailure() { |
| 102 | + $this->serializer->method('serialize')->willReturn(false); |
| 103 | + $array = [ |
| 104 | + 'foo' => 'bar' |
| 105 | + ]; |
| 106 | + $result = $this->configHelper->serialize($array); |
| 107 | + $this->assertEquals('', $result); |
| 108 | + } |
| 109 | +} |
0 commit comments