UriTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Codeception\Util;
  3. class UriTest extends \Codeception\Test\Unit
  4. {
  5. // tests
  6. public function testUrlMerge()
  7. {
  8. $this->assertEquals(
  9. 'http://codeception.com/quickstart',
  10. Uri::mergeUrls('http://codeception.com/hello', '/quickstart'),
  11. 'merge paths'
  12. );
  13. $this->assertEquals(
  14. 'http://codeception.com/hello/davert',
  15. Uri::mergeUrls('http://codeception.com/hello/world', 'davert'),
  16. 'merge relative urls'
  17. );
  18. $this->assertEquals(
  19. 'https://github.com/codeception/codeception',
  20. Uri::mergeUrls('http://codeception.com/hello/world', 'https://github.com/codeception/codeception'),
  21. 'merge absolute urls'
  22. );
  23. }
  24. /**
  25. * @Issue https://github.com/Codeception/Codeception/pull/2141
  26. */
  27. public function testMergingScheme()
  28. {
  29. $this->assertEquals(
  30. 'https://google.com/account/',
  31. Uri::mergeUrls('http://google.com/', 'https://google.com/account/')
  32. );
  33. $this->assertEquals('https://facebook.com/', Uri::mergeUrls('https://google.com/test/', '//facebook.com/'));
  34. $this->assertEquals(
  35. 'https://facebook.com/#anchor2',
  36. Uri::mergeUrls('https://google.com/?param=1#anchor', '//facebook.com/#anchor2')
  37. );
  38. }
  39. /**
  40. * @Issue https://github.com/Codeception/Codeception/pull/2841
  41. */
  42. public function testMergingPath()
  43. {
  44. $this->assertEquals('/form/?param=1#anchor', Uri::mergeUrls('/form/?param=1', '#anchor'));
  45. $this->assertEquals('/form/?param=1#anchor2', Uri::mergeUrls('/form/?param=1#anchor1', '#anchor2'));
  46. $this->assertEquals('/form/?param=2', Uri::mergeUrls('/form/?param=1#anchor', '?param=2'));
  47. $this->assertEquals('/page/', Uri::mergeUrls('/form/?param=1#anchor', '/page/'));
  48. }
  49. /**
  50. * @Issue https://github.com/Codeception/Codeception/pull/4847
  51. */
  52. public function testMergingNonParsingPath()
  53. {
  54. $this->assertEquals('/3.0/en/index/page:5', Uri::mergeUrls('https://cakephp.org/', '/3.0/en/index/page:5'));
  55. }
  56. /**
  57. * @Issue https://github.com/Codeception/Codeception/pull/2499
  58. */
  59. public function testAppendAnchor()
  60. {
  61. $this->assertEquals(
  62. 'http://codeception.com/quickstart#anchor',
  63. Uri::appendPath('http://codeception.com/quickstart', '#anchor')
  64. );
  65. $this->assertEquals(
  66. 'http://codeception.com/quickstart#anchor',
  67. Uri::appendPath('http://codeception.com/quickstart#first', '#anchor')
  68. );
  69. }
  70. public function testAppendPath()
  71. {
  72. $this->assertEquals(
  73. 'http://codeception.com/quickstart/path',
  74. Uri::appendPath('http://codeception.com/quickstart', 'path')
  75. );
  76. $this->assertEquals(
  77. 'http://codeception.com/quickstart/path',
  78. Uri::appendPath('http://codeception.com/quickstart', '/path')
  79. );
  80. }
  81. public function testAppendEmptyPath()
  82. {
  83. $this->assertEquals(
  84. 'http://codeception.com/quickstart',
  85. Uri::appendPath('http://codeception.com/quickstart', '')
  86. );
  87. }
  88. public function testAppendPathRemovesQueryStringAndAnchor()
  89. {
  90. $this->assertEquals(
  91. 'http://codeception.com/quickstart',
  92. Uri::appendPath('http://codeception.com/quickstart?a=b#c', '')
  93. );
  94. }
  95. public function testMergeUrlsWhenBaseUriHasNoTrailingSlashAndUriPathHasNoLeadingSlash()
  96. {
  97. $this->assertEquals(
  98. 'http://codeception.com/test',
  99. Uri::mergeUrls('http://codeception.com', 'test'));
  100. }
  101. public function testMergeUrlsWhenBaseUriEndsWithSlashButUriPathHasNoLeadingSlash()
  102. {
  103. $this->assertEquals(
  104. 'http://codeception.com/test',
  105. Uri::mergeUrls('http://codeception.com/', 'test'));
  106. }
  107. }