pestphp / pest

Pest is an elegant PHP testing Framework with a focus on simplicity, meticulously designed to bring back the joy of testing in PHP.
https://pestphp.com
MIT License
9.69k stars 362 forks source link

load pest by wp cli #741

Closed aiiddqd closed 1 year ago

aiiddqd commented 1 year ago

Hi everyone!

Thank you for great test tools.

I have some strange question) And I will be grateful for the answers.

I like Laravel's approach running tests like php artisan test

And I would like to make similar tools for WordPress via WP CLI. Kind of php wp test

I have some problem.

I made simple wrapper like:

<?php 

require_once(__DIR__ . '/vendor/autoload.php');

add_action( 'cli_init', function(){
    WP_CLI::add_command( 'pest', function(){

        $posts = get_posts();
        if(is_array($posts)){
            WP_CLI::success( 'test ok' );
        } else {
            WP_CLI::error('test fail');
        }

        test('example', function () {
            expect(true)->toBeTrue();
        });
    } );
});

Where:

If I comment Pest part - all good - and working fine for base TDD

But I want Pest part for better testing of WordPress plugins.

If I run Pest part I get errors:

[26-Mar-2023 20:18:05 UTC] PHP Fatal error:  Uncaught AssertionError: assert(array_key_exists(self::FILE, $trace)) in /Users/man/Projects/test/app/public/wp-content/mu-plugins/vendor/pestphp/pest/src/Support/Backtrace.php:29
Stack trace:
#0 /Users/man/Projects/test/app/public/wp-content/mu-plugins/vendor/pestphp/pest/src/Support/Backtrace.php(29): assert(false, 'assert(array_ke...')
#1 /Users/man/Projects/test/app/public/wp-content/mu-plugins/vendor/pestphp/pest/src/Functions.php(99): Pest\Support\Backtrace::testFile()
#2 /Users/man/Projects/test/app/public/wp-content/mu-plugins/pester.php(17): test('example', Object(Closure))
#3 [internal function]: WP_CLI\Runner->{closure}(Array, Array)
#4 phar:///Applications/Local.app/Contents/Resources/extraResources/bin/wp-cli/wp-cli.phar/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/CommandFactory.php(102): call_user_func(Object(Closure), Array, Array)
#5 [internal function]: WP_CLI\Dispatcher\CommandFactory::WP_CLI\Dispatcher\{closure}(Array, Array)
#6 phar:///Applications/Local.app/Contents/Resources/extraResources/bin/wp-cli/wp-cli.phar/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/Subcommand.php(491): call_user_func(Object(Closure), Array, Array)
#7 phar:///Applications/Local.app/Contents/Resources/extraResources/bin/wp-cli/wp-cli.phar/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(419): WP_CLI\Dispatcher\Subcommand->invoke(Array, Array, Array)
#8 phar:///Applications/Local.app/Contents/Resources/extraResources/bin/wp-cli/wp-cli.phar/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(442): WP_CLI\Runner->run_command(Array, Array)
#9 phar:///Applications/Local.app/Contents/Resources/extraResources/bin/wp-cli/wp-cli.phar/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1256): WP_CLI\Runner->run_command_and_exit()
#10 phar:///Applications/Local.app/Contents/Resources/extraResources/bin/wp-cli/wp-cli.phar/vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/LaunchRunner.php(28): WP_CLI\Runner->start()
#11 phar:///Applications/Local.app/Contents/Resources/extraResources/bin/wp-cli/wp-cli.phar/vendor/wp-cli/wp-cli/php/bootstrap.php(78): WP_CLI\Bootstrap\LaunchRunner->process(Object(WP_CLI\Bootstrap\BootstrapState))
#12 phar:///Applications/Local.app/Contents/Resources/extraResources/bin/wp-cli/wp-cli.phar/vendor/wp-cli/wp-cli/php/wp-cli.php(32): WP_CLI\bootstrap()
#13 phar:///Applications/Local.app/Contents/Resources/extraResources/bin/wp-cli/wp-cli.phar/php/boot-phar.php(11): include('phar:///Applica...')
#14 /Applications/Local.app/Contents/Resources/extraResources/bin/wp-cli/wp-cli.phar(4): include('phar:///Applica...')
#15 {main}
  thrown in /Users/man/Projects/test/app/public/wp-content/mu-plugins/vendor/pestphp/pest/src/Support/Backtrace.php on line 29

And I can't find solution.

Any ideas?

I would like to run Pest test by WP CLI

aiiddqd commented 1 year ago

As an option - fit like this https://github.com/akaunting/akaunting/blob/master/phpunit.xml

  1. tests for core
  2. tests for modules as wildcard
nunomaduro commented 1 year ago

Sorry, I don't use Wordpress so I can't help you here.