| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Tests\Core\Api\Rest; |
| 6 |
|
| 7 |
use Exception; |
| 8 |
use Packetery\Core\Api\Rest\Exception\InvalidApiKeyException; |
| 9 |
use Packetery\Core\Api\Rest\PickupPointValidate; |
| 10 |
use Packetery\Core\Api\Rest\PickupPointValidateResponse; |
| 11 |
use Packetery\Core\Api\Rest\RestException; |
| 12 |
use Packetery\Core\Interfaces\IWebRequestClient; |
| 13 |
use PHPUnit\Framework\MockObject\MockObject; |
| 14 |
use PHPUnit\Framework\TestCase; |
| 15 |
use Tests\Core\DummyFactory; |
| 16 |
|
| 17 |
class PickupPointValidateTest extends TestCase { |
| 18 |
public function testConstructWithValidApiKey(): void { |
| 19 |
$webRequestClientMock = $this->getWebRequestClientMock(); |
| 20 |
$validator = PickupPointValidate::createWithValidApiKey( $webRequestClientMock, 'dummyApiKey' ); |
| 21 |
|
| 22 |
self::assertNotNull( $validator ); |
| 23 |
} |
| 24 |
|
| 25 |
public function testConstructWithNullApiKey(): void { |
| 26 |
$webRequestClientMock = $this->getWebRequestClientMock(); |
| 27 |
|
| 28 |
$this->expectException( InvalidApiKeyException::class ); |
| 29 |
$this->expectExceptionMessage( 'API key is missing' ); |
| 30 |
|
| 31 |
PickupPointValidate::createWithValidApiKey( $webRequestClientMock, null ); |
| 32 |
} |
| 33 |
|
| 34 |
public function testValidateOk(): void { |
| 35 |
$webRequestClientMock = $this->getWebRequestClientMock(); |
| 36 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode |
| 37 |
$expectedResponse = json_encode( |
| 38 |
[ |
| 39 |
'isValid' => true, |
| 40 |
'errors' => [], |
| 41 |
'status' => 200, |
| 42 |
] |
| 43 |
); |
| 44 |
$webRequestClientMock->method( 'post' ) |
| 45 |
->willReturn( $expectedResponse ); |
| 46 |
$validator = PickupPointValidate::createWithValidApiKey( $webRequestClientMock, 'dummyApiKey' ); |
| 47 |
|
| 48 |
self::assertInstanceOf( |
| 49 |
PickupPointValidateResponse::class, |
| 50 |
$validator->validate( DummyFactory::getEmptyPickupPointValidateRequest() ) |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
public function testValidateFail(): void { |
| 55 |
$webRequestClientMock = $this->getWebRequestClientMock(); |
| 56 |
$webRequestClientMock->method( 'post' ) |
| 57 |
->willThrowException( new Exception( 'dummyException' ) ); |
| 58 |
$validator = PickupPointValidate::createWithValidApiKey( $webRequestClientMock, 'dummyApiKey' ); |
| 59 |
|
| 60 |
$this->expectException( RestException::class ); |
| 61 |
$validator->validate( DummyFactory::getEmptyPickupPointValidateRequest() ); |
| 62 |
} |
| 63 |
|
| 64 |
private function getWebRequestClientMock(): MockObject&IWebRequestClient { |
| 65 |
return $this->createMock( IWebRequestClient::class ); |
| 66 |
} |
| 67 |
} |
| 68 |
|