diff --git a/.github/workflows/all-pr-tests.yml b/.github/workflows/all-pr-tests.yml new file mode 100644 index 0000000..359fe9b --- /dev/null +++ b/.github/workflows/all-pr-tests.yml @@ -0,0 +1,44 @@ +name: "All Pull Request Tests" + +on: + pull_request: + branches: + - develop + types: [opened, synchronize, reopened, ready_for_review] + +jobs: + # We use a single job to ensure that all steps run in the same environment and + # reduce the number of minutes used. + pr-tests: + # Don't run on draft PRs + if: github.event.pull_request.draft == false + # Timeout after 10 minutes + timeout-minutes: 10 + # Define a matrix of PHP/WordPress versions to test against + strategy: + fail-fast: false + matrix: + php: [8.1, 8.2, 8.3] + wordpress: ["latest"] + runs-on: ubuntu-latest + # Cancel any existing runs of this workflow + concurrency: + group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.ref }}-P${{ matrix.php }}-WP${{ matrix.wordpress }} + cancel-in-progress: true + # Name the job in the matrix + name: "PR Tests PHP ${{ matrix.php }} WordPress ${{ matrix.wordpress }}" + steps: + - uses: actions/checkout@v4 + + - name: Run General Tests + # See https://github.com/alleyinteractive/action-test-general for more options + uses: alleyinteractive/action-test-general@develop + + - name: Run PHP Tests + # See https://github.com/alleyinteractive/action-test-php for more options + uses: alleyinteractive/action-test-php@develop + with: + php-version: '${{ matrix.php }}' + wordpress-version: '${{ matrix.wordpress }}' + skip-wordpress-install: 'true' + skip-services: 'true' # Skip MySQL unless the library is not using SQLite testing. diff --git a/.github/workflows/coding-quality.yml b/.github/workflows/coding-quality.yml deleted file mode 100644 index 769b420..0000000 --- a/.github/workflows/coding-quality.yml +++ /dev/null @@ -1,14 +0,0 @@ -name: Code Quality - -on: - push: - branches: - - main - pull_request: - # Uncomment and edit the following to run on a schedule. - # schedule: - # - cron: '45 5 * * 0' # Run once per week at 5:45am UTC on Sundays. - -jobs: - code-quality: - uses: alleyinteractive/.github/.github/workflows/php-code-quality.yml@main diff --git a/.github/workflows/coding-standards.yml b/.github/workflows/coding-standards.yml deleted file mode 100644 index 49a310e..0000000 --- a/.github/workflows/coding-standards.yml +++ /dev/null @@ -1,16 +0,0 @@ -name: Coding Standards - -on: - push: - branches: - - main - pull_request: - # Uncomment and edit the following to run on a schedule. - # schedule: - # - cron: '0 5 * * 0' # Run once per week at 5am UTC on Sundays. - -jobs: - coding-standards: - uses: alleyinteractive/.github/.github/workflows/php-coding-standards.yml@main - with: - php: "8.2" diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml deleted file mode 100644 index 9500474..0000000 --- a/.github/workflows/unit-test.yml +++ /dev/null @@ -1,16 +0,0 @@ -name: Testing Suite - -on: - push: - branches: - - main - pull_request: - # Uncomment and edit the following to run on a schedule. - # schedule: - # - cron: '15 5 * * 0' # Run once per week at 5:15am UTC on Sundays. - -jobs: - unit-tests: - uses: alleyinteractive/.github/.github/workflows/php-tests.yml@main - with: - php: "8.2" diff --git a/Makefile b/Makefile index d9e033f..e652afa 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +MAKEFLAGS += --no-builtin-rules + setup: php ./configure.php diff --git a/buddy.yml b/buddy.yml deleted file mode 100644 index db65b1a..0000000 --- a/buddy.yml +++ /dev/null @@ -1,110 +0,0 @@ -- pipeline: "Pull Request Tests" - trigger_mode: "ON_EVERY_PUSH" - ref_name: "refs/pull/*" - ref_type: "WILDCARD" - priority: "NORMAL" - target_site_url: "https://github.com/alleyinteractive/create-php-package" - fetch_all_refs: true - fail_on_prepare_env_warning: true - trigger_condition: "ALWAYS" - actions: - - action: "Gitignored files check" - type: "BUILD" - working_directory: "/buddy/create-php-package" - docker_image_name: "alleyops/ci-resources" - docker_image_tag: "8.2-fpm-wp" - execute_commands: - - "if [[ ! -z $(git ls-files -i --exclude-standard) ]]; then exit 1; fi" - volume_mappings: - - "/:/buddy/create-php-package" - trigger_condition: "ALWAYS" - shell: "BASH" - run_next_parallel: true - - action: "Check for git conflicts" - type: "BUILD" - working_directory: "/buddy/create-php-package" - docker_image_name: "alleyops/ci-resources" - docker_image_tag: "8.2-fpm-wp" - execute_commands: - - "! git grep -E '<<<<<<< |>>>>>>> ' -- './*' ':(exclude)buddy.yml' ':(exclude).buddy/*'" - volume_mappings: - - "/:/buddy/create-php-package" - trigger_condition: "ALWAYS" - shell: "BASH" - - action: "Composer install" - type: "BUILD" - working_directory: "/buddy/create-php-package" - docker_image_name: "alleyops/ci-resources" - docker_image_tag: "8.2-fpm-wp" - execute_commands: - - "composer install -q" - volume_mappings: - - "/:/buddy/create-php-package" - trigger_condition: "ALWAYS" - shell: "BASH" - - action: "phpunit" - type: "BUILD" - working_directory: "/buddy/create-php-package" - docker_image_name: "alleyops/ci-resources" - docker_image_tag: "8.2-fpm-wp" - execute_commands: - - "composer phpunit" - setup_commands: - - "echo \"extension=memcache.so\" >> /usr/local/etc/php/conf.d/buddy.ini" - services: - - type: "MARIADB" - version: "10.3" - connection: - host: "mariadb" - port: 3306 - user: "root" - password: "root" - db: "wordpress_unit_tests" - - type: "MEMCACHED" - version: "1.5.6" - connection: - host: "memcached" - port: 11211 - volume_mappings: - - "/:/buddy/create-php-package" - trigger_condition: "ALWAYS" - shell: "BASH" - run_next_parallel: true - - action: "composer phpcs" - type: "BUILD" - working_directory: "/buddy/create-php-package" - docker_image_name: "alleyops/ci-resources" - docker_image_tag: "8.2-fpm-wp" - execute_commands: - - "composer phpcs" - volume_mappings: - - "/:/buddy/create-php-package" - trigger_condition: "ALWAYS" - shell: "BASH" - variables: - - key: "CACHEDIR" - value: "/tmp/test-cache" - type: "VAR" - description: "Cache folder for remote requests." - - key: "SKIP_DISCOVERY" - value: "true" - type: "VAR" - - key: "WP_CORE_DIR" - value: "/tmp/wordpress" - type: "VAR" - description: "WordPress checkout folder." - - key: "WP_VERSION" - value: "latest" - type: "VAR" - - key: "WP_DB_PASSWORD" - value: "root" - type: "VAR" - - key: "WP_DB_HOST" - value: "mariadb" - type: "VAR" - - key: "WP_SKIP_DB_CREATE" - value: "true" - type: "VAR" - - key: "WP_PHPUNIT_PATH" - value: "composer run phpunit" - type: "VAR" diff --git a/composer.json b/composer.json index f343d91..7c5e674 100644 --- a/composer.json +++ b/composer.json @@ -19,9 +19,8 @@ "alleyinteractive/composer-wordpress-autoloader": "^1.0" }, "require-dev": { - "alleyinteractive/alley-coding-standards": "^1.0 || ^2.0", - "mantle-framework/testkit": "^0.12", - "nunomaduro/collision": "^5.0 || ^6.0", + "alleyinteractive/alley-coding-standards": "^2.0", + "mantle-framework/testkit": "^1.0", "szepeviktor/phpstan-wordpress": "^1.3" }, "config": { @@ -32,13 +31,15 @@ }, "sort-packages": true }, + "autoload-dev": { + "psr-4": { + "Alley\\Create_PHP_Package\\Tests\\": "tests" + } + }, "extra": { "wordpress-autoloader": { "autoload": { - "Create_PHP_Package\\": "src" - }, - "autoload-dev": { - "Create_PHP_Package\\Tests\\": "tests" + "Alley\\Create_PHP_Package\\": "src" } } }, diff --git a/configure.php b/configure.php index 0778ef9..3dc9ca9 100644 --- a/configure.php +++ b/configure.php @@ -41,7 +41,7 @@ function writeln( string $line ): void { function run( string $command, string $dir = null ): string { $command = $dir ? "cd {$dir} && {$command}" : $command; - return trim( shell_exec( $command ) ); + return trim( (string) shell_exec( $command ) ); } function str_after( string $subject, string $search ): string { @@ -55,7 +55,7 @@ function str_after( string $subject, string $search ): string { } function slugify( string $subject ): string { - return strtolower( trim( preg_replace( '/[^A-Za-z0-9-]+/', '-', $subject ), '-' ) ); + return strtolower( trim( (string) preg_replace( '/[^A-Za-z0-9-]+/', '-', $subject ), '-' ) ); } function title_case( string $subject ): string { @@ -66,15 +66,23 @@ function ensure_capitalp( string $text ): string { return str_replace( 'Wordpress', 'WordPress', $text ); } +/** + * @param string $file + * @param array $replacements + */ function replace_in_file( string $file, array $replacements ): void { $contents = file_get_contents( $file ); + if ( empty( $contents ) ) { + return; + } + file_put_contents( $file, str_replace( array_keys( $replacements ), array_values( $replacements ), - $contents + $contents, ) ); } @@ -82,6 +90,10 @@ function replace_in_file( string $file, array $replacements ): void { function remove_readme_paragraphs( string $file ): void { $contents = file_get_contents( $file ); + if ( empty( $contents ) ) { + return; + } + file_put_contents( $file, trim( preg_replace( '/.*/s', '', $contents ) ?: $contents ), @@ -92,11 +104,17 @@ function determine_separator( string $path ): string { return str_replace( '/', DIRECTORY_SEPARATOR, $path ); } +/** + * @return array + */ function list_all_files_for_replacement(): array { - return explode( PHP_EOL, run( 'grep -R -l ./ --exclude LICENSE --exclude configure.php --exclude composer.lock --exclude-dir .git --exclude-dir .github --exclude-dir vendor --exclude-dir bin --exclude-dir webpack --exclude-dir modules --exclude-dir .phpcs' ) ); + return explode( PHP_EOL, run( 'grep -R -l ./ --exclude LICENSE --exclude .phpunit.result.cache --exclude-dir node_modules --exclude configure.php --exclude composer.lock --exclude-dir .git --exclude-dir .github --exclude-dir vendor --exclude-dir bin --exclude-dir webpack --exclude-dir modules --exclude-dir .phpcs' ) ); } -function delete_files( string|array $paths ) { +/** + * @param string|array $paths + */ +function delete_files( string|array $paths ): void { if ( ! is_array( $paths ) ) { $paths = [ $paths ]; } @@ -112,8 +130,19 @@ function delete_files( string|array $paths ) { } } +$current_dir = getcwd(); + echo "\nWelcome friend to alleyinteractive/create-php-package! 😀\nLet's setup your PHP package 🚀\n\n"; +if ( ! $current_dir ) { + die( 'Could not determine current directory.' ); +} + +$folder_name = ensure_capitalp( basename( $current_dir ) ); + +$package_name = ask( 'Package name?', str_replace( '_', ' ', title_case( $folder_name ) ) ); +$package_name_slug = slugify( $package_name ); + $git_name = run( 'git config user.name' ); $author_name = ask( 'Author name?', $git_name ); @@ -128,13 +157,13 @@ function delete_files( string|array $paths ) { $vendor_name = ask( 'Vendor name (usually the Github Organization)?', $username_guess ); $vendor_slug = slugify( $vendor_name ); -$current_dir = getcwd(); -$folder_name = ensure_capitalp( basename( $current_dir ) ); +$is_wordpress_package = confirm( 'Is this a WordPress package?', false ); -$package_name = ask( 'Package name?', str_replace( '_', ' ', title_case( $folder_name ) ) ); -$package_name_slug = slugify( $package_name ); +$namespace = ask( + 'Package namespace?', + $is_wordpress_package ? 'Alley\\WP\\' . title_case( $package_name ) : 'Alley\\' . title_case( $package_name ), +); -$namespace = ask( 'Package namespace?', title_case( $package_name ) ); $class_name = ask( 'Base class name for package?', title_case( $package_name ) ); $description = ask( 'Package description?', "This is my PHP package {$package_name}" ); @@ -161,7 +190,10 @@ function delete_files( string|array $paths ) { 'A skeleton PHP package geared for WordPress Development' => $description, - 'Create_PHP_Package' => $namespace, + // Extra slashes are here for composer.json. + 'Alley\\\Create_PHP_Package\\\\' => str_replace( '\\', '\\\\', $namespace ) . '\\\\', + 'Alley\Create_PHP_Package' => $namespace, + 'Example_Package' => $class_name, 'package_name' => $package_name, @@ -199,12 +231,6 @@ function delete_files( string|array $paths ) { echo "\n\n"; } -if ( - file_exists( __DIR__ . '/buddy.yml' ) && confirm( 'Do you need the Buddy CI configuration? (Alley devs only -- if the package is open-source it will not be needed)', false ) -) { - delete_files( [ '.buddy', 'buddy.yml' ] ); -} - if ( confirm( 'Let this script delete itself?', true ) ) { delete_files( [ diff --git a/phpcs.xml b/phpcs.xml index fc57e61..f356bdf 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -38,7 +38,7 @@ - + diff --git a/phpunit.xml b/phpunit.xml index 7d8c272..24f82fc 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,18 +1,21 @@ + - - - tests/feature - - - tests/unit - - + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + bootstrap="tests/bootstrap.php" + backupGlobals="false" + colors="true" + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.2/phpunit.xsd" + cacheDirectory=".phpunit.result.cache"> + + + tests/Feature + + + tests/Unit + + + + + + diff --git a/src/class-example-package.php b/src/class-example-package.php index ca2a9c2..e44ba53 100644 --- a/src/class-example-package.php +++ b/src/class-example-package.php @@ -5,7 +5,7 @@ * @package create-php-package */ -namespace Create_PHP_Package; +namespace Alley\Create_PHP_Package; /** * Example Package diff --git a/tests/feature/test-example-test.php b/tests/Feature/ExampleTest.php similarity index 60% rename from tests/feature/test-example-test.php rename to tests/Feature/ExampleTest.php index 0968f85..f063068 100644 --- a/tests/feature/test-example-test.php +++ b/tests/Feature/ExampleTest.php @@ -1,12 +1,12 @@ assertTrue( true ); $this->assertNotEmpty( home_url() ); diff --git a/tests/class-test-case.php b/tests/TestCase.php similarity index 53% rename from tests/class-test-case.php rename to tests/TestCase.php index ecdfb7a..32a96f6 100644 --- a/tests/class-test-case.php +++ b/tests/TestCase.php @@ -1,11 +1,11 @@ assertTrue(true); }