pestphp / pest

Pest is an elegant PHP testing Framework with a focus on simplicity, meticulously designed to bring back the joy of testing in PHP.
https://pestphp.com
MIT License
9.42k stars 338 forks source link

Feature tests are ignoring the uses statement in Pest.php #704

Closed normykinz closed 1 year ago

normykinz commented 1 year ago

In a fresh Laravel project with the Laravel plugin installed generated tests seem to be ignoring the uses statment in Pest.php

uses(
    Tests\TestCase::class,
    Illuminate\Foundation\Testing\RefreshDatabase::class,
)->in('Feature');

Doesn't work but using the same uses call in the test itself does work.

nunomaduro commented 1 year ago

Can you elaborate a little more, and share more detail? Environment used, etc.

ConnySjoblom commented 1 year ago

Might have something related to this also in our env, upgrading to Pest 2 gives this error: A facade root has not been set. which would seem to be Laravel not being bootstrapped

normykinz commented 1 year ago

Here's my phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" cacheDirectory=".phpunit.cache">
  <testsuites>
    <testsuite name="Unit">
      <directory suffix="Test.php">./tests/Unit</directory>
    </testsuite>
    <testsuite name="Feature">
      <directory suffix="Test.php">./tests/Feature</directory>
    </testsuite>
  </testsuites>
  <coverage>
    <include>
      <directory suffix=".php">./app</directory>
    </include>
  </coverage>
  <php>
    <env name="APP_ENV" value="testing"/>
    <env name="BCRYPT_ROUNDS" value="4"/>
    <env name="CACHE_DRIVER" value="array"/>
    <env name="DB_CONNECTION" value="sqlite"/>
    <env name="DB_DATABASE" value=":memory:"/>
    <env name="MAIL_MAILER" value="array"/>
    <env name="QUEUE_CONNECTION" value="sync"/>
    <env name="SESSION_DRIVER" value="array"/>
    <env name="TELESCOPE_ENABLED" value="false"/>
  </php>
</phpunit>

It's also ignoring any custom functions in Pest.php.

jlespanollabelgrup commented 1 year ago

I have the same problem. Also, I can't access to variables I declared in beforeEach and custom functions in file Pest.php.

MirasMustimov commented 1 year ago

Hi guys, same here, cant seem to use my custom helper functions #707.

nunomaduro commented 1 year ago

Seems to be related to the usage of Windows. We are checking...

ConnySjoblom commented 1 year ago

I'm experiencing this problem in Docker on MacOS

nunomaduro commented 1 year ago

@ConnySjoblom We are checking, thanks!

nunomaduro commented 1 year ago

@ConnySjoblom Can you share more details? Like full pwd of your application, how are you running Pest on docker, etc?

ConnySjoblom commented 1 year ago

Code located in /Users/connysjoblom/Code/<app name> Running pest with sail bin pest

We're using beforeEach quite heavily, and CreateApplication.php is standard except some usage of Http::fake()

Any other specifics you would need?

nunomaduro commented 1 year ago

What's your tests directory structure? Can I see your Pest.php file?

ConnySjoblom commented 1 year ago
Bridge
CreatesApplication.php
Feature
Pest.php
TestCase.php
Unit
Utilities

No subfolders within those folders

nunomaduro commented 1 year ago

@ConnySjoblom So, if you declare a custom helper, within Pest.php, you can't use it in your Feature tests?

ConnySjoblom commented 1 year ago

Using the example toBeOne() in a new empty testfile I get this:

Call to a member function toBeOne() on int

  at tests/Feature/Asd.php:4
      1▕ <?php
      2▕
      3▕ it('works', function () {
  ➜   4▕     expect(1)->toBeOne();
      5▕ });
      6▕
nunomaduro commented 1 year ago

Hope is not asking too much. But can you try to understand, what within this file is not working for your environment? https://github.com/pestphp/pest/blob/2.x/src/Bootstrappers/BootFiles.php#L43

ConnySjoblom commented 1 year ago

I can have a look at it, takes a minute before I can begin though

ConnySjoblom commented 1 year ago
^ "/var/www/html/tests/Unit//Expectations"
^ "/var/www/html/tests/Unit//Expectations.php"
^ "/var/www/html/tests/Unit//Helpers"
^ "/var/www/html/tests/Unit//Helpers.php"
^ "/var/www/html/tests/Unit//Pest.php"

all hit line 46

nunomaduro commented 1 year ago

On the Pest\ConfigLoader file, can you put this code on the line 63, and let me know how it goes?

        $basePath = str_contains($directory, DIRECTORY_SEPARATOR)
            ? Str::beforeLast($directory, DIRECTORY_SEPARATOR)
            : Str::beforeLast($directory, '/');

        return is_dir($basePath) ? $basePath : self::DEFAULT_TESTS_PATH;
MirasMustimov commented 1 year ago

Hope is not asking too much. But can you try to understand, what within this file is not working for your environment? https://github.com/pestphp/pest/blob/2.x/src/Bootstrappers/BootFiles.php#L43

I dumped whether file exists and what the file names is

foreach (self::STRUCTURE as $filename) {
            $filename = sprintf('%s%s%s', $testsPath, DIRECTORY_SEPARATOR, $filename);
            dump($filename);
            dump(file_exists($filename));
            if (! file_exists($filename)) {
                continue;
            }

and my output is this:

^ "C:\OSPanel\domains\pest2./tests/Unit/\Expectations"
^ false
^ "C:\OSPanel\domains\pest2./tests/Unit/\Expectations.php"
^ false
^ "C:\OSPanel\domains\pest2./tests/Unit/\Helpers"
^ false
^ "C:\OSPanel\domains\pest2./tests/Unit/\Helpers.php"
^ false
^ "C:\OSPanel\domains\pest2./tests/Unit/\Pest.php"
^ false

seems like it fails to find the files

ConnySjoblom commented 1 year ago
   $basePath = str_contains($directory, DIRECTORY_SEPARATOR)
            ? Str::beforeLast($directory, DIRECTORY_SEPARATOR)
            : Str::beforeLast($directory, '/');

        return is_dir($basePath) ? $basePath : self::DEFAULT_TESTS_PATH;

Same error with this here, but noticed we have two testsuites defined in phpunit.xml like this:

      <testsuite name="Unit">
        <directory>tests/Unit</directory>
        <directory>tests/Utilities</directory>
      </testsuite>
      <testsuite name="Feature">
        <directory>tests/Feature</directory>
      </testsuite>

Removing these and creating one new like this

      <testsuite name="Unit">
        <directory>tests</directory>
      </testsuite>

Seems to make everything load as it should

nunomaduro commented 1 year ago

Fixed found. Releasing Pest with that fix.

nunomaduro commented 1 year ago

Pest v2.0.2 released, and it fixes this issue. Please update and let me know how it goes.

normykinz commented 1 year ago

Brilliant thanks for all your great work, Nuno. It's woking like a champion now!

ConnySjoblom commented 1 year ago

I'm a bit late to the show, but I can also confirm it working after the update, thanks for the quick debugging @nunomaduro !