diff --git a/.github/workflows/php-scoper.yml b/.github/workflows/php-scoper.yml new file mode 100644 index 0000000..781293a --- /dev/null +++ b/.github/workflows/php-scoper.yml @@ -0,0 +1,28 @@ +name: Setup PHP Scoper + +on: + workflow_call: + inputs: + scoper: + required: true + type: boolean + +jobs: + setup-php-scoper: + runs-on: ubuntu-latest + steps: + - name: Do PHP Scoper + if: inputs.scoper + run: | + composer global require humbug/php-scoper + ~/.composer/vendor/bin/php-scoper add-prefix --output-dir=vendor-prefixed --force + composer dump-autoload --working-dir vendor-prefixed --classmap-authoritative + composer scoper-patch + + - name: Cleanup PHP Scoper + if: inputs.scoper + run: rm -rf vendor + + - name: Skip Scoper + if: ! inputs.scoper + run: echo "Skipping Scoper" diff --git a/.github/workflows/wordpress.yml b/.github/workflows/wordpress.yml index 7089a1c..da8e933 100644 --- a/.github/workflows/wordpress.yml +++ b/.github/workflows/wordpress.yml @@ -19,7 +19,7 @@ jobs: php: [ '7.2', '7.4', '8.0' ] # PHP versions to check. wp: [ 'latest', '5.9' ] # WordPress version to check. tools: [ composer ] - + scoper: [ true, false ] services: mysql: image: mysql:8.0 @@ -47,12 +47,19 @@ jobs: - name: Install dependencies run: composer install --prefer-dist --no-progress --no-suggest + - uses: ./.github/workflows/php-scoper.yml + with: + scoper: ${{ matrix.scoper }} + + - name: Make Binaries Executionable + if: matrix.scoper + run: chmod +x vendor-prefixed/vendor/bin/* + - name: Start MySQL run: | sudo systemctl start mysql mysql -h 127.0.0.1 --port 3306 -u root --password=root -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';" - - name: Install WordPress run: bash bin/install-wp-tests.sh wordpress root root 127.0.0.1:3306 ${{ matrix.wp }} @@ -103,15 +110,12 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: Build Package. + - name: Build Package run: bash bin/build.sh - - name: Do PHP scoper - run: | - composer global require humbug/php-scoper - ~/.composer/vendor/bin/php-scoper add-prefix --output-dir=vendor-prefixed --force - composer dump-autoload --working-dir vendor-prefixed --classmap-authoritative - composer help-scoper + - uses: ./.github/workflows/php-scoper.yml + with: + scoper: true - name: Clean Unwanted files run: bash bin/clean.sh diff --git a/composer.json b/composer.json index 034a44d..1adc38f 100644 --- a/composer.json +++ b/composer.json @@ -14,15 +14,20 @@ "test": "phpunit", "lint": "phpcs --standard=phpcs.ruleset.xml $(find ./ -name '*.php')", "fix": "phpcbf --standard=phpcs.ruleset.xml $(find ./ -name '*.php')", - "scoper": "php-scoper", - "prefix": [ - "composer install --no-dev", + "scoper": [ "php-scoper add-prefix --output-dir=vendor-prefixed --force", "composer dump-autoload --working-dir vendor-prefixed --classmap-authoritative", - "composer install", - "@help-scoper" + "@scoper-patch" + ], + "scoper-patch": [ + "php bin/patcher.php", + "chmod +x vendor-prefixed/vendor/bin/*" ], - "help-scoper": "php bin/patcher.php" + "prefix": [ + "composer install --no-dev", + "@scoper", + "composer install" + ] }, "require": { "php": ">=7.2", diff --git a/package.json b/package.json index 935b114..c07f110 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "update": "wp-env start --update", "stop": "wp-env stop", "test": "wp-env run tests-cli ./wp-content/plugins/ga-communicator/vendor/bin/phpunit -c ./wp-content/plugins/ga-communicator/phpunit.xml.dist", + "test:scoper": "wp-env run tests-cli ./wp-content/plugins/ga-communicator/vendor-prefixed/vendor/bin/phpunit -c ./wp-content/plugins/ga-communicator/phpunit.xml.dist", "env": "wp-env", "cli": "wp-env run cli wp", "cli:test": "wp-env run tests-cli wp", diff --git a/scoper.inc.php b/scoper.inc.php index af966eb..8a5cdc1 100644 --- a/scoper.inc.php +++ b/scoper.inc.php @@ -60,14 +60,25 @@ static function ( string $filePath, string $prefix, string $contents ): string { // For more information see: https://github.com/humbug/php-scoper/blob/master/docs/configuration.md#excluded-symbols 'exclude-namespaces' => [ + '/^cli/', + '/^Composer/', + '/^DeepCopy/', + '/^Doctrine/', '/^Kunoichi/', + '/^PharIo/', + '/^PHP_CodeSniffer/', + '/^PHPCompatibility/', + '/^PHPCSStandards/', + '/^PHPUnit/', + '/^Yoast/', + '/^SebastianBergmann/', + '/^TheSeer/', '/^WP_/', - '/^Composer/', - '/^cli/' ], 'exclude-classes' => [ '/^WP_/', - '/^Composer/' + '/^Composer/', + '/^PHPUnit/', ], 'exclude-functions' => [ // 'mb_str_split', @@ -80,7 +91,7 @@ static function ( string $filePath, string $prefix, string $contents ): string { // // For more information see: https://github.com/humbug/php-scoper/blob/master/docs/configuration.md#exposed-symbols 'expose-global-constants' => true, - 'expose-global-classes' => true, + 'expose-global-classes' => false, 'expose-global-functions' => false, 'expose-namespaces' => [ ], diff --git a/tests/bootstrap.php b/tests/bootstrap.php index ee85b70..b743bd1 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -19,7 +19,13 @@ // Give access to tests_add_filter() function. require_once $_tests_dir . '/includes/functions.php'; -require_once dirname( __DIR__ ) . '/vendor/autoload.php'; +/** + * Manually load the plugin being tested. + */ +function _manually_load_plugin() { + require dirname( dirname( __FILE__ ) ) . '/ga-communicator.php'; +} +tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); // Start up the WP testing environment. require $_tests_dir . '/includes/bootstrap.php';