Skip to content

Commit f91df55

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

File tree

2 files changed

+133
-7
lines changed

2 files changed

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

0 commit comments

Comments
 (0)