diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..b55bf15 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,55 @@ +name: Tests + +on: + push: + branches: [ master, main ] + pull_request: + branches: [ master, main ] + +jobs: + test: + runs-on: ubuntu-latest + permissions: + contents: read + strategy: + fail-fast: false + matrix: + php-version: + - '5.6' + - '7.0' + - '7.1' + - '7.2' + - '7.3' + - '7.4' + - '8.0' + - '8.1' + - '8.2' + - '8.3' + - '8.4' + + name: PHP ${{ matrix.php-version }} + + steps: + - uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + coverage: none + + - name: Install dependencies + run: | + if [ "${{ matrix.php-version }}" = "5.6" ] || [ "${{ matrix.php-version }}" = "7.0" ] || [ "${{ matrix.php-version }}" = "7.1" ]; then + composer require --dev phpunit/phpunit:^5.7 --no-interaction --no-update + elif [ "${{ matrix.php-version }}" = "7.2" ] || [ "${{ matrix.php-version }}" = "7.3" ] || [ "${{ matrix.php-version }}" = "7.4" ]; then + composer require --dev phpunit/phpunit:^8.5 --no-interaction --no-update + elif [ "${{ matrix.php-version }}" = "8.0" ] || [ "${{ matrix.php-version }}" = "8.1" ]; then + composer require --dev phpunit/phpunit:^9.5 --no-interaction --no-update + else + composer require --dev phpunit/phpunit:^10.0 --no-interaction --no-update + fi + composer install --no-interaction --prefer-dist + + - name: Run tests + run: vendor/bin/phpunit diff --git a/.gitignore b/.gitignore index 245ce84..b9220cd 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ .idea build composer.lock -vendor \ No newline at end of file +vendor/ +.phpunit.cache +.phpunit.result.cache diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index dea4bf1..0000000 --- a/.travis.yml +++ /dev/null @@ -1,13 +0,0 @@ -sudo: false - -language: php -php: - - 5.3 - - 5.4 - - 5.5 - - 5.6 - - 7.0 - - 7.1 - - hhvm - -script: phpunit --coverage-text diff --git a/composer.json b/composer.json index a5d83de..639a06a 100644 --- a/composer.json +++ b/composer.json @@ -14,5 +14,8 @@ }, "autoload": { "files": ["src/http_build_url.php"] + }, + "require-dev": { + "phpunit/phpunit": "^5.7 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0" } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 62dfd81..0804712 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,7 +1,9 @@ - + - + ./tests @@ -10,12 +12,4 @@ ./src - - - - - \ No newline at end of file diff --git a/readme.md b/readme.md index cdf10f4..bd089cf 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # http_build_url() for PHP -[![Build Status](https://travis-ci.org/jakeasmith/http_build_url.png)](https://travis-ci.org/jakeasmith/http_build_url) +[![Tests](https://github.com/jakeasmith/http_build_url/actions/workflows/test.yml/badge.svg)](https://github.com/ozh/http_build_url/actions/workflows/test.yml) [![Code Climate](https://codeclimate.com/github/jakeasmith/http_build_url/badges/gpa.svg)](https://codeclimate.com/github/jakeasmith/http_build_url) [![Latest Stable Version](https://poser.pugx.org/jakeasmith/http_build_url/v/stable.png)](https://packagist.org/packages/jakeasmith/http_build_url) [![Total Downloads](https://poser.pugx.org/jakeasmith/http_build_url/downloads.png)](https://packagist.org/packages/jakeasmith/http_build_url) @@ -15,6 +15,136 @@ The easiest way to install this library is to use [Composer](https://getcomposer $ composer require jakeasmith/http_build_url ^1 ``` +## Usage + +The `http_build_url()` function builds a URL from an array of parts or merges two URLs together according to specified flags. + +### Basic Examples + +#### Building a URL from parts + +```php + 'https', + 'host' => 'www.example.com', + 'path' => '/path/to/page', + 'query' => 'foo=bar' +]); + +echo $url; // https://www.example.com/path/to/page?foo=bar +``` + +#### Merging URL parts + +```php + 'https', + 'host' => 'secure.example.com', + 'path' => '/new/path.php' +]); + +echo $new_url; // https://user:pass@secure.example.com:8080/new/path.php?a=b#files +``` + +### Advanced Examples with Flags + +The function accepts various flags to control how URLs are merged and manipulated: + +#### Joining paths + +```php + 'baz/qux'], + HTTP_URL_JOIN_PATH +); + +echo $url; // http://example.com/foo/bar/baz/qux +``` + +#### Joining query strings + +```php + 'foo=new&extra=param'], + HTTP_URL_JOIN_QUERY +); + +echo $url; // http://example.com/page?foo=new&old=value&extra=param +``` + +#### Stripping URL components + +```php + 'ftp', + 'host' => 'ftp.example.com', + 'path' => 'files/current/', + 'query' => 'a=c' + ], + HTTP_URL_STRIP_AUTH | HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY | HTTP_URL_STRIP_FRAGMENT +); + +echo $url; // ftp://ftp.example.com/pub/files/current/?a=c +``` + +### Available Flags + +- `HTTP_URL_REPLACE` (default) - Replace parts of the first URL with parts from the second +- `HTTP_URL_JOIN_PATH` - Join relative paths instead of replacing +- `HTTP_URL_JOIN_QUERY` - Merge query parameters instead of replacing +- `HTTP_URL_STRIP_USER` - Remove username from URL +- `HTTP_URL_STRIP_PASS` - Remove password from URL +- `HTTP_URL_STRIP_AUTH` - Remove both username and password +- `HTTP_URL_STRIP_PORT` - Remove port from URL +- `HTTP_URL_STRIP_PATH` - Remove path from URL +- `HTTP_URL_STRIP_QUERY` - Remove query string from URL +- `HTTP_URL_STRIP_FRAGMENT` - Remove fragment (hash) from URL +- `HTTP_URL_STRIP_ALL` - Remove all components except scheme and host + +Flags can be combined using the bitwise OR operator (`|`). + ## License This project is licensed under the MIT License - see the LICENSE file for details. diff --git a/tests/HttpBuildUrlTest.php b/tests/HttpBuildUrlTest.php index 40a3f15..bd2fbfa 100644 --- a/tests/HttpBuildUrlTest.php +++ b/tests/HttpBuildUrlTest.php @@ -1,6 +1,13 @@ assertSame($expected, $actual); } - public function trailingSlashProvider() + public static function trailingSlashProvider() { return array( array( @@ -180,7 +187,7 @@ public function testBitmasks($constant, $expected) $this->assertSame($expected, $actual); } - public function pathProvider() + public static function pathProvider() { return array( array('/donuts/brownies', 'http://user:pass@www.example.com:8080/donuts/brownies?a=b#files'), @@ -189,7 +196,7 @@ public function pathProvider() ); } - public function queryProvider() + public static function queryProvider() { return array( array('a=c', 'http://user:pass@www.example.com:8080/pub/index.php?a=c#files'), @@ -197,7 +204,7 @@ public function queryProvider() ); } - public function bitmaskProvider() + public static function bitmaskProvider() { return array( array('HTTP_URL_REPLACE', 'http://user:pass@www.example.com:8080/pub/index.php?a=b#files'), diff --git a/tests/bootstrap.php b/tests/bootstrap.php index cf8b03e..827bb4a 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,13 +1,17 @@ =' ) ) { - class_alias( 'PHPUnit\Framework\Assert', 'PHPUnit_Framework_Assert' ); - class_alias( 'PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase' ); - class_alias( 'PHPUnit\Framework\Error\Error', 'PHPUnit_Framework_Error' ); - class_alias( 'PHPUnit\Framework\Error\Notice', 'PHPUnit_Framework_Error_Notice' ); - class_alias( 'PHPUnit\Framework\Error\Warning', 'PHPUnit_Framework_Error_Warning' ); +// PHPUnit 6+ uses namespaced classes, create aliases for older PHPUnit versions compatibility +if ( class_exists( 'PHPUnit\Runner\Version' ) ) { + // PHPUnit 6+ + if ( ! class_exists( 'PHPUnit_Framework_TestCase' ) ) { + class_alias( 'PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase' ); + } +} else { + // PHPUnit 4-5 - ensure forward compatibility classes exist + if ( ! class_exists( 'PHPUnit\Framework\TestCase' ) && class_exists( 'PHPUnit_Framework_TestCase' ) ) { + class_alias( 'PHPUnit_Framework_TestCase', 'PHPUnit\Framework\TestCase' ); + } } // Past this point, tests will start