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