Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
An attempt to run php vendor/phpunit/phpunit/phpunit -c web/core web/core/modules/views/tests/src/Unit/Plugin/argument_default/RawTest.php
resulted to me with:
Fatal error: Trait 'Prophecy\PhpUnit\ProphecyTrait' not found in D:\wamp\www\d9unit\web\core\tests\Drupal\TestTo
ols\PhpUnitCompatibility\PhpUnit9\TestCompatibilityTrait.php on line 12
Steps to reproduce
This happened to me with latest D9.1.x-dev (e0f0ebc)
Proposed resolution
According to change record Updated to PHPUnit 9
Use composer drupal-phpunit-upgrade
See #4
The original proposal, which also worked
The error is fixed for me after I run: composer require phpspec/prophecy-phpunit
So, this dependency might be missing somewhere probably.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Comment | File | Size | Author |
---|---|---|---|
#23 | 3182653-23.patch | 860 bytes | longwave |
Comments
Comment #2
cilefen CreditAttribution: cilefen commentedComment #3
hugronaphor CreditAttribution: hugronaphor commentedI got it after upgrading to Composer 2
adding
phpspec/prophecy-phpunit
fixed the issueComment #4
longwaveThe problem is that Drupal 9 is compatible with both PHPUnit 8 and PHPUnit 9, but we only need
phpspec/prophecy-phpunit
on PHPUnit 9 and it won't install if you have PHPUnit 8 so we can't add it as a dependency.The command
composer drupal-phpunit-upgrade
exists already to upgrade PHPUnit and add the dependency for you.This is also documented in the change record at https://www.drupal.org/node/3176567
I'm not sure there is a way of making this more foolproof while we are compatible with both versions?
Comment #5
mvonfrie CreditAttribution: mvonfrie as a volunteer and commentedWhat about adding it as suggestion to the composer.json?
At least this gives the user a hint when running
composer install
orcomposer update
that he might have to addphpspec/prophecy-phpunit
as dependency.Comment #6
quietone CreditAttribution: quietone as a volunteer commentedI just ran into this. I did
composer drupal-phpunit-upgrade
as provided in #4 and that worked just fine. I added that solution to the IS, keeping the original.Comment #7
moshe weitzman CreditAttribution: moshe weitzman as a volunteer and commentedWe are running into this as well on DrupalSpoons. The proposal to run
composer drupal-phpunit-upgrade
does not work for any site built on core-composer-scaffold since that script is defined in drupal/drupal but not in drupal/core. We can't easily hard code the contents of the script because it varies by Drupal version.Comment #8
laceysanderson CreditAttribution: laceysanderson commentedAlso running into this when testing 9.1.x-dev on docker. This tracks with @moshe-weitzman thoughts regarding drupal/core. I install as follows:
When I try to run
composer drupal-phpunit-upgrade
as provided in #4 the script is not available. However usingcomposer require phpspec/prophecy-phpunit
does fix it :-)Comment #9
fgmFixed broken CR link
Comment #10
longwaveI've helped two people on Slack today with this error message alone.
I wonder if we can add some detection code that detects this state and tells the user what to do?
Comment #11
longwaveComment #12
mondrake#10 and #11 sound like a good idea to me.
Comment #13
Gábor HojtsyRan into this exact problem today while setting up a new dev site for Upgrade Status locally. This pointed me in the right direction.
Comment #14
Eli-TSlight query with the wording of the patch in #11:
PHPUnit 9 requires Prophecy PhpUnit.
Isn't it that Drupal requires Prophecy PhpUnit when testing with PHPUnit 9?
PHPUnit 9 doesn't require Prophecy PhpUnit by itself, or it would be in PHPUnit's requirements.
Comment #15
Eli-TPatch attached with shameless bikeshedding from #14
Comment #16
Eli-TAdding interdiff
Comment #17
longwaveGood point.
Comment #18
BR0kENWouldn't that make sense to do something like `shell_exec('composer require --dev phpspec/prophecy-phpunit')`?
Comment #19
longwaveBetter to tell the user what to do rather than try and fix it, as we might not be able to run Composer.
We have similar instructions in run-tests.sh if PHPUnit is not installed at all or the wrong version.
Comment #20
Eli-TI think if we actually started automatically running composer commands when people run unit tests, that would be very confusing.
We also don't know if composer is on the path so this might not even work.
Comment #21
BR0kENMakes sense 👍
Comment #22
alexpottThis looks like a fine approach to me. I think we should limit this to we do in drupal-phpunit-upgrade - ie.
composer require --dev phpspec/prophecy-phpunit:^2
We also should add an @todo to remove when the minimum PHPUnit is 9 because then we can add this as a real dependency.
Further, let's say "when using PHPUnit 9 or greater" - or something like that.
Comment #23
longwaveAddressed #22.
Comment #24
Eli-THave reviewed the patch in #23, it includes the suggested changes from #22.
Todo comment added - there is no current issue for removing PHPUnit 8 so we don't need to reference one here.
Hence setting back to RTBC.
Comment #25
alexpottCommitted and pushed cbccba94b6 to 9.2.x and 94d0c1f1f2 to 9.1.x. Thanks!
Comment #28
mglamanSo I tried searching the issue queue for this and it didn't show. So I'll comment what I got back in my phpstan-drupal testing:
Comment #30
Cangurin limpiezashi, i use
PHPUnit 9.5.5
Drupal core 9.2.0
when i try use
vendor/bin/phpunit -c phpunit.xml
or
SIMPLETEST_BASE_URL=http://localhost/c17062021/ ./vendor/bin/phpunit -c core/ --testsuite functional --filter SkeletonTest
i always get
PHP Fatal error: Trait 'Symfony\Bridge\PhpUnit\Legacy\PolyfillAssertTrait' not found in /var/www/html/c17062021/sites/simpletest/Assert.php on line 91
any idea?, thanks.
Comment #31
cilefen CreditAttribution: cilefen commentedThat could be https://www.drupal.org/node/3217694
Comment #32
arshadkhan35 CreditAttribution: arshadkhan35 as a volunteer commentedAdding
composer require --dev symfony/phpunit-bridge
worked for me.Comment #33
hiramanpatil CreditAttribution: hiramanpatil at Randstad Risesmart commented@arshadkhan35,
I have tried it but still getting the same error.
Please check my reply here - https://www.drupal.org/project/drupal/issues/3346242#comment-15387854
Were you able to run any contributed module tests?
Thanks,