|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\InventoryInStorePickup\Test\Unit\Model\SearchRequest\Area\SearchTerm; |
| 9 | + |
| 10 | +use Magento\Directory\Model\Country\Postcode\ValidatorInterface; |
| 11 | +use Magento\Framework\DataObject; |
| 12 | +use Magento\InventoryInStorePickup\Model\SearchRequest\Area\SearchTerm\PostcodeParser; |
| 13 | +use Magento\InventoryInStorePickupApi\Model\SearchRequest\Area\SearchTerm\DelimiterConfig; |
| 14 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 15 | +use PHPUnit\Framework\MockObject\MockObject; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +class PostcodeParserTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var PostcodeParser |
| 22 | + */ |
| 23 | + private $model; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var ValidatorInterface|MockObject |
| 27 | + */ |
| 28 | + private $validatorMock; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var DelimiterConfig|MockObject |
| 32 | + */ |
| 33 | + private $delimiterConfigMock; |
| 34 | + |
| 35 | + protected function setUp(): void |
| 36 | + { |
| 37 | + $this->validatorMock = $this->createMock(ValidatorInterface::class); |
| 38 | + $this->delimiterConfigMock = $this->createMock(DelimiterConfig::class); |
| 39 | + $this->model = new PostcodeParser($this->validatorMock, $this->delimiterConfigMock); |
| 40 | + } |
| 41 | + |
| 42 | + #[ |
| 43 | + DataProvider('executeDataProvider'), |
| 44 | + ] |
| 45 | + public function testExecute(string $searchTerm, string $country, bool $isValid, string $postcode): void |
| 46 | + { |
| 47 | + $this->delimiterConfigMock->expects($this->atLeastOnce()) |
| 48 | + ->method('getDelimiter') |
| 49 | + ->willReturn(':'); |
| 50 | + $this->validatorMock->expects($this->once()) |
| 51 | + ->method('validate') |
| 52 | + ->willReturn($isValid); |
| 53 | + |
| 54 | + $result = new DataObject(); |
| 55 | + $result->setData('country', $country); |
| 56 | + $this->model->execute($searchTerm, $result); |
| 57 | + $this->assertEquals($postcode, $result->getData('postcode')); |
| 58 | + } |
| 59 | + |
| 60 | + public static function executeDataProvider(): array |
| 61 | + { |
| 62 | + return [ |
| 63 | + ['12345', 'AS', true, '12345'], |
| 64 | + ['1234:AT', 'AT', true, '1234'], |
| 65 | + ['11123', 'AX', false, ''], |
| 66 | + ['AA12345:BB', 'BB', false, ''], |
| 67 | + ]; |
| 68 | + } |
| 69 | + |
| 70 | + public function testExecuteWithException(): void |
| 71 | + { |
| 72 | + $this->delimiterConfigMock->expects($this->atLeastOnce()) |
| 73 | + ->method('getDelimiter') |
| 74 | + ->willReturn(':'); |
| 75 | + $this->validatorMock->expects($this->once()) |
| 76 | + ->method('validate') |
| 77 | + ->willThrowException(new \InvalidArgumentException()); |
| 78 | + |
| 79 | + $result = new DataObject(); |
| 80 | + $result->setData('country', 'UU'); |
| 81 | + $this->model->execute('-', $result); |
| 82 | + $this->assertEquals('', $result->getData('postcode')); |
| 83 | + } |
| 84 | +} |
0 commit comments