PluginProbe
Packeta / trunk
Packeta vtrunk
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / tests / Core / Api / Rest / PickupPointValidateTest.php

PickupPointValidateTest.php in Packeta trunk, at tests/Core/Api/Rest/PickupPointValidateTest.php

68 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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