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