pest icon indicating copy to clipboard operation
pest copied to clipboard

[Bug]: Fatal incompatibility between Hamcrest and PEST

Open vbulash opened this issue 1 year ago • 4 comments

What Happened

Run Pest results:

vbulash@iMac-Valerij 12:00:13 ~/PhpstormProjects/example-app # ./vendor/bin/pest .
PHP Fatal error:  Declaration of Hamcrest\Core\CombinableMatcherTest::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp(): void in /Users/vbulash/PhpstormProjects/example-app/vendor/hamcrest/hamcrest-php/tests/Hamcrest/Core/CombinableMatcherTest.php on line 10

   ERROR  Fatal error: Declaration of Hamcrest\Core\CombinableMatcherTest::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp(): void in /Users/vbulash/PhpstormProjects/example-app/vendor/hamcrest/hamcrest-php/tests/Hamcrest/Core/CombinableMatcherTest.php on line 10.

How to Reproduce

Newly installed Laravel 11 example-app by:

composer create-project laravel/laravel example-app
npm install && npm run build

then install Pest:

composer remove phpunit/phpunit
composer require pestphp/pest --dev --with-all-dependencies

Run Pest (still without any tests):

./vendor/bin/pest .

Sample Repository

No response

Pest Version

3.7

PHP Version

8.4.3

Operation System

macOS

Notes

No response

vbulash avatar Feb 09 '25 09:02 vbulash

Using . as a selector will search for all tests in the current directory, including vendor/. This includes tests from Hamcrest because they haven't excluded them from releases (they did in 2022, but haven't done a subsequent release).

Try just running vendor/bin/pest to use the locations from the phpunit.xml configuration.

owenvoke avatar Feb 09 '25 10:02 owenvoke

Unfortunately, PHPStorm automation substitute folder as '.' and there is no way to change it to 'tests'. Yes, it's not problem of Pest.

But when I change Pest command line with existing Laravel app from auto-generated /usr/bin/php /var/www/html/vendor/bin/pest --teamcity --configuration /var/www/html/phpunit.xml . to /usr/bin/php /var/www/html/vendor/bin/pest --teamcity --configuration /var/www/html/phpunit.xml tests, then Pest didn't see DB connection:

root@8089a3bccd0f:/var/www/html# /usr/bin/php /var/www/html/vendor/bin/pest --configuration /var/www/html/phpunit.xml tests

   FAIL  Tests\Feature\V2TemplateUnionTest
   FAILED  Tests\Feature\MQTTBatchTest >                                                                                                                                                         Error   
  Call to a member function connection() on null

  at vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1851
    1847▕      * @return \Illuminate\Database\Connection
    1848▕      */
    1849▕     public static function resolveConnection($connection = null)
    1850▕     {
  ➜ 1851▕         return static::$resolver->connection($connection);
    1852▕     }
    1853▕ 
    1854▕     /**
    1855▕      * Get the connection resolver instance.

      +8 vendor frames 
  9   tests/Feature/MQTTBatchTest.php:50

while php artisan test use this connection without any problem phpunit.xml.zip

vbulash avatar Feb 09 '25 11:02 vbulash

So, forget about it I found the way how to fix all problems with Pest start inside PhpStorm

vbulash avatar Feb 09 '25 11:02 vbulash

Bumped into this today. My hacky solution is to just nuke test directories out of vendor on installation or update:

"post-install-cmd": [
    "rm -rf vendor/**/tests"
],
"post-update-cmd": [
    "rm -rf vendor/**/tests"
],

Now PhpStorm can tell me how many of my tests are broken! Yay!

Vusys avatar Mar 26 '25 20:03 Vusys