Skip to content

Commit 40c4022

Browse files
committed
Unit tests for USPSEndpointTest
1 parent 4f55611 commit 40c4022

File tree

2 files changed

+132
-7
lines changed

2 files changed

+132
-7
lines changed

src/VerifierServer/Endpoints/USPSEndpoint.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,21 @@ class USPSEndpoint implements EndpointInterface
2222
CONST BASE_URL = 'http://production.shippingapis.com/ShippingAPITest.dll?API=ZipCodeLookup&XML=';
2323
CONST INFO = 'Information provided by www.usps.com';
2424

25-
public string $userid;
2625
public string $id = '0';
2726
public string $address1 = '';
28-
public string $address2 = '6406 Ivy Lane';
29-
public string $city = 'Greenbelt';
27+
public string $address2 = '6406 IVY LANE';
28+
public string $city = 'GREENBELT';
3029
public string $state = 'MD';
3130
public string $zip5 = '20770';
3231
public string $zip4 = '1441';
3332

34-
public function __construct()
33+
public function __construct(public string $userid)
3534
{
36-
$this->userid = $_ENV['USPS_USERID']
37-
?: throw new RuntimeException('USPS_USERID environment variable not set.');
3835
}
3936

4037
public function handle(
4138
string $method,
42-
ServerRequestInterface|string $request,
39+
$request,
4340
int|string &$response,
4441
array &$headers,
4542
string &$body,
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Tests\VerifierServer\Endpoints;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use VerifierServer\Endpoints\USPSEndpoint;
7+
use Psr\Http\Message\ServerRequestInterface;
8+
use React\Http\Message\Response;
9+
10+
class USPSEndpointTest extends TestCase
11+
{
12+
private USPSEndpoint $endpoint;
13+
14+
protected function setUp(): void
15+
{
16+
$this->endpoint = new USPSEndpoint('test_userid');
17+
}
18+
19+
public function testHandleGet(): void
20+
{
21+
$request = $this->createMock(ServerRequestInterface::class);
22+
$request->method('getQueryParams')->willReturn([
23+
'address2' => '6406 Ivy Lane',
24+
'city' => 'Greenbelt',
25+
'state' => 'MD',
26+
]);
27+
28+
$response = 0;
29+
$headers = [];
30+
$body = '';
31+
32+
$this->endpoint->handle('GET', $request, $response, $headers, $body);
33+
34+
$this->assertEquals(Response::STATUS_OK, $response);
35+
$this->assertArrayHasKey('Content-Type', $headers);
36+
$this->assertEquals('application/xml; charset=UTF-8', $headers['Content-Type']);
37+
$this->assertNotEmpty($body);
38+
}
39+
40+
public function testHandleHead(): void
41+
{
42+
$response = 0;
43+
$headers = [];
44+
$body = '';
45+
46+
$content = $this->endpoint->handle('HEAD', '', $response, $headers, $body);
47+
48+
$this->assertEquals(Response::STATUS_OK, $response);
49+
$this->assertArrayHasKey('Content-Type', $headers);
50+
$this->assertArrayHasKey('Content-Length', $headers);
51+
//$this->assertNotEmpty($content);
52+
}
53+
54+
public function testHandlePost(): void
55+
{
56+
$request = $this->createMock(ServerRequestInterface::class);
57+
$request->method('getQueryParams')->willReturn([
58+
'address2' => '6406 Ivy Lane',
59+
'city' => 'Greenbelt',
60+
'state' => 'MD',
61+
]);
62+
63+
$response = 0;
64+
$headers = [];
65+
$body = '';
66+
67+
$this->endpoint->handle('POST', $request, $response, $headers, $body);
68+
69+
$this->assertEquals(Response::STATUS_OK, $response);
70+
$this->assertArrayHasKey('Content-Type', $headers);
71+
$this->assertEquals('application/xml; charset=UTF-8', $headers['Content-Type']);
72+
$this->assertNotEmpty($body);
73+
}
74+
75+
public function testHandleInvalidMethod(): void
76+
{
77+
$response = 0;
78+
$headers = [];
79+
$body = '';
80+
81+
$this->endpoint->handle('INVALID', '', $response, $headers, $body);
82+
83+
$this->assertEquals(Response::STATUS_METHOD_NOT_ALLOWED, $response);
84+
$this->assertArrayHasKey('Content-Type', $headers);
85+
$this->assertEquals('text/plain', $headers['Content-Type']);
86+
$this->assertEquals('Method Not Allowed', $body);
87+
}
88+
89+
public function testGetWithMissingFields(): void
90+
{
91+
$request = $this->createMock(ServerRequestInterface::class);
92+
$request->method('getQueryParams')->willReturn([]);
93+
94+
$response = 0;
95+
$headers = [];
96+
$body = '';
97+
98+
$this->endpoint->handle('GET', $request, $response, $headers, $body);
99+
100+
$this->assertEquals(Response::STATUS_BAD_REQUEST, $response);
101+
$this->assertArrayHasKey('Content-Type', $headers);
102+
$this->assertEquals('text/plain', $headers['Content-Type']);
103+
$this->assertStringContainsString('is required', $body);
104+
}
105+
106+
public function testXmlStringGeneration(): void
107+
{
108+
$reflection = new \ReflectionClass($this->endpoint);
109+
$method = $reflection->getMethod('__xmlstring');
110+
$method->setAccessible(true);
111+
112+
$xmlString = $method->invoke($this->endpoint);
113+
114+
$this->assertStringContainsString('<ZipCodeLookupRequest', $xmlString);
115+
$this->assertStringContainsString('<Address2>6406 IVY LANE</Address2>', $xmlString);
116+
}
117+
118+
public function testContentGeneration(): void
119+
{
120+
$reflection = new \ReflectionClass($this->endpoint);
121+
$method = $reflection->getMethod('__content');
122+
$method->setAccessible(true);
123+
124+
$content = $method->invoke($this->endpoint);
125+
126+
$this->assertStringContainsString('<Info>Information provided by www.usps.com</Info>', $content);
127+
}
128+
}

0 commit comments

Comments
 (0)