Complex classes like Net_URL2Test often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Net_URL2Test, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class Net_URL2Test extends PHPUnit_Framework_TestCase |
||
25 | { |
||
26 | /** |
||
27 | * Tests setting a zero-length string and false as authority |
||
28 | * Also: Regression test for Bug #20420 |
||
29 | * |
||
30 | * @covers Net_URL2::setAuthority |
||
31 | * @return void |
||
32 | * @link https://pear.php.net/bugs/bug.php?id=20420 |
||
33 | */ |
||
34 | public function testSetEmptyAuthority() |
||
47 | |||
48 | /** |
||
49 | * Tests setting an empty userinfo part |
||
50 | * Also: Regression test for Bug #20013 and Bug #20399 |
||
51 | * |
||
52 | * @covers Net_URL2::setUserinfo |
||
53 | * @covers Net_URL2::getUserinfo |
||
54 | * @covers Net_URL2::getURL |
||
55 | * @return void |
||
56 | * @link https://pear.php.net/bugs/bug.php?id=20013 |
||
57 | * @link https://pear.php.net/bugs/bug.php?id=20399 |
||
58 | */ |
||
59 | public function testSetEmptyUserinfo() |
||
73 | |||
74 | /** |
||
75 | * Tests an URL with no userinfo and normalization |
||
76 | * |
||
77 | * Also: Regression test for Bug #20385 |
||
78 | * |
||
79 | * @covers Net_URL2::getUserinfo |
||
80 | * @covers Net_URL2::normalize |
||
81 | * @covers Net_URL2::getNormalizedURL |
||
82 | * @return void |
||
83 | * @link https://pear.php.net/bugs/bug.php?id=20385 |
||
84 | */ |
||
85 | public function testNoUserinfoAndNormalize() |
||
98 | |||
99 | /** |
||
100 | * Tests setQueryVariable(). |
||
101 | * |
||
102 | * @return void |
||
103 | */ |
||
104 | public function testSetQueryVariable() |
||
111 | |||
112 | /** |
||
113 | * Tests setQueryVariables(). |
||
114 | * |
||
115 | * @return void |
||
116 | */ |
||
117 | public function testSetQueryVariables() |
||
128 | |||
129 | /** |
||
130 | * Tests unsetQueryVariable() |
||
131 | * |
||
132 | * @return void |
||
133 | */ |
||
134 | public function testUnsetQueryVariable() |
||
151 | |||
152 | /** |
||
153 | * Tests setQuery(). |
||
154 | * |
||
155 | * @return void |
||
156 | */ |
||
157 | public function testSetQuery() |
||
166 | |||
167 | /** |
||
168 | * Tests getQuery(). |
||
169 | * |
||
170 | * @return void |
||
171 | */ |
||
172 | public function testGetQuery() |
||
180 | |||
181 | /** |
||
182 | * Tests setScheme(). |
||
183 | * |
||
184 | * @return void |
||
185 | */ |
||
186 | public function testSetScheme() |
||
195 | |||
196 | /** |
||
197 | * Tests setting the fragment. |
||
198 | * |
||
199 | * @return void |
||
200 | */ |
||
201 | public function testSetFragment() |
||
208 | |||
209 | /** |
||
210 | * A dataProvider for paths that are solved to a base URI. |
||
211 | * |
||
212 | * @see testResolveUrls |
||
213 | * @return array |
||
214 | */ |
||
215 | public function provideResolveUrls() |
||
274 | |||
275 | /** |
||
276 | * Test the resolve() function to resolve URLs to each other. |
||
277 | * |
||
278 | * @param string $baseURL base-URI |
||
279 | * @param array $relativeAbsolutePairs url-pairs, relative => resolved |
||
280 | * @param array $options Net_URL2 options |
||
281 | * |
||
282 | * @dataProvider provideResolveUrls |
||
283 | * @covers Net_URL2::resolve |
||
284 | * @return void |
||
285 | */ |
||
286 | public function testResolveUrls($baseURL, array $relativeAbsolutePairs, |
||
297 | |||
298 | /** |
||
299 | * Helper method to turn options with strings as the constant names |
||
300 | * (to allow to externalize the fixtures) into a concrete options |
||
301 | * array that uses the values from the Net_URL2 class constants. |
||
302 | * |
||
303 | * @param array $options options |
||
304 | * |
||
305 | * @return array |
||
306 | */ |
||
307 | private function _translateOptionData(array $options) |
||
318 | |||
319 | /** |
||
320 | * Test the resolve() function throwing an exception with invalid data. |
||
321 | * |
||
322 | * @covers Net_URL2::resolve |
||
323 | * @return void |
||
324 | */ |
||
325 | public function testResolveException() |
||
339 | |||
340 | /** |
||
341 | * Assert that there is a last error message and it contains needle. |
||
342 | * |
||
343 | * @param string $needle needle |
||
344 | * |
||
345 | * @return void |
||
346 | */ |
||
347 | private function _assertLastErrorContains($needle) |
||
360 | |||
361 | /** |
||
362 | * Test UrlEncoding |
||
363 | * |
||
364 | * @return void |
||
365 | * @link https://pear.php.net/bugs/bug.php?id=18267 |
||
366 | */ |
||
367 | public function testUrlEncoding() |
||
384 | |||
385 | /** |
||
386 | * A test to verify that keys in QUERY_STRING are encoded by default. |
||
387 | * |
||
388 | * @return void |
||
389 | * @see Net_URL2::OPTION_ENCODE_KEYS |
||
390 | * @see Net_URL2::buildQuery() |
||
391 | */ |
||
392 | public function testEncodeKeys() |
||
401 | |||
402 | /** |
||
403 | * A test to verify that keys in QUERY_STRING are not encoded when we supply |
||
404 | * 'false' via {@link Net_URL2::__construct()}. |
||
405 | * |
||
406 | * @return void |
||
407 | * @see Net_URL2::OPTION_ENCODE_KEYS |
||
408 | * @see Net_URL2::buildQuery() |
||
409 | */ |
||
410 | public function testDontEncodeKeys() |
||
422 | |||
423 | /** |
||
424 | * Brackets for array query variables |
||
425 | * |
||
426 | * Also text to not encode zero based integer sequence into brackets |
||
427 | * |
||
428 | * @return void |
||
429 | * |
||
430 | * @link https://pear.php.net/bugs/bug.php?id=20427 |
||
431 | */ |
||
432 | public function testUseBrackets() |
||
445 | |||
446 | /** |
||
447 | * Do not use brackets for query variables passed as array |
||
448 | * |
||
449 | * @return void |
||
450 | */ |
||
451 | public function testDontUseBrackets() |
||
463 | |||
464 | /** |
||
465 | * A dataProvider for example URIs from RFC 3986 Section 1.1.2 |
||
466 | * |
||
467 | * @return array |
||
468 | * @link http://tools.ietf.org/html/rfc3986#section-1.1.2 |
||
469 | * @see testExampleUri |
||
470 | */ |
||
471 | public function provideExampleUri() |
||
484 | |||
485 | /** |
||
486 | * test that Net_URL2 works with the example URIs from RFC 3986 Section 1.1.2 |
||
487 | * |
||
488 | * @param string $uri example URI |
||
489 | * |
||
490 | * @return void |
||
491 | * @dataProvider provideExampleUri |
||
492 | * @link http://tools.ietf.org/html/rfc3986#section-1.1.2 |
||
493 | * @see testComponentRecompositionAndNormalization |
||
494 | */ |
||
495 | public function testExampleUri($uri) |
||
502 | |||
503 | /** |
||
504 | * A dataProvider for pairs of paths with dot segments and |
||
505 | * their form when removed. |
||
506 | * |
||
507 | * @see testRemoveDotSegments |
||
508 | * @return array |
||
509 | */ |
||
510 | public function providePath() |
||
534 | |||
535 | /** |
||
536 | * Test removal of dot segments |
||
537 | * |
||
538 | * @param string $path Path |
||
539 | * @param string $assertion Assertion |
||
540 | * |
||
541 | * @dataProvider providePath |
||
542 | * @covers Net_URL2::removeDotSegments |
||
543 | * @return void |
||
544 | */ |
||
545 | public function testRemoveDotSegments($path, $assertion) |
||
549 | |||
550 | /** |
||
551 | * Test removeDotSegments() loop limit warning |
||
552 | * |
||
553 | * @covers Net_URL2::removeDotSegments |
||
554 | * @return void |
||
555 | */ |
||
556 | public function testRemoveDotSegmentsLoopLimit() |
||
566 | |||
567 | /** |
||
568 | * A dataProvider for query strings and their array representation |
||
569 | * |
||
570 | * @see testGetQueryVariables |
||
571 | * @return array |
||
572 | */ |
||
573 | public function provideQueryStrings() |
||
620 | |||
621 | /** |
||
622 | * Test parsing of query variables |
||
623 | * |
||
624 | * @param string $query string |
||
625 | * @param mixed $expected null to test against parse_str() behavior |
||
626 | * @param array $options Net_URL2 options |
||
627 | * |
||
628 | * @dataProvider provideQueryStrings |
||
629 | * @covers Net_URL2::getQueryVariables |
||
630 | * @covers Net_URL2::_queryArrayByKey |
||
631 | * @covers Net_URL2::_queryArrayByBrackets |
||
632 | * @covers Net_URL2::_queryKeyBracketOffset |
||
633 | * @return void |
||
634 | */ |
||
635 | public function testGetQueryVariables($query, $expected = null, |
||
659 | |||
660 | /** |
||
661 | * data provider of host and port |
||
662 | * |
||
663 | * @return array |
||
664 | * @see testHostAndPort |
||
665 | */ |
||
666 | public function provideHostAndPort() |
||
680 | |||
681 | /** |
||
682 | * test that an authority containing host and port maps to expected host and port |
||
683 | * |
||
684 | * This is also a regression test to test that using ip-literals works along- |
||
685 | * side ipv4 and reg-name hosts incl. port numbers |
||
686 | * |
||
687 | * It was reported as Bug #20423 on 2014-10-06 18:25 UTC that |
||
688 | * http://[::1]// URI drops the host |
||
689 | * |
||
690 | * @param string $authority string |
||
691 | * @param string $expectedHost string |
||
692 | * @param string|bool $expectedPort string or FALSE |
||
693 | * |
||
694 | * @return void |
||
695 | * @dataProvider provideHostAndPort |
||
696 | * @covers Net_URL2::setAuthority() |
||
697 | * @link https://pear.php.net/bugs/bug.php?id=20423 |
||
698 | * @link http://tools.ietf.org/html/rfc3986#section-3.2.2 |
||
699 | * @link http://tools.ietf.org/html/rfc3986#section-3.2 |
||
700 | * @link http://tools.ietf.org/html/rfc3986#section-3.2.3 |
||
701 | */ |
||
702 | public function testHostAndPort($authority, $expectedHost, $expectedPort) |
||
709 | |||
710 | /** |
||
711 | * This is a regression test to test that Net_URL2::getQueryVariables() does |
||
712 | * not have a problem with nested array values in form of stacked brackets and |
||
713 | * was reported as Bug #17036 on 2010-01-26 15:48 UTC that there would be |
||
714 | * a problem with parsed query string. |
||
715 | * |
||
716 | * @link https://pear.php.net/bugs/bug.php?id=17036 |
||
717 | * @covers Net_URL2::getQueryVariables |
||
718 | * @return void |
||
719 | */ |
||
720 | public function test17036() |
||
733 | |||
734 | /** |
||
735 | * This is a regression test to test that resolve() does |
||
736 | * merge the path if the base path is empty as the opposite |
||
737 | * was reported as Bug #19176 on 2011-12-31 02:07 UTC |
||
738 | * |
||
739 | * @return void |
||
740 | */ |
||
741 | public function test19176() |
||
747 | |||
748 | /** |
||
749 | * This is a regression test that removeDotSegments('0') is |
||
750 | * working as it was reported as not-working in Bug #19315 |
||
751 | * on 2012-03-04 04:18 UTC. |
||
752 | * |
||
753 | * @return void |
||
754 | */ |
||
755 | public function test19315() |
||
772 | |||
773 | /** |
||
774 | * This is a regression test to test that recovering from |
||
775 | * a wrongly encoded URL is possible. |
||
776 | * |
||
777 | * It was requested as Request #19684 on 2011-12-31 02:07 UTC |
||
778 | * that redirects containing spaces should work. |
||
779 | * |
||
780 | * @return void |
||
781 | */ |
||
782 | public function test19684() |
||
811 | |||
812 | /** |
||
813 | * data provider of list of equivalent URLs. |
||
814 | * |
||
815 | * @see testNormalize |
||
816 | * @see testConstructSelf |
||
817 | * @return array |
||
818 | */ |
||
819 | public function provideEquivalentUrlLists() |
||
847 | |||
848 | /** |
||
849 | * This is a coverage test to invoke the normalize() |
||
850 | * method. |
||
851 | * |
||
852 | * @return void |
||
853 | * |
||
854 | * @dataProvider provideEquivalentUrlLists |
||
855 | */ |
||
856 | public function testNormalize() |
||
873 | |||
874 | /** |
||
875 | * This is a coverage test to invoke __get and __set |
||
876 | * |
||
877 | * @covers Net_URL2::__get |
||
878 | * @covers Net_URL2::__set |
||
879 | * @return void |
||
880 | */ |
||
881 | public function testMagicSetGet() |
||
893 | |||
894 | /** |
||
895 | * data provider of uri and normal URIs |
||
896 | * |
||
897 | * @return array |
||
898 | * @see testComponentRecompositionAndNormalization |
||
899 | */ |
||
900 | public function provideComposedAndNormalized() |
||
909 | |||
910 | /** |
||
911 | * Tests Net_URL2 RFC 3986 5.3. Component Recomposition in the light |
||
912 | * of normalization |
||
913 | * |
||
914 | * This is also a regression test to test that a missing authority works well |
||
915 | * with normalization |
||
916 | * |
||
917 | * It was reported as Bug #20418 on 2014-10-02 22:10 UTC that there is an |
||
918 | * Incorrect normalization of URI with missing authority |
||
919 | * |
||
920 | * @param string $uri URI |
||
921 | * |
||
922 | * @return void |
||
923 | * @covers Net_URL2::getUrl() |
||
924 | * @covers Net_URL2::normalize() |
||
925 | * @dataProvider provideComposedAndNormalized |
||
926 | * @link https://pear.php.net/bugs/bug.php?id=20418 |
||
927 | * @see testExampleUri |
||
928 | */ |
||
929 | public function testComponentRecompositionAndNormalization($uri) |
||
936 | |||
937 | /** |
||
938 | * Tests Net_URL2 ctors URL parameter works with objects implementing |
||
939 | * __toString(). |
||
940 | * |
||
941 | * @dataProvider provideEquivalentUrlLists |
||
942 | * @coversNothing |
||
943 | * @return void |
||
944 | */ |
||
945 | public function testConstructSelf() |
||
954 | |||
955 | /** |
||
956 | * This is a feature test to see that the userinfo's data is getting |
||
957 | * encoded as outlined in #19684. |
||
958 | * |
||
959 | * @covers Net_URL2::setAuthority |
||
960 | * @covers Net_URL2::setUserinfo |
||
961 | * @return void |
||
962 | */ |
||
963 | public function testEncodeDataUserinfoAuthority() |
||
974 | |||
975 | /** |
||
976 | * This is a regression test to test that using the |
||
977 | * host-name "0" does work with getAuthority() |
||
978 | * |
||
979 | * It was reported as Bug #20156 on 2013-12-27 22:56 UTC |
||
980 | * that setAuthority() with "0" as host would not work |
||
981 | * |
||
982 | * @covers Net_URL2::setAuthority |
||
983 | * @covers Net_URL2::getAuthority |
||
984 | * @covers Net_URL2::setHost |
||
985 | * @return void |
||
986 | */ |
||
987 | public function test20156() |
||
1000 | |||
1001 | /** |
||
1002 | * This is a regression test to test that setting "0" as path |
||
1003 | * does not break normalize(). |
||
1004 | * |
||
1005 | * It was reported as Bug #20157 on 2013-12-27 23:42 UTC that |
||
1006 | * normalize() with "0" as path would not work. |
||
1007 | * |
||
1008 | * @covers Net_URL2::normalize |
||
1009 | * @return void |
||
1010 | */ |
||
1011 | public function test20157() |
||
1019 | |||
1020 | /** |
||
1021 | * This is a regression test to ensure that fragment-only references can be |
||
1022 | * resolved to a non-absolute Base-URI. |
||
1023 | * |
||
1024 | * It was reported as Bug #20158 2013-12-28 14:49 UTC that fragment-only |
||
1025 | * references would not be resolved to non-absolute base URI |
||
1026 | * |
||
1027 | * @covers Net_URL2::resolve |
||
1028 | * @covers Net_URL2::_isFragmentOnly |
||
1029 | * @return void |
||
1030 | */ |
||
1031 | public function test20158() |
||
1037 | |||
1038 | /** |
||
1039 | * This is a regression test to ensure that authority and path are properly |
||
1040 | * combined when the path does not start with a slash which is the separator |
||
1041 | * character between authority and path. |
||
1042 | * |
||
1043 | * It was reported as Bug #20159 2013-12-28 17:18 UTC that authority |
||
1044 | * would not be terminated by slash |
||
1045 | * |
||
1046 | * @covers Net_URL2::getUrl |
||
1047 | * @return void |
||
1048 | */ |
||
1049 | public function test20159() |
||
1055 | |||
1056 | /** |
||
1057 | * This is a regression test to test that using the file:// URI scheme with |
||
1058 | * an empty (default) hostname has the empty authority preserved when the |
||
1059 | * full URL is build. |
||
1060 | * |
||
1061 | * It was reported as Bug #20304 on 2014-06-21 00:06 UTC |
||
1062 | * that file:// URI are crippled. |
||
1063 | * |
||
1064 | * Tests with a default authority for the "file" URI scheme |
||
1065 | * |
||
1066 | * @covers Net_URL2::getURL |
||
1067 | * @return void |
||
1068 | * @link https://pear.php.net/bugs/bug.php?id=20304 |
||
1069 | */ |
||
1070 | public function test20304() |
||
1088 | } |
||
1089 |
If you suppress an error, we recommend checking for the error condition explicitly: