diff --git a/.gitattributes b/.gitattributes index 11192e1..ec6d62f 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,13 +1,11 @@ .github export-ignore bin export-ignore Tests export-ignore -vendor export-ignore .editorconfig export-ignore .gitattributes export-ignore .gitignore export-ignore -composer.lock export-ignore LICENSE export-ignore -phpcs.xml export-ignore +phpcs.xml.dist export-ignore phpstan.neon.dist export-ignore README.md export-ignore diff --git a/.github/workflows/lint_phpcs.yml b/.github/workflows/lint_phpcs.yml deleted file mode 100644 index 484ecd1..0000000 --- a/.github/workflows/lint_phpcs.yml +++ /dev/null @@ -1,36 +0,0 @@ -name: PHP CodeSniffer lint - -on: - pull_request: - branches: - - trunk - - develop - -jobs: - run: - runs-on: ${{ matrix.operating-system }} - - strategy: - fail-fast: false - matrix: - operating-system: [ubuntu-latest] - php-versions: ['7.4'] - - name: Lint with PHPCS. PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }}. - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-versions }} - coverage: none # XDebug can be enabled here 'coverage: xdebug' - tools: composer:v2 - - - name: Install dependencies - run: composer install --no-interaction --no-progress - - - name: Lint with PHPCS - run: composer phpcs - diff --git a/.github/workflows/lint_phpstan.yml b/.github/workflows/lint_phpstan.yml deleted file mode 100644 index 2079b44..0000000 --- a/.github/workflows/lint_phpstan.yml +++ /dev/null @@ -1,36 +0,0 @@ -name: PHPStan lint - -on: - pull_request: - branches: - - trunk - - develop - -jobs: - run: - runs-on: ${{ matrix.operating-system }} - - strategy: - fail-fast: false - matrix: - operating-system: [ubuntu-latest] - php-versions: ['7.4'] - - name: Lint with PHPStan. PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }}. - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-versions }} - coverage: none # XDebug can be enabled here 'coverage: xdebug' - tools: composer:v2 - - - name: Install dependencies - run: composer install --no-interaction --no-progress - - # Linting - - name: Lint with phpstan - run: composer run-stan diff --git a/.github/workflows/phpcs.yml b/.github/workflows/phpcs.yml new file mode 100644 index 0000000..aa1391e --- /dev/null +++ b/.github/workflows/phpcs.yml @@ -0,0 +1,15 @@ +name: PHP CodeSniffer + +on: + pull_request: + # Allow manually triggering the workflow. + workflow_dispatch: + +# Cancel all previous workflow runs for the same branch that have not yet completed. +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + phpcs: + uses: wp-media/workflows/.github/workflows/phpcs.yml@main diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 0000000..9681221 --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,15 @@ +name: PHPStan + +on: + pull_request: + # Allow manually triggering the workflow. + workflow_dispatch: + +# Cancel all previous workflow runs for the same branch that have not yet completed. +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + phpstan: + uses: wp-media/workflows/.github/workflows/phpstan.yml@main diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml new file mode 100644 index 0000000..c7c8ab2 --- /dev/null +++ b/.github/workflows/phpunit.yml @@ -0,0 +1,51 @@ +name: PHPUnit + +on: + pull_request: + # Allow manually triggering the workflow. + workflow_dispatch: + +# Cancel all previous workflow runs for the same branch that have not yet completed. +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + run: + name: PHPUnit + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.2' + ini-values: 'memory_limit=1G' + coverage: none + tools: cs2pr + + - name: Install Composer dependencies + uses: "ramsey/composer-install@v3" + with: + # Bust the cache at least once a month - output format: YYYY-MM. + custom-cache-suffix: $(date -u "+%Y-%m") + + - name: Install SVN + run: sudo apt-get install subversion + + - name: Start mysql service + run: sudo /etc/init.d/mysql start + + - name: Install tests + run: bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1:3306 latest + + - name: Mysql8 auth plugin workaround + run: sudo mysql -u root -proot -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';" + + - name: Run PHPUnit unit tests + run: composer test-unit + + - name: Run PHPUnit integration tests + run: composer test-integration diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml deleted file mode 100644 index e194d0d..0000000 --- a/.github/workflows/tests.yml +++ /dev/null @@ -1,67 +0,0 @@ -name: Unit/Integration tests - -on: - pull_request: - branches: - - trunk - - develop - -jobs: - run: - runs-on: ${{ matrix.operating-system }} - - strategy: - fail-fast: false - matrix: - operating-system: [ubuntu-latest] - php-versions: ['8.0', '7.3', '7.4'] - wp-versions: ['latest'] - - name: WP ${{ matrix.wp-versions }} with PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }}. - - env: - WP_TESTS_DIR: "/tmp/tests/phpunit" - WP_CORE_DIR: "/tmp/wordpress-develop" - - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-versions }} - coverage: none # XDebug can be enabled here 'coverage: xdebug' - tools: composer:v2 - - - name: Start mysql service - run: sudo /etc/init.d/mysql start - - - name: Setup problem matchers for PHP - run: echo "::add-matcher::${{ runner.tool_cache }}/php.json" - - - name: Setup problem matchers for PHPUnit - run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" - - - name: Get composer cache directory - id: composercache - run: echo "::set-output name=dir::$(composer config cache-files-dir)" - - - name: Cache dependencies - uses: actions/cache@v2 - with: - path: ${{ steps.composercache.outputs.dir }} - key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} - restore-keys: ${{ runner.os }}-composer- - - - name: Install dependencies - run: composer install --no-interaction --no-progress --ignore-platform-reqs - - - name: Install tests - run: bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1:3306 ${{ matrix.wp-versions }} - - - name: Mysql8 auth plugin workaround - run: sudo mysql -u root -proot -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';" - - - name: Test - run: composer run-tests diff --git a/.github/workflows/tests_legacy.yml b/.github/workflows/tests_legacy.yml deleted file mode 100644 index 9f5d5c8..0000000 --- a/.github/workflows/tests_legacy.yml +++ /dev/null @@ -1,70 +0,0 @@ -name: Unit/Integration tests - Legacy - -on: - pull_request: - branches: - - trunk - - develop - -jobs: - run: - runs-on: ${{ matrix.operating-system }} - - strategy: - fail-fast: false - matrix: - operating-system: [ubuntu-latest] - php-versions: ['7.1', '7.2'] - wp-versions: ['5.3.6'] - - name: WP ${{ matrix.wp-versions }} with PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }}. - - env: - WP_TESTS_DIR: "/tmp/tests/phpunit" - WP_CORE_DIR: "/tmp/wordpress-develop" - - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-versions }} - coverage: none # XDebug can be enabled here 'coverage: xdebug' - tools: composer:v2 - - - name: Start mysql service - run: sudo /etc/init.d/mysql start - - - name: Setup problem matchers for PHP - run: echo "::add-matcher::${{ runner.tool_cache }}/php.json" - - - name: Setup problem matchers for PHPUnit - run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" - - - name: Get composer cache directory - id: composercache - run: echo "::set-output name=dir::$(composer config cache-files-dir)" - - - name: Cache dependencies - uses: actions/cache@v2 - with: - path: ${{ steps.composercache.outputs.dir }} - key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} - restore-keys: ${{ runner.os }}-composer- - - - name: Remove unmet dependencies by legacy versions - run: composer remove --dev phpstan/phpstan szepeviktor/phpstan-wordpress - - - name: Install dependencies - run: composer install --no-interaction --no-progress - - - name: Install tests - run: bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1:3306 ${{ matrix.wp-versions }} - - - name: Mysql8 auth plugin workaround - run: sudo mysql -u root -proot -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';" - - - name: Test - run: composer run-tests diff --git a/.gitignore b/.gitignore index 758269d..55b84c1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor/* composer.lock +.phpunit.result.cache diff --git a/Tests/Fixtures/OptionArray/getOptions.php b/Tests/Fixtures/OptionArray/GetOptionsTest.php similarity index 81% rename from Tests/Fixtures/OptionArray/getOptions.php rename to Tests/Fixtures/OptionArray/GetOptionsTest.php index a61a829..be57922 100644 --- a/Tests/Fixtures/OptionArray/getOptions.php +++ b/Tests/Fixtures/OptionArray/GetOptionsTest.php @@ -2,7 +2,7 @@ return [ 'test_data' => [ - 'testShouldReturnEmptyArray' => [ + 'testShouldReturnEmptyArray' => [ 'option' => [], ], 'testShouldReturnArrayWithData' => [ diff --git a/Tests/Fixtures/OptionArray/get.php b/Tests/Fixtures/OptionArray/GetTest.php similarity index 79% rename from Tests/Fixtures/OptionArray/get.php rename to Tests/Fixtures/OptionArray/GetTest.php index 0fced92..d8c9819 100644 --- a/Tests/Fixtures/OptionArray/get.php +++ b/Tests/Fixtures/OptionArray/GetTest.php @@ -2,8 +2,8 @@ return [ 'test_data' => [ - 'testShouldReturnFilteredValueWhenPreFiltering' => [ - 'option' => [ + 'testShouldReturnFilteredValueWhenPreFiltering' => [ + 'option' => [ 'foo' => 'bar', 'test' => 0, 'rocket' => 'no', @@ -17,7 +17,7 @@ 'expected' => 'yes', ], 'testShouldReturnValueWhenExistsAndNoPrefiltering' => [ - 'option' => [ + 'option' => [ 'foo' => 'bar', 'test' => 0, 'rocket' => 'no', @@ -29,8 +29,8 @@ ], 'expected' => 'no', ], - 'testShouldReturnDefaultWhenKeyDoesntExist' => [ - 'option' => [ + 'testShouldReturnDefaultWhenKeyDoesntExist' => [ + 'option' => [ 'foo' => 'bar', 'test' => 0, 'rocket' => 'no', @@ -42,8 +42,8 @@ ], 'expected' => 'default', ], - 'testShouldReturnFilteredValueWhenPostFiltering' => [ - 'option' => [ + 'testShouldReturnFilteredValueWhenPostFiltering' => [ + 'option' => [ 'foo' => 'bar', 'test' => 0, 'rocket' => 'no', diff --git a/Tests/Fixtures/OptionArray/has.php b/Tests/Fixtures/OptionArray/HasTest.php similarity index 80% rename from Tests/Fixtures/OptionArray/has.php rename to Tests/Fixtures/OptionArray/HasTest.php index b4c8f5d..fc0e70a 100644 --- a/Tests/Fixtures/OptionArray/has.php +++ b/Tests/Fixtures/OptionArray/HasTest.php @@ -9,10 +9,10 @@ 'key' => 'foobar', 'expected' => false, ], - 'testShouldReturnTrueWhenArrayHasKey' => [ + 'testShouldReturnTrueWhenArrayHasKey' => [ 'option' => [ 'test' => 'value', - 'foobar' => 'foo' + 'foobar' => 'foo', ], 'key' => 'foobar', 'expected' => true, diff --git a/Tests/Fixtures/OptionArray/set.php b/Tests/Fixtures/OptionArray/SetTest.php similarity index 75% rename from Tests/Fixtures/OptionArray/set.php rename to Tests/Fixtures/OptionArray/SetTest.php index f763452..4da1cb2 100644 --- a/Tests/Fixtures/OptionArray/set.php +++ b/Tests/Fixtures/OptionArray/SetTest.php @@ -3,12 +3,12 @@ return [ 'test_data' => [ 'testShouldAddOptionWhenDoesntExist' => [ - 'option' => [ + 'option' => [ 'foo' => 'bar', 'test' => 0, ], - 'key' => 'rocket', - 'value' => 'yes', + 'key' => 'rocket', + 'value' => 'yes', 'expected' => [ 'foo' => 'bar', 'test' => 0, @@ -16,13 +16,13 @@ ], ], 'testShouldOverrideOptionWhenExists' => [ - 'option' => [ + 'option' => [ 'foo' => 'bar', 'test' => 0, 'rocket' => 'no', ], - 'key' => 'rocket', - 'value' => 'yes', + 'key' => 'rocket', + 'value' => 'yes', 'expected' => [ 'foo' => 'bar', 'test' => 0, diff --git a/Tests/Fixtures/OptionArray/setValues.php b/Tests/Fixtures/OptionArray/SetValuesTest.php similarity index 82% rename from Tests/Fixtures/OptionArray/setValues.php rename to Tests/Fixtures/OptionArray/SetValuesTest.php index be3d22e..04aa909 100644 --- a/Tests/Fixtures/OptionArray/setValues.php +++ b/Tests/Fixtures/OptionArray/SetValuesTest.php @@ -3,13 +3,13 @@ return [ 'test_data' => [ 'testShouldAddOptionsWhenDoesntExist' => [ - 'option' => [ + 'option' => [ 'foo' => 'bar', 'test' => 0, ], - 'values' => [ + 'values' => [ 'rocket' => 'yes', - 'enabled' => true, + 'enabled' => true, ], 'expected' => [ 'foo' => 'bar', @@ -19,15 +19,15 @@ ], ], 'testShouldOverrideOptionsWhenExists' => [ - 'option' => [ + 'option' => [ 'foo' => 'bar', 'test' => 0, 'rocket' => 'no', 'enabled' => false, ], - 'values' => [ + 'values' => [ 'rocket' => 'yes', - 'enabled' => true, + 'enabled' => true, ], 'expected' => [ 'foo' => 'bar', diff --git a/Tests/Fixtures/SiteOptions/delete.php b/Tests/Fixtures/Options/DeleteTest.php similarity index 86% rename from Tests/Fixtures/SiteOptions/delete.php rename to Tests/Fixtures/Options/DeleteTest.php index 56da957..b8365a5 100644 --- a/Tests/Fixtures/SiteOptions/delete.php +++ b/Tests/Fixtures/Options/DeleteTest.php @@ -2,7 +2,7 @@ return [ 'test_data' => [ - 'testShouldDeletePrefixedOption' => [ + 'testShouldDeletePrefixedOption' => [ 'option' => [ 'prefix' => 'wp_', 'name' => 'rocket', diff --git a/Tests/Fixtures/Options/getOptionName.php b/Tests/Fixtures/Options/GetOptionNameTest.php similarity index 83% rename from Tests/Fixtures/Options/getOptionName.php rename to Tests/Fixtures/Options/GetOptionNameTest.php index f2a415e..c5b8f7d 100644 --- a/Tests/Fixtures/Options/getOptionName.php +++ b/Tests/Fixtures/Options/GetOptionNameTest.php @@ -2,7 +2,7 @@ return [ 'test_data' => [ - 'testShouldReturnPrefixedNameWhenPrefixIsSet' => [ + 'testShouldReturnPrefixedNameWhenPrefixIsSet' => [ 'prefix' => 'wpmedia_options_', 'option' => 'rocket', 'expected' => 'wpmedia_options_rocket', diff --git a/Tests/Fixtures/SiteOptions/get.php b/Tests/Fixtures/Options/GetTest.php similarity index 82% rename from Tests/Fixtures/SiteOptions/get.php rename to Tests/Fixtures/Options/GetTest.php index 5be891f..49e540b 100644 --- a/Tests/Fixtures/SiteOptions/get.php +++ b/Tests/Fixtures/Options/GetTest.php @@ -3,15 +3,15 @@ return [ 'test_data' => [ 'testShouldReturnDefaultWhenOptionDoesntExist' => [ - 'option' => [ + 'option' => [ 'name' => 'rocket', 'default' => [], 'value' => null, ], 'expected' => [], ], - 'testShouldReturnOptionWhenExists' => [ - 'option' => [ + 'testShouldReturnOptionWhenExists' => [ + 'option' => [ 'name' => 'rocket', 'default' => '', 'value' => 'foobar', @@ -19,7 +19,7 @@ 'expected' => 'foobar', ], 'testShouldReturnOptionAsArrayWhenExistsAsStringAndDefaultIsArray' => [ - 'option' => [ + 'option' => [ 'name' => 'rocket', 'default' => [], 'value' => 'foobar', diff --git a/Tests/Fixtures/Options/has.php b/Tests/Fixtures/Options/HasTest.php similarity index 68% rename from Tests/Fixtures/Options/has.php rename to Tests/Fixtures/Options/HasTest.php index b941dbd..137fa3b 100644 --- a/Tests/Fixtures/Options/has.php +++ b/Tests/Fixtures/Options/HasTest.php @@ -4,13 +4,13 @@ 'test_data' => [ 'testShouldReturnFalseWhenOptionNotExist' => [ 'option' => [ - 'name' => 'rocket', + 'name' => 'rocket', 'value' => false, ], ], - 'testShouldReturnTrueWhenOptionExist' => [ + 'testShouldReturnTrueWhenOptionExist' => [ 'option' => [ - 'name' => 'rocket', + 'name' => 'rocket', 'value' => [ 'key' => 'value', ], diff --git a/Tests/Fixtures/Options/set.php b/Tests/Fixtures/Options/SetTest.php similarity index 85% rename from Tests/Fixtures/Options/set.php rename to Tests/Fixtures/Options/SetTest.php index 6ecba50..e1a9314 100644 --- a/Tests/Fixtures/Options/set.php +++ b/Tests/Fixtures/Options/SetTest.php @@ -2,7 +2,7 @@ return [ 'test_data' => [ - 'testShouldSetPrefixedOption' => [ + 'testShouldSetPrefixedOption' => [ 'prefix' => 'wpmedia_options_', 'name' => 'rocket', 'value' => 'foobar', diff --git a/Tests/Fixtures/Options/delete.php b/Tests/Fixtures/SiteOptions/DeleteTest.php similarity index 86% rename from Tests/Fixtures/Options/delete.php rename to Tests/Fixtures/SiteOptions/DeleteTest.php index 56da957..b8365a5 100644 --- a/Tests/Fixtures/Options/delete.php +++ b/Tests/Fixtures/SiteOptions/DeleteTest.php @@ -2,7 +2,7 @@ return [ 'test_data' => [ - 'testShouldDeletePrefixedOption' => [ + 'testShouldDeletePrefixedOption' => [ 'option' => [ 'prefix' => 'wp_', 'name' => 'rocket', diff --git a/Tests/Fixtures/Options/get.php b/Tests/Fixtures/SiteOptions/GetTest.php similarity index 82% rename from Tests/Fixtures/Options/get.php rename to Tests/Fixtures/SiteOptions/GetTest.php index 5be891f..49e540b 100644 --- a/Tests/Fixtures/Options/get.php +++ b/Tests/Fixtures/SiteOptions/GetTest.php @@ -3,15 +3,15 @@ return [ 'test_data' => [ 'testShouldReturnDefaultWhenOptionDoesntExist' => [ - 'option' => [ + 'option' => [ 'name' => 'rocket', 'default' => [], 'value' => null, ], 'expected' => [], ], - 'testShouldReturnOptionWhenExists' => [ - 'option' => [ + 'testShouldReturnOptionWhenExists' => [ + 'option' => [ 'name' => 'rocket', 'default' => '', 'value' => 'foobar', @@ -19,7 +19,7 @@ 'expected' => 'foobar', ], 'testShouldReturnOptionAsArrayWhenExistsAsStringAndDefaultIsArray' => [ - 'option' => [ + 'option' => [ 'name' => 'rocket', 'default' => [], 'value' => 'foobar', diff --git a/Tests/Fixtures/SiteOptions/set.php b/Tests/Fixtures/SiteOptions/SetTest.php similarity index 85% rename from Tests/Fixtures/SiteOptions/set.php rename to Tests/Fixtures/SiteOptions/SetTest.php index 6ecba50..e1a9314 100644 --- a/Tests/Fixtures/SiteOptions/set.php +++ b/Tests/Fixtures/SiteOptions/SetTest.php @@ -2,7 +2,7 @@ return [ 'test_data' => [ - 'testShouldSetPrefixedOption' => [ + 'testShouldSetPrefixedOption' => [ 'prefix' => 'wpmedia_options_', 'name' => 'rocket', 'value' => 'foobar', diff --git a/Tests/Integration/OptionArray/get.php b/Tests/Integration/OptionArray/GetTest.php similarity index 64% rename from Tests/Integration/OptionArray/get.php rename to Tests/Integration/OptionArray/GetTest.php index d86721c..b4d3a73 100644 --- a/Tests/Integration/OptionArray/get.php +++ b/Tests/Integration/OptionArray/GetTest.php @@ -1,4 +1,5 @@ $option Option values. + * @param string $key Key to get. + * @param array $config Config for pre and post filters. + * @param mixed $expected Expected value. */ public function testShouldReturnExpectedValue( $option, $key, $config, $expected ) { add_option( 'wpmedia', $option ); @@ -48,10 +68,20 @@ public function testShouldReturnExpectedValue( $option, $key, $config, $expected remove_filter( "wpmedia_get_option_{$key}", [ $this, 'set_post_value' ] ); } + /** + * Set the pre value for the option. + * + * @return mixed + */ public function set_pre_value() { return $this->pre_value; } + /** + * Set the post value for the option. + * + * @return mixed + */ public function set_post_value() { return $this->post_value; } diff --git a/Tests/Integration/Options/delete.php b/Tests/Integration/Options/DeleteTest.php similarity index 72% rename from Tests/Integration/Options/delete.php rename to Tests/Integration/Options/DeleteTest.php index bbc76bc..70ebb35 100644 --- a/Tests/Integration/Options/delete.php +++ b/Tests/Integration/Options/DeleteTest.php @@ -1,4 +1,5 @@ $option Array of test data. */ - public function testShoulDeleteOption( $option ) { + public function testShouldDeleteOption( $option ) { $options = new Options( $option['prefix'] ); add_option( "{$option['prefix']}{$option['name']}", $option['value'] ); diff --git a/Tests/Integration/Options/get.php b/Tests/Integration/Options/GetTest.php similarity index 76% rename from Tests/Integration/Options/get.php rename to Tests/Integration/Options/GetTest.php index 4f2b8f7..5264460 100644 --- a/Tests/Integration/Options/get.php +++ b/Tests/Integration/Options/GetTest.php @@ -1,4 +1,5 @@ $option Array of test data. + * @param mixed $expected Expected value. */ public function testShouldReturnExpectedValue( $option, $expected ) { $options = new Options(); - if ( ! is_null( $option['value'] ) ) { add_option( $option['name'], $option['value'] ); } diff --git a/Tests/Integration/Options/HasTest.php b/Tests/Integration/Options/HasTest.php new file mode 100644 index 0000000..40cf76c --- /dev/null +++ b/Tests/Integration/Options/HasTest.php @@ -0,0 +1,34 @@ + $option Array of test data. + */ + public function testShouldReturnExpected( $option ) { + if ( false !== $option['value'] ) { + add_option( $option['name'], $option['value'] ); + } + + $options = new Options(); + + if ( false !== $option['value'] ) { + $this->assertTrue( $options->has( $option['name'] ) ); + } else { + $this->assertFalse( $options->has( $option['name'] ) ); + } + + delete_option( 'rocket' ); + } +} diff --git a/Tests/Integration/Options/set.php b/Tests/Integration/Options/SetTest.php similarity index 68% rename from Tests/Integration/Options/set.php rename to Tests/Integration/Options/SetTest.php index cc89806..a64195d 100644 --- a/Tests/Integration/Options/set.php +++ b/Tests/Integration/Options/SetTest.php @@ -1,4 +1,5 @@ assertTrue( $options->has( $option['name'] ) ); - } else { - $this->assertFalse( $options->has( $option['name'] ) ); - } - - delete_option( 'rocket' ); - } -} diff --git a/Tests/Integration/SiteOptions/delete.php b/Tests/Integration/SiteOptions/DeleteTest.php similarity index 81% rename from Tests/Integration/SiteOptions/delete.php rename to Tests/Integration/SiteOptions/DeleteTest.php index c89c25a..25b907d 100644 --- a/Tests/Integration/SiteOptions/delete.php +++ b/Tests/Integration/SiteOptions/DeleteTest.php @@ -1,4 +1,5 @@ $option Array of test data. */ public function testShoulDeleteOption( $option ) { $options = new SiteOptions( $option['prefix'] ); diff --git a/Tests/Integration/SiteOptions/get.php b/Tests/Integration/SiteOptions/GetTest.php similarity index 77% rename from Tests/Integration/SiteOptions/get.php rename to Tests/Integration/SiteOptions/GetTest.php index 24c23b0..1028bed 100644 --- a/Tests/Integration/SiteOptions/get.php +++ b/Tests/Integration/SiteOptions/GetTest.php @@ -1,4 +1,5 @@ $option Array of test data. + * @param mixed $expected Expected value. */ public function testShouldReturnExpectedValue( $option, $expected ) { $options = new SiteOptions(); - if ( ! is_null( $option['value'] ) ) { add_site_option( $option['name'], $option['value'] ); } diff --git a/Tests/Integration/SiteOptions/set.php b/Tests/Integration/SiteOptions/SetTest.php similarity index 68% rename from Tests/Integration/SiteOptions/set.php rename to Tests/Integration/SiteOptions/SetTest.php index cea064e..12bfecc 100644 --- a/Tests/Integration/SiteOptions/set.php +++ b/Tests/Integration/SiteOptions/SetTest.php @@ -1,4 +1,5 @@ } + */ protected $config; + /** + * Setup method for the test case. + * + * @return void + */ + public function set_up() { + parent::set_up(); - public function setUp() { if ( empty( $this->config ) ) { $this->loadTestDataConfig(); } - - parent::setUp(); } - public function configTestData() { + /** + * Get the test data configuration. + * + * @return array + */ + public function configTestData(): array { if ( empty( $this->config ) ) { $this->loadTestDataConfig(); } @@ -27,10 +42,19 @@ public function configTestData() { : $this->config; } - protected function loadTestDataConfig() { + /** + * Load test data configuration. + * + * @return void + */ + protected function loadTestDataConfig(): void { $obj = new ReflectionObject( $this ); $filename = $obj->getFileName(); + if ( false === $filename ) { + return; + } + $this->config = $this->getTestData( dirname( $filename ), basename( $filename, '.php' ) ); } } diff --git a/Tests/Integration/bootstrap.php b/Tests/Integration/bootstrap.php new file mode 100644 index 0000000..20664dd --- /dev/null +++ b/Tests/Integration/bootstrap.php @@ -0,0 +1,8 @@ + + + + + ../../inc + + + + + . + + + diff --git a/Tests/Unit/OptionArray/getOptions.php b/Tests/Unit/OptionArray/GetOptionsTest.php similarity index 76% rename from Tests/Unit/OptionArray/getOptions.php rename to Tests/Unit/OptionArray/GetOptionsTest.php index 2737766..dc361dc 100644 --- a/Tests/Unit/OptionArray/getOptions.php +++ b/Tests/Unit/OptionArray/GetOptionsTest.php @@ -1,4 +1,5 @@ $option Value of the option array. */ public function testShouldReturnExpectedArray( $option ) { $options = new OptionArray( $option, 'wpmedia' ); diff --git a/Tests/Unit/OptionArray/get.php b/Tests/Unit/OptionArray/GetTest.php similarity index 75% rename from Tests/Unit/OptionArray/get.php rename to Tests/Unit/OptionArray/GetTest.php index 64c98d7..1816622 100644 --- a/Tests/Unit/OptionArray/get.php +++ b/Tests/Unit/OptionArray/GetTest.php @@ -1,4 +1,5 @@ $option Option values. + * @param string $key Key to get. + * @param array $config Config for pre and post filters. + * @param mixed $expected Expected value. */ public function testShouldReturnExpectedValue( $option, $key, $config, $expected ) { $options = Mockery::mock( OptionArray::class . '[has]', [ $option, 'wpmedia' ] ); @@ -31,7 +35,7 @@ public function testShouldReturnExpectedValue( $option, $key, $config, $expected $options->shouldReceive( 'has' ) ->once() ->with( $key ) - ->andReturn( $config[ 'exists' ] ); + ->andReturn( $config['exists'] ); } if ( isset( $config['post'] ) ) { diff --git a/Tests/Unit/OptionArray/has.php b/Tests/Unit/OptionArray/HasTest.php similarity index 59% rename from Tests/Unit/OptionArray/has.php rename to Tests/Unit/OptionArray/HasTest.php index 5b200c8..1b51674 100644 --- a/Tests/Unit/OptionArray/has.php +++ b/Tests/Unit/OptionArray/HasTest.php @@ -1,4 +1,5 @@ $option Value of the option array. + * @param string $key Key to check in the option array. + * @param bool $expected Expected result. */ public function testShouldReturnExpected( $option, $key, $expected ) { $options = new OptionArray( $option, 'wpmedia' ); - $this->assertSame( + $this->assertSame( $expected, $options->has( $key ) ); diff --git a/Tests/Unit/OptionArray/set.php b/Tests/Unit/OptionArray/SetTest.php similarity index 58% rename from Tests/Unit/OptionArray/set.php rename to Tests/Unit/OptionArray/SetTest.php index b3bfd22..1a559d4 100644 --- a/Tests/Unit/OptionArray/set.php +++ b/Tests/Unit/OptionArray/SetTest.php @@ -1,4 +1,5 @@ $option Value of the option array. + * @param string $key Key to set in the option array. + * @param mixed $value Value to set for the given key. + * @param array $expected Expected state of the option array after setting the value. */ public function testShouldSetValue( $option, $key, $value, $expected ) { $options = new OptionArray( $option, 'wpmedia' ); diff --git a/Tests/Unit/OptionArray/setValues.php b/Tests/Unit/OptionArray/SetValuesTest.php similarity index 68% rename from Tests/Unit/OptionArray/setValues.php rename to Tests/Unit/OptionArray/SetValuesTest.php index 0cad95c..5493d14 100644 --- a/Tests/Unit/OptionArray/setValues.php +++ b/Tests/Unit/OptionArray/SetValuesTest.php @@ -1,4 +1,5 @@ $option Array of data to configure the option. + * @param array $values Values to set. + * @param array $expected Expected values after setting. */ public function testShouldSetValues( $option, $values, $expected ) { $options = new OptionArray( $option, 'wpmedia' ); diff --git a/Tests/Unit/Options/GetOptionNameTest.php b/Tests/Unit/Options/GetOptionNameTest.php new file mode 100644 index 0000000..f3214f6 --- /dev/null +++ b/Tests/Unit/Options/GetOptionNameTest.php @@ -0,0 +1,29 @@ +assertSame( + $expected, + $options->get_option_name( $name ) + ); + } +} diff --git a/Tests/Unit/Options/get.php b/Tests/Unit/Options/GetTest.php similarity index 82% rename from Tests/Unit/Options/get.php rename to Tests/Unit/Options/GetTest.php index c41d913..da951e1 100644 --- a/Tests/Unit/Options/get.php +++ b/Tests/Unit/Options/GetTest.php @@ -1,4 +1,5 @@ $option Array of test data. + * @param mixed $expected Expected value. */ public function testShouldReturnExpectedValue( $option, $expected ) { $options = new Options( 'wpmedia_options_' ); - if ( is_null( $option['value'] ) ) { Functions\expect( 'get_option' ) ->once() diff --git a/Tests/Unit/Options/has.php b/Tests/Unit/Options/HasTest.php similarity index 85% rename from Tests/Unit/Options/has.php rename to Tests/Unit/Options/HasTest.php index 9de41d0..cd43a32 100644 --- a/Tests/Unit/Options/has.php +++ b/Tests/Unit/Options/HasTest.php @@ -1,4 +1,5 @@ $option Array of test data. */ public function testShouldReturnExpected( $option ) { $options = new Options(); diff --git a/Tests/Unit/Options/getOptionName.php b/Tests/Unit/Options/getOptionName.php deleted file mode 100644 index de14b59..0000000 --- a/Tests/Unit/Options/getOptionName.php +++ /dev/null @@ -1,24 +0,0 @@ -assertSame( - $expected, - $options->get_option_name( $option ) - ); - } -} diff --git a/Tests/Unit/SiteOptions/get.php b/Tests/Unit/SiteOptions/GetTest.php similarity index 82% rename from Tests/Unit/SiteOptions/get.php rename to Tests/Unit/SiteOptions/GetTest.php index 4402395..de3e1b2 100644 --- a/Tests/Unit/SiteOptions/get.php +++ b/Tests/Unit/SiteOptions/GetTest.php @@ -1,4 +1,5 @@ $option Array of test data. + * @param mixed $expected Expected value. */ public function testShouldReturnExpectedValue( $option, $expected ) { $options = new SiteOptions( 'wpmedia_options_' ); - if ( is_null( $option['value'] ) ) { Functions\expect( 'get_site_option' ) ->once() diff --git a/Tests/Unit/TestCase.php b/Tests/Unit/TestCase.php index e9a5ad1..98c5942 100644 --- a/Tests/Unit/TestCase.php +++ b/Tests/Unit/TestCase.php @@ -1,4 +1,5 @@ } + */ protected $config; - protected function setUp() { + /** + * Setup method for the test case. + * + * @return void + */ + protected function set_up() { + parent::set_up(); + if ( empty( $this->config ) ) { $this->loadTestDataConfig(); } - - parent::setUp(); } - public function configTestData() { + /** + * Get the test data configuration. + * + * @return array + */ + public function configTestData(): array { if ( empty( $this->config ) ) { $this->loadTestDataConfig(); } @@ -27,10 +42,19 @@ public function configTestData() { : $this->config; } - protected function loadTestDataConfig() { + /** + * Load test data configuration. + * + * @return void + */ + protected function loadTestDataConfig(): void { $obj = new ReflectionObject( $this ); $filename = $obj->getFileName(); + if ( false === $filename ) { + return; + } + $this->config = $this->getTestData( dirname( $filename ), basename( $filename, '.php' ) ); } } diff --git a/Tests/Unit/bootstrap.php b/Tests/Unit/bootstrap.php new file mode 100644 index 0000000..e06aaee --- /dev/null +++ b/Tests/Unit/bootstrap.php @@ -0,0 +1,7 @@ + + + + + ../../inc + + + + + . + + + diff --git a/composer.json b/composer.json index a0abb61..91236c8 100644 --- a/composer.json +++ b/composer.json @@ -12,41 +12,53 @@ ], "type": "library", "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true, + "phpstan/extension-installer": true + } }, "support": { "issues": "https://github.com/wp-media/options/issues", "source": "https://github.com/wp-media/options" }, "require": { - "php": ">= 7" + "php": ">= 7.4" }, "require-dev": { - "php": ">= 7", - "brain/monkey": "^2.0", + "php": ">= 7.4", "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0", "phpcompatibility/phpcompatibility-wp": "^2.0", - "phpstan/phpstan": "^0.12.3", - "phpunit/phpunit": "^7.5", - "szepeviktor/phpstan-wordpress": "^0.7.0", - "wp-coding-standards/wpcs": "^2", - "wp-media/phpunit": "^2.0" + "php-stubs/wordpress-tests-stubs": "^6.8", + "phpstan/extension-installer": "^1.3", + "phpstan/phpstan-mockery": "^1.1", + "phpstan/phpstan-phpunit": "^1.4", + "szepeviktor/phpstan-wordpress": "^1.3", + "roave/security-advisories": "dev-master", + "wp-coding-standards/wpcs": "^3", + "wp-media/phpunit": "^3" }, "autoload": { - "psr-4": { "WPMedia\\Options\\": "." }, - "exclude-from-classmap": [ "/Tests/" ] + "psr-4": { + "WPMedia\\Options\\": "inc/" + } }, "autoload-dev": { - "psr-4": { "WPMedia\\Options\\Tests\\": "Tests/" } - }, + "psr-4": { + "WPMedia\\Options\\Tests\\Unit\\": "Tests/Unit", + "WPMedia\\Options\\Tests\\Integration\\": "Tests/Integration", + "WPMedia\\Options\\Tests\\Fixtures\\": "Tests/Fixtures" + } + }, "scripts": { - "test-unit": "\"vendor/bin/wpmedia-phpunit\" unit path=Tests/Unit", - "test-integration": "\"vendor/bin/wpmedia-phpunit\" integration path=Tests/Integration/", - "run-stan":"vendor/bin/phpstan analyze --no-progress", - "run-tests": [ - "@test-unit", - "@test-integration" - ], - "phpcs": "phpcs --basepath=." + "phpcs": "vendor/bin/phpcs", + "phpcs:fix": "vendor/bin/phpcbf", + "phpstan": "vendor/bin/phpstan analyze --memory-limit=2G", + "test-unit":"\"vendor/bin/phpunit\" --testsuite unit --colors=always --configuration Tests/Unit/phpunit.xml.dist", + "test-integration": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration Tests/Integration/phpunit.xml.dist", + "run-tests": [ + "@test-unit", + "@test-integration" + ] } } diff --git a/AbstractOptions.php b/inc/AbstractOptions.php similarity index 86% rename from AbstractOptions.php rename to inc/AbstractOptions.php index 40f0016..2615270 100644 --- a/AbstractOptions.php +++ b/inc/AbstractOptions.php @@ -28,12 +28,12 @@ public function get_option_name( string $name ): string { /** * Gets the option for the given name. Returns the default value if the value does not exist. * - * @param string $name Name of the option to get. - * @param mixed $default Default value to return if the value does not exist. + * @param string $name Name of the option to get. + * @param mixed $default_value Default value to return if the value does not exist. * * @return mixed */ - abstract public function get( string $name, $default = null ); + abstract public function get( string $name, $default_value = null ); /** * Sets the value of an option. Update the value if the option for the given name already exists. diff --git a/OptionArray.php b/inc/OptionArray.php similarity index 75% rename from OptionArray.php rename to inc/OptionArray.php index 8895c69..085d6c7 100644 --- a/OptionArray.php +++ b/inc/OptionArray.php @@ -49,33 +49,34 @@ public function has( string $key ): bool { * Gets the value associated with a specific key. * * @param string $key key name. - * @param mixed $default default value to return if key doesn't exist. + * @param mixed $default_value default value to return if key doesn't exist. * * @return mixed */ - public function get( string $key, $default = null ) { + public function get( string $key, $default_value = null ) { /** * Pre-filter any option before read * - * @param mixed $default The default value. + * @param mixed $value Value pre-retrieved. + * @param mixed $default_value The default value. */ - $value = apply_filters( $this->slug . '_pre_get_option_' . $key, null, $default ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals + $value = apply_filters( $this->slug . '_pre_get_option_' . $key, null, $default_value ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals if ( null !== $value ) { return $value; } if ( ! $this->has( $key ) ) { - return $default; + return $default_value; } /** * Filter any option after read * - * @param mixed $value The value associated with the provided key. - * @param mixed $default The default value. + * @param mixed $value The value associated with the provided key. + * @param mixed $default_value The default value. */ - return apply_filters( $this->slug . '_get_option_' . $key, $this->options[ $key ], $default ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals + return apply_filters( $this->slug . '_get_option_' . $key, $this->options[ $key ], $default_value ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals } /** @@ -86,7 +87,7 @@ public function get( string $key, $default = null ) { * * @return void */ - public function set( string $key, $value ) { + public function set( string $key, $value ): void { $this->options[ $key ] = $value; } @@ -97,7 +98,7 @@ public function set( string $key, $value ) { * * @return void */ - public function set_values( array $options ) { + public function set_values( array $options ): void { foreach ( $options as $key => $value ) { $this->set( $key, $value ); } @@ -130,6 +131,7 @@ public function offsetExists( $offset ): bool { * * @return mixed */ + #[\ReturnTypeWillChange] public function offsetGet( $offset ) { return $this->get( $offset ); } @@ -142,7 +144,7 @@ public function offsetGet( $offset ) { * * @return void */ - public function offsetSet( $offset, $value ) { + public function offsetSet( $offset, $value ): void { $this->set( $offset, $value ); } @@ -153,7 +155,7 @@ public function offsetSet( $offset, $value ) { * * @return void */ - public function offsetUnset( $offset ) { + public function offsetUnset( $offset ): void { unset( $this->options[ $offset ] ); } } diff --git a/Options.php b/inc/Options.php similarity index 80% rename from Options.php rename to inc/Options.php index 2a211cd..7e0fd18 100644 --- a/Options.php +++ b/inc/Options.php @@ -19,15 +19,15 @@ public function __construct( string $prefix = '' ) { /** * Gets the option for the given name. Returns the default value if the value does not exist. * - * @param string $name Name of the option to get. - * @param mixed $default Default value to return if the value does not exist. + * @param string $name Name of the option to get. + * @param mixed $default_value Default value to return if the value does not exist. * * @return mixed */ - public function get( string $name, $default = null ) { - $option = get_option( $this->get_option_name( $name ), $default ); + public function get( string $name, $default_value = null ) { + $option = get_option( $this->get_option_name( $name ), $default_value ); - if ( is_array( $default ) && ! is_array( $option ) ) { + if ( is_array( $default_value ) && ! is_array( $option ) ) { $option = (array) $option; } diff --git a/OptionsInterface.php b/inc/OptionsInterface.php similarity index 80% rename from OptionsInterface.php rename to inc/OptionsInterface.php index 03d011a..876c5c6 100644 --- a/OptionsInterface.php +++ b/inc/OptionsInterface.php @@ -14,17 +14,17 @@ interface OptionsInterface { * * @return string */ - public function get_option_name( string $name): string; + public function get_option_name( string $name ): string; /** * Gets the option for the given name. Returns the default value if the value does not exist. * - * @param string $name Name of the option to get. - * @param mixed $default Default value to return if the value does not exist. + * @param string $name Name of the option to get. + * @param mixed $default_value Default value to return if the value does not exist. * * @return mixed */ - public function get( string $name, $default = null ); + public function get( string $name, $default_value = null ); /** * Sets the value of an option. Update the value if the option for the given name already exists. diff --git a/SiteOptions.php b/inc/SiteOptions.php similarity index 80% rename from SiteOptions.php rename to inc/SiteOptions.php index 7952d5b..a91a943 100644 --- a/SiteOptions.php +++ b/inc/SiteOptions.php @@ -19,15 +19,15 @@ public function __construct( string $prefix = '' ) { /** * Gets the option for the given name. Returns the default value if the value does not exist. * - * @param string $name Name of the option to get. - * @param mixed $default Default value to return if the value does not exist. + * @param string $name Name of the option to get. + * @param mixed $default_value Default value to return if the value does not exist. * * @return mixed */ - public function get( string $name, $default = null ) { - $option = get_site_option( $this->get_option_name( $name ), $default ); + public function get( string $name, $default_value = null ) { + $option = get_site_option( $this->get_option_name( $name ), $default_value ); - if ( is_array( $default ) && ! is_array( $option ) ) { + if ( is_array( $default_value ) && ! is_array( $option ) ) { $option = (array) $option; } diff --git a/phpcs.xml b/phpcs.xml.dist similarity index 52% rename from phpcs.xml rename to phpcs.xml.dist index ba5f58f..ceb8f04 100644 --- a/phpcs.xml +++ b/phpcs.xml.dist @@ -4,13 +4,14 @@ + + - . + inc + Tests - /Tests/* - /vendor/* @@ -21,37 +22,22 @@ - - + + - - - - - - - + + + + + + - - - - - - - - - - - - diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 03bb07d..dad368e 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,16 +1,11 @@ includes: - phar://phpstan.phar/conf/bleedingEdge.neon - - vendor/szepeviktor/phpstan-wordpress/extension.neon parameters: - level: 4 - checkMissingIterableValueType: false + level: 5 inferPrivatePropertyTypeFromConstructor: true paths: - - %currentWorkingDirectory%/AbstractOptions.php - - %currentWorkingDirectory%/OptionArray.php - - %currentWorkingDirectory%/Options.php - - %currentWorkingDirectory%/OptionsInterface.php - - %currentWorkingDirectory%/SiteOptions.php - ignoreErrors: - # Uses func_get_args() - - '#^Function apply_filters(_ref_array)? invoked with [34567] parameters, 2 required\.$#' + - inc/ + - Tests/ + scanFiles: + - vendor/php-stubs/wordpress-stubs/wordpress-stubs.php + - vendor/php-stubs/wordpress-tests-stubs/wordpress-tests-stubs.php