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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

devad created an issue. See original summary.

cilefen’s picture

Component: simpletest.module » phpunit
hugronaphor’s picture

I got it after upgrading to Composer 2
adding phpspec/prophecy-phpunit fixed the issue

longwave’s picture

Category: Bug report » Support request

The 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?

mvonfrie’s picture

What about adding it as suggestion to the composer.json?

{
    "suggest": {
        "phpspec/prophecy-phpunit": "Needed if you are using PHPUnit 9.x, but don't install together with PHPUnit 8.x. See https://www.drupal.org/node/3176567 for details."
    }
}

At least this gives the user a hint when running composer install or composer update that he might have to add phpspec/prophecy-phpunit as dependency.

quietone’s picture

Issue summary: View changes

I 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.

moshe weitzman’s picture

We 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.

laceysanderson’s picture

Also running into this when testing 9.1.x-dev on docker. This tracks with @moshe-weitzman thoughts regarding drupal/core. I install as follows:

composer create-project drupal/recommended-project:9.1.x-dev drupal9 --stability dev --no-interaction \
  && cd drupal9 \
  && composer require --dev drupal/core-dev:9.1.x-dev \
  && composer require drush/drush \
  && composer up

When I try to run composer drupal-phpunit-upgrade as provided in #4 the script is not available. However using composer require phpspec/prophecy-phpunit does fix it :-)

fgm’s picture

Issue summary: View changes

Fixed broken CR link

longwave’s picture

I'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?

longwave’s picture

Category: Support request » Bug report
Status: Active » Needs review
FileSize
750 bytes
mondrake’s picture

Status: Needs review » Reviewed & tested by the community

#10 and #11 sound like a good idea to me.

Gábor Hojtsy’s picture

Ran into this exact problem today while setting up a new dev site for Upgrade Status locally. This pointed me in the right direction.

Eli-T’s picture

Slight 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.

Eli-T’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
768 bytes

Patch attached with shameless bikeshedding from #14

Eli-T’s picture

FileSize
782 bytes

Adding interdiff

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Good point.

BR0kEN’s picture

Wouldn't that make sense to do something like `shell_exec('composer require --dev phpspec/prophecy-phpunit')`?

longwave’s picture

Better 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.

Eli-T’s picture

Wouldn't that make sense to do something like `shell_exec('composer require --dev phpspec/prophecy-phpunit')`?

I 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.

BR0kEN’s picture

Makes sense 👍

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit9/TestCompatibilityTrait.php
@@ -4,6 +4,11 @@
+  print "Drupal requires Prophecy PhpUnit when using PHPUnit 9. Please use 'composer require --dev phpspec/prophecy-phpunit' to ensure that it is present.\n";

This 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.

longwave’s picture

Status: Needs work » Needs review
FileSize
860 bytes

Addressed #22.

Eli-T’s picture

Status: Needs review » Reviewed & tested by the community

Have 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.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed cbccba94b6 to 9.2.x and 94d0c1f1f2 to 9.1.x. Thanks!

  • alexpott committed cbccba9 on 9.2.x
    Issue #3182653 by longwave, Eli-T, devad: PHPUnit 9.4.3 Fatal error:...

  • alexpott committed 94d0c1f on 9.1.x
    Issue #3182653 by longwave, Eli-T, devad: PHPUnit 9.4.3 Fatal error:...
mglaman’s picture

So 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:

Reflection error: Prophecy\PhpUnit\ProphecyTrait not found.
Class Prophecy\PhpUnit\ProphecyTrait not found.
Failed asserting that actual size 2 matches expected size 0.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Cangurin limpiezas’s picture

hi, 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.

cilefen’s picture

arshadkhan35’s picture

Adding composer require --dev symfony/phpunit-bridge worked for me.

hiramanpatil’s picture

@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,