Pixelairport's avatar

Github Workflow problem with PhpStan

Hi. I uploaded my code and pest tests run without a problem. But PhpStan locally works, but in Github I get an error:

Error: Call to an undefined method Pest\Expectation<mixed>::toBeValidUuid().
Error: Call to an undefined method Pest\Expectation<mixed>::toBeValidUuid().
 ------ ----------------------------------------------------------------------- 
  Line   tests/Feature/Traits/HasUuidTest.php                                   
 ------ ----------------------------------------------------------------------- 
  52     Call to an undefined method Pest\Expectation<mixed>::toBeValidUuid().  
         🪪  method.notFound                                                    
  64     Call to an undefined method Pest\Expectation<mixed>::toBeValidUuid().  
         🪪  method.notFound                                                    
 ------ ----------------------------------------------------------------------- 

Can somebody help, how I should do this? I tried with docblocks and also with a stub file, but it was not working. Maybe I did it wrong, but dont know how to solve it.

0 likes
4 replies
LaryAI's avatar
Level 58

Certainly! The error message:

Call to an undefined method Pest\Expectation<mixed>::toBeValidUuid().

means that PHPStan doesn’t know about your custom expectation method (toBeValidUuid) on Pest’s Expectation class, even though it works at runtime.

This is a common situation when extending Pest with custom expectations. To fix it, you need to add a stub file or proper docblocks or configure PHPStan to recognize the custom method.


Solution

1. Create a PHPStan Stub File

Create a stub file (e.g., pest-extensions.stub) anywhere in your project (commonly in a /stubs directory).

<?php

namespace Pest;

class Expectation
{
    /**
     * @phpstan-assert self $this
     */
    public function toBeValidUuid(): self {}
}

2. Register Stub File in phpstan.neon

In your phpstan.neon (or phpstan.neon.dist) config file, add the stub file path:

includes:
    - ./vendor/phpstan/phpstan/conf/bleedingEdge.neon

parameters:
    scanFiles:
        - stubs/pest-extensions.stub

Make sure the path matches where you placed the stub file.


3. (Alternatively) Use Docblocks (less preferred for custom globals)

You can also document the method via docblocks if you are using your own custom class, but for Pest custom expectations, stub files offer a more robust solution.


4. Clear the Cache

After making these changes, it's a good idea to clear PHPStan’s cache:

php artisan clear-compiled
# or
rm -rf ./storage/framework/cache/data
# and
vendor/bin/phpstan clear-result-cache

Summary

Why does it work locally, but not on CI?

  • Locally, you might have IDE helpers or cached analysis that mask the problem, but GitHub Actions runs with a fresh environment, making issues like missing stubs apparent.

Use the stub file approach for custom Pest expectations so PHPStan can recognize them! If you add more custom expectations, be sure to update your stub as needed.

If you need further help, let me know!

Pixelairport's avatar

Thats what I already testet. But then I get:


 ------ --------------------------------------------------------------------------------------- 
  Line   tests/Expectations.php                                                                 
 ------ --------------------------------------------------------------------------------------- 
  11     Call to an undefined method Pest\Expectation<null>::extend().                          
         🪪  method.notFound                                                                    
  14     Access to an undefined property Pest\Expectation::$value.                              
         🪪  property.notFound                                                                  
         💡  Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property  
  14     Call to an undefined method Pest\Expectation<mixed>::toMatch().                        
         🪪  method.notFound                                                                    
 ------ --------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------ 
  Line   tests/Feature/Traits/HasUuidTest.php                  
 ------ ------------------------------------------------------ 
  64     Call to an undefined method Pest\Expectation::and().  
         🪪  method.notFound                                   
 ------ ------------------------------------------------------ 


martinbean's avatar

@pixelairport First thing I’d check is that you’re using the exact same version of PHP both locally and in your GitHub Actions workflow, as different versions may result in different versions of packages being installed.

1 like
Pixelairport's avatar
Pixelairport
OP
Best Answer
Level 12

As a beginner with phpstan I found out, after hours of trying, that it is not needed to also make phpstan for pest tests. I copied the phpstan.neon.dist content from a tutorial. But in source was also /tests as path. I deleted this. Now everything works. Main problem was, that Expectations were not found. But I work alone or in small teams. It needs too much time to do also phpstan checks for tests. And I saw also in bigger repos, that there is also /tests not included. Thx for help... sometimes its a bit difficult to start new topics.

Please or to participate in this conversation.