| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Tests\Module\Order; |
| 6 |
|
| 7 |
use Packetery\Core\Api\Rest\PickupPointValidateRequest; |
| 8 |
use Packetery\Core\Api\Rest\PickupPointValidateResponse; |
| 9 |
use Packetery\Core\Log\ILogger; |
| 10 |
use Packetery\Core\Log\Record; |
| 11 |
use Packetery\Module\Framework\WpAdapter; |
| 12 |
use Packetery\Module\Options\OptionsProvider; |
| 13 |
use Packetery\Module\Order\PickupPointValidator; |
| 14 |
use Packetery\Module\WebRequestClient; |
| 15 |
use PHPUnit\Framework\MockObject\MockObject; |
| 16 |
use PHPUnit\Framework\TestCase; |
| 17 |
|
| 18 |
class PickupPointValidatorTest extends TestCase { |
| 19 |
|
| 20 |
private OptionsProvider|MockObject $optionsProviderMock; |
| 21 |
private ILogger|MockObject $loggerMock; |
| 22 |
private WebRequestClient|MockObject $webRequestClientMock; |
| 23 |
private WpAdapter|MockObject $wpAdapter; |
| 24 |
|
| 25 |
private function createMocks(): void { |
| 26 |
$this->optionsProviderMock = $this->createMock( OptionsProvider::class ); |
| 27 |
$this->loggerMock = $this->createMock( ILogger::class ); |
| 28 |
$this->webRequestClientMock = $this->createMock( WebRequestClient::class ); |
| 29 |
$this->wpAdapter = $this->createMock( WpAdapter::class ); |
| 30 |
} |
| 31 |
|
| 32 |
public function testValidateReturnsErrorResponseWhenApiKeyIsInvalid(): void { |
| 33 |
$this->createMocks(); |
| 34 |
|
| 35 |
$this->optionsProviderMock |
| 36 |
->method( 'get_api_key' ) |
| 37 |
->willReturn( null ); |
| 38 |
|
| 39 |
$this->wpAdapter->method( '__' )->willReturn( 'dummyErrorTitle' ); |
| 40 |
|
| 41 |
$validator = new PickupPointValidator( |
| 42 |
$this->optionsProviderMock, |
| 43 |
$this->loggerMock, |
| 44 |
$this->webRequestClientMock, |
| 45 |
$this->wpAdapter |
| 46 |
); |
| 47 |
|
| 48 |
$requestMock = $this->createMock( PickupPointValidateRequest::class ); |
| 49 |
|
| 50 |
$this->loggerMock |
| 51 |
->expects( $this->once() ) |
| 52 |
->method( 'add' ) |
| 53 |
->with( |
| 54 |
$this->callback( |
| 55 |
function ( Record $record ) { |
| 56 |
return $record->status === Record::STATUS_ERROR |
| 57 |
&& $record->action === Record::ACTION_PICKUP_POINT_VALIDATE; |
| 58 |
} |
| 59 |
) |
| 60 |
); |
| 61 |
|
| 62 |
$response = $validator->validate( $requestMock ); |
| 63 |
|
| 64 |
$this->assertInstanceOf( PickupPointValidateResponse::class, $response ); |
| 65 |
$this->assertEmpty( $response->getErrors() ); |
| 66 |
} |
| 67 |
|
| 68 |
public function testValidateCallsValidateWhenApiKeyIsValid(): void { |
| 69 |
$this->createMocks(); |
| 70 |
|
| 71 |
$this->optionsProviderMock |
| 72 |
->method( 'get_api_key' ) |
| 73 |
->willReturn( 'dummyApiKey' ); |
| 74 |
|
| 75 |
$this->wpAdapter->method( '__' )->willReturn( 'dummyErrorTitle' ); |
| 76 |
|
| 77 |
$this->webRequestClientMock |
| 78 |
->expects( $this->once() ) |
| 79 |
->method( 'post' ) |
| 80 |
->with( |
| 81 |
$this->callback( |
| 82 |
function ( string $url ) { |
| 83 |
return str_contains( $url, 'validate' ); |
| 84 |
} |
| 85 |
), |
| 86 |
$this->anything() |
| 87 |
) |
| 88 |
->willReturn( |
| 89 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode |
| 90 |
json_encode( |
| 91 |
[ |
| 92 |
'isValid' => true, |
| 93 |
'errors' => [], |
| 94 |
] |
| 95 |
) |
| 96 |
); |
| 97 |
|
| 98 |
$validator = new PickupPointValidator( |
| 99 |
$this->optionsProviderMock, |
| 100 |
$this->loggerMock, |
| 101 |
$this->webRequestClientMock, |
| 102 |
$this->wpAdapter |
| 103 |
); |
| 104 |
|
| 105 |
$requestMock = $this->createMock( PickupPointValidateRequest::class ); |
| 106 |
|
| 107 |
$response = $validator->validate( $requestMock ); |
| 108 |
|
| 109 |
$this->assertInstanceOf( PickupPointValidateResponse::class, $response ); |
| 110 |
$this->assertEmpty( $response->getErrors() ); |
| 111 |
} |
| 112 |
} |
| 113 |
|