22
33namespace AsyncAws \Core \Test ;
44
5+ use AsyncAws \Core \Request ;
56use PHPUnit \Framework \TestCase as PHPUnitTestCase ;
67
78class TestCase extends PHPUnitTestCase
@@ -16,6 +17,85 @@ public static function assertHttpFormEqualsHttpForm(string $expected, string $ac
1617
1718 self ::assertEqualsCanonicalizing ($ expectedArray , $ actualArray , $ message );
1819 }
20+
21+ /**
22+ * Asserts that two Body documents are equal.
23+ */
24+ public static function assertUrlEqualsUrl (string $ expected , string $ actual , string $ message = '' )
25+ {
26+ $ actualUrl = \parse_url ($ actual );
27+ $ expectedUrl = \parse_url ($ expected );
28+ self ::assertSame ($ expectedUrl ['path ' ] ?? '/ ' , $ actualUrl ['path ' ] ?? '/ ' );
29+
30+ $ expectedQuery = [];
31+ foreach (array_filter (\explode ('& ' , $ expectedUrl ['query ' ] ?? '' )) as $ item ) {
32+ $ item = explode ('= ' , $ item );
33+ $ expectedQuery [$ item [0 ]] = \urldecode ($ item [1 ] ?? '' );
34+ }
35+
36+ $ actualQuery = [];
37+ foreach (array_filter (\explode ('& ' , $ actualUrl ['query ' ] ?? '' )) as $ item ) {
38+ $ item = explode ('= ' , $ item );
39+ $ actualQuery [$ item [0 ]] = \urldecode ($ item [1 ] ?? '' );
40+ }
41+ self ::assertEqualsIgnoringCase ($ expectedQuery , $ actualQuery );
42+ }
43+
44+ /**
45+ * Asserts that two Body documents are equal.
46+ */
47+ public static function assertRequestEqualsHttpRequest (string $ expected , Request $ actual , string $ message = '' )
48+ {
49+ $ expected = \explode ("\n\n" , trim ($ expected ));
50+ $ headers = $ expected [0 ];
51+ $ body = $ expected [1 ] ?? '' ;
52+ $ headers = explode ("\n" , $ headers );
53+ \array_map ('trim ' , $ headers );
54+ [$ method , $ url ] = explode (' ' , \array_shift ($ headers ));
55+
56+ self ::assertSame ($ method , $ actual ->getMethod ());
57+
58+ $ actualUrl = $ actual ->getUri ();
59+ if ($ actual ->getQuery ()) {
60+ $ actualUrl .= false !== \strpos ($ actual ->getUri (), '? ' ) ? '& ' : '? ' ;
61+ $ actualUrl .= \http_build_query ($ actual ->getQuery ());
62+ }
63+ self ::assertUrlEqualsUrl ($ url , $ actualUrl );
64+
65+ $ expectedHeaders = [];
66+ foreach ($ headers as $ header ) {
67+ [$ key , $ value ] = \explode (': ' , trim ($ header ), 2 );
68+ $ expectedHeaders [\strtolower ($ key )] = trim ($ value );
69+ }
70+ self ::assertEqualsIgnoringCase ($ expectedHeaders , $ actual ->getHeaders (), $ message );
71+
72+ switch ($ expectedHeaders ['content-type ' ] ?? null ) {
73+ case 'application/x-www-form-urlencoded ' :
74+ self ::assertHttpFormEqualsHttpForm (\trim ($ body ), $ actual ->getBody ()->stringify (), $ message );
75+
76+ break ;
77+ case 'application/json ' :
78+ if ('' === \trim ($ body )) {
79+ self ::assertSame ($ body , $ actual ->getBody ()->stringify ());
80+ } else {
81+ self ::assertJsonStringEqualsJsonString (\trim ($ body ), $ actual ->getBody ()->stringify (), $ message );
82+ }
83+
84+ break ;
85+ case 'application/xml ' :
86+ if ('' === \trim ($ body )) {
87+ self ::assertSame ($ body , $ actual ->getBody ()->stringify ());
88+ } else {
89+ self ::assertXmlStringEqualsXmlString (\trim ($ body ), $ actual ->getBody ()->stringify (), $ message );
90+ }
91+
92+ break ;
93+ default :
94+ self ::assertSame (trim ($ body ), $ actual ->getBody ()->stringify ());
95+
96+ break ;
97+ }
98+ }
1999}
20100if (!\class_exists (PHPUnitTestCase::class)) {
21101 \class_alias (InternalTestCase::class, PHPUnitTestCase::class);
0 commit comments