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.46k stars 341 forks source link

[Bug]: Pest not support when composer "vendor" dir is customised (aka in a subdirectory not named vendor/) #1130

Open luri-fr opened 5 months ago

luri-fr commented 5 months ago

What Happened

I use composer config to change vendor/ directory to libs/. (see : https://getcomposer.org/doc/06-config.md#vendor-dir ) But when I use ./libs/bin/pest --init, I have somes error :

PHP Warning:  include_once(D:\[...]\MyOidc\libs\pestphp\pest/vendor/autoload.php): Failed to open stream: No such file or directory in D:\[...]\MyOidc\libs\pestphp\pest\bin\pest on line 63
PHP Stack trace:
PHP   1. {main}() D:\[...]\MyOidc\libs\bin\pest:0
PHP   2. include() D:\[...]\MyOidc\libs\bin\pest:119
PHP   3. {closure:D:\[...]\MyOidc\libs\pestphp\pest\bin\pest:12-99}() D:\[...]\MyOidc\libs\pestphp\pest\bin\pest:99

Warning: include_once(D:\[...]\MyOidc\libs\pestphp\pest/vendor/autoload.php): Failed to open stream: No such file or directory in D:\[...]\MyOidc\libs\pestphp\pest\bin\pest on line 63

Call Stack:
    0.0002     559592   1. {main}() D:\[...]\MyOidc\libs\bin\pest:0
    0.0003     571984   2. include('D:\[...]\MyOidc\libs\pestphp\pest\bin\pest') D:\[...]\MyOidc\libs\bin\pest:119
    0.0004     572368   3. {closure:D:\[...]\MyOidc\libs\pestphp\pest\bin\pest:12-99}() D:\[...]\MyOidc\libs\pestphp\pest\bin\pest:99

PHP Warning:  include_once(): Failed opening 'D:\[...]\MyOidc\libs\pestphp\pest/vendor/autoload.php' for inclusion (include_path='.;C:\php\pear') in D:\[...]\MyOidc\libs\pestphp\pest\bin\pest on line 63
PHP Stack trace:
PHP   1. {main}() D:\[...]\MyOidc\libs\bin\pest:0
PHP   2. include() D:\[...]\MyOidc\libs\bin\pest:119
PHP   3. {closure:D:\[...]\MyOidc\libs\pestphp\pest\bin\pest:12-99}() D:\[...]\MyOidc\libs\pestphp\pest\bin\pest:99

Warning: include_once(): Failed opening 'D:\[...]\MyOidc\libs\pestphp\pest/vendor/autoload.php' for inclusion (include_path='.;C:\php\pear') in D:\[...]\MyOidc\libs\pestphp\pest\bin\pest on line 63

Call Stack:
    0.0002     559592   1. {main}() D:\[...]\MyOidc\libs\bin\pest:0
    0.0003     571984   2. include('D:\[...]\MyOidc\libs\pestphp\pest\bin\pest') D:\[...]\MyOidc\libs\bin\pest:119
    0.0004     572368   3. {closure:D:\[...]\MyOidc\libs\pestphp\pest\bin\pest:12-99}() D:\[...]\MyOidc\libs\pestphp\pest\bin\pest:99

PHP Fatal error:  Uncaught Error: Class "Symfony\Component\Console\Input\ArgvInput" not found in D:\[...]\MyOidc\libs\pestphp\pest\bin\pest:69
Stack trace:
#0 D:\[...]\MyOidc\libs\pestphp\pest\bin\pest(99): {closure}()
#1 D:\[...]\MyOidc\libs\bin\pest(119): include('D:\\[...]\\...')
#2 {main}
  thrown in D:\[...]\MyOidc\libs\pestphp\pest\bin\pest on line 69

Fatal error: Uncaught Error: Class "Symfony\Component\Console\Input\ArgvInput" not found in D:\[...]\MyOidc\libs\pestphp\pest\bin\pest on line 69

Error: Class "Symfony\Component\Console\Input\ArgvInput" not found in D:\[...]\MyOidc\libs\pestphp\pest\bin\pest on line 69

Call Stack:
    0.0002     559592   1. {main}() D:\[...]\MyOidc\libs\bin\pest:0
    0.0003     571984   2. include('D:\[...]\MyOidc\libs\pestphp\pest\bin\pest') D:\Gilles\Docs\projets\MyOidc\libs\bin\pest:119
    0.0004     572368   3. {closure:D:\[...]\MyOidc\libs\pestphp\pest\bin\pest:12-99}() D:\[...]\MyOidc\libs\pestphp\pest\bin\pest:99

Composer explicitly support custom vendor directory, so Pest must support them.

I'll see if there's an easy modification I can make to correct this.

How to Reproduce

composer install --dev

./libs/bin/pest --init

Sample Repository

No response

Pest Version

2.34

PHP Version

8.2.16

Operation System

Windows

Notes

No response

luri-fr commented 5 months ago

Ok. In my libs/bin/pest file, I have :

#!/usr/bin/env php
<?php

/**
 * Proxy PHP file generated by Composer
 *
 * This file includes the referenced bin path (../pestphp/pest/bin/pest)
 * using a stream wrapper to prevent the shebang from being output on PHP<8
 *
 * @generated
 */

namespace Composer;

$GLOBALS['_composer_bin_dir'] = __DIR__;
$GLOBALS['_composer_autoload_path'] = __DIR__ . '/..'.'/autoload.php';

if (PHP_VERSION_ID < 80000) {
[...]

So, if I replace Line 54 of pestphp/pest/bin/pest :

    $vendorPath = dirname(__DIR__, 4).'/vendor/autoload.php';

by

    $vendorPath = $GLOBALS['_composer_autoload_path'];

it's working for me. But this fix is linked to the version of composer (2.7.2) and i don't know in which version composer introduce $GLOBALS['_composer_autoload_path'] and which minimum version of composer you want to support. So I don't make PR. (I don't want to break other people.)

luri-fr commented 5 months ago

Well... not really working.

.\libs\bin\pest --init create file in libs\bin\ directory and after I move the files .\libs\bin\pest show :

   INFO  Cannot open bootstrap script "D:\[...]\MyOidc\vendor/autoload.php".
luri-fr commented 5 months ago

Ok I found.

In phpunit.xml, I must replace :

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>

by

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
         bootstrap="libs/autoload.php"
         colors="true"
>

Perhaps this can be added in the documention ?