Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Scaffold PHPUnit test suite with make:addon command #9593

Merged
merged 10 commits into from
Mar 1, 2024

Conversation

duncanmcclean
Copy link
Member

@duncanmcclean duncanmcclean commented Feb 27, 2024

This pull request improves the php please make:addon command by automatically scaffolding a PHPUnit test suite alongside new addons.

This PR, combined with #9573, will streamline the process of setting up addon tests suites, ultimately saving time for addon developers. ✨

@duncanmcclean duncanmcclean marked this pull request as ready for review February 27, 2024 11:49
Copy link
Member

@jasonvarga jasonvarga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The composer install step failed for me. Not sure how it passed for you. It was complaining about a plugin not being allowed.

Adding this to the composer.json.stub fixes it:

    "config": {
        "allow-plugins": {
            "pixelfear/composer-dist-plugin": true
        }
    }

Also, the tests don't want to run for me. Did they work for you?

sandbox/addons/pixelfear/charlie on  master [!?] 
→ ./vendor/bin/phpunit

Fatal error: Uncaught Error: Class "Statamic\Extend\AddonTestCase" not found in /Users/jason/Code/statamic/sandbox/addons/pixelfear/charlie/tests/TestCase.php:8
Stack trace:
#0 /Users/jason/Code/statamic/sandbox/addons/pixelfear/charlie/vendor/composer/ClassLoader.php(576): include()
#1 /Users/jason/Code/statamic/sandbox/addons/pixelfear/charlie/vendor/composer/ClassLoader.php(427): Composer\Autoload\{closure}('/Users/jason/Co...')
#2 /Users/jason/Code/statamic/sandbox/addons/pixelfear/charlie/tests/ExampleTest.php(5): Composer\Autoload\ClassLoader->loadClass('Pixelfear\\Charl...')
#3 /Users/jason/Code/statamic/sandbox/addons/pixelfear/charlie/vendor/phpunit/phpunit/src/Util/FileLoader.php(66): include_once('/Users/jason/Co...')

Funnily, using VSCode's "Better PHPUnit" extension to run the test from the file itself does work:

Executing task: valet php ./vendor/bin/phpunit /Users/jason/Code/statamic/sandbox/addons/pixelfear/charlie/tests/ExampleTest.php --configuration /Users/jason/Code/statamic/sandbox/addons/pixelfear/charlie/phpunit.xml 

PHPUnit 9.6.17 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 00:00.180, Memory: 34.00 MB

OK (1 test, 0 assertions)

@duncanmcclean
Copy link
Member Author

The composer install step failed for me. Not sure how it passed for you. It was complaining about a plugin not being allowed.

Weird, it was definitely working for me. 🤔

Anyways, I've added that plugin to the composer.json stub. Maybe try again and see if it works this time?

Also, the tests don't want to run for me. Did they work for you?

It'll be because the AddonTestCase is only available on master but when it installs statamic/cms in the addon, it's installing the latest v4 tag which doesn't include that class.

You probably need to symlink your local cms repository before running the tests. Once v5 is tagged, this shouldn't be an issue.

@jasonvarga
Copy link
Member

You're right!

I changed the stub to "statamic/cms": "dev-include-tests-in-make-addon-stubs as 4.50.0", ran make:addon, and I was able to run the tests immediately. That way it could work without needing to symlink my cms first.

Since it works, I've bumped the constraint to ^5.0 since that's what it's gonna be when this is shipped.

@jasonvarga jasonvarga merged commit 36e9f80 into master Mar 1, 2024
34 checks passed
@jasonvarga jasonvarga deleted the include-tests-in-make-addon-stubs branch March 1, 2024 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants