Skip to content

Commit

Permalink
#50 PHP-Scoper がテスト可能に
Browse files Browse the repository at this point in the history
  • Loading branch information
fumikito committed Jul 19, 2023
1 parent 8896e26 commit 04ae20f
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 20 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/php-scoper.yml
Original file line number Diff line number Diff line change
@@ -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"
22 changes: 13 additions & 9 deletions .github/workflows/wordpress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }}

Expand Down Expand Up @@ -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
Expand Down
17 changes: 11 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 15 additions & 4 deletions scoper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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' => [
],
Expand Down
8 changes: 7 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit 04ae20f

Please sign in to comment.