sebastianbergmann / phpunit

The PHP Unit Testing framework.
https://phpunit.de/
BSD 3-Clause "New" or "Revised" License
19.67k stars 2.2k forks source link

Extensions aren't found in the include path (in a Composer project) #5918

Closed BlairCooper closed 1 month ago

BlairCooper commented 1 month ago
Q A
PHPUnit version 11.3.0
PHP version 8.2.21
Installation Method Composer

Summary

When using a Composer project, if an extension is in a folder specified by the include-path parameter, the class is not found. By default Composer does not use the include-path when loading classes. While it is possible to set the Composer "use-include-path" configuration parameter to true, this is not practical when something outside the project (e.g. Eclipse) is the one launching PHPUnit with an extension.

Current behavior

The extension, located in the include path is not loaded and PHPUnit displays: "Cannot bootstrap extension because class does not exist".

How to reproduce

Assuming:

Running the command: "/php8.2.21/php /myproject/vendor/phpunit/phpunit/phpunit --include-path /extensions --extension=PHPUnitExtensionLogger /myproject/test" will run the unit tests defined in the project but will report a warning at the end, "Cannot bootstrap extension because class PHPUnitExtensionLogger does not exist".

Expected behavior

If the class_exists() method is unable to locate the file, the bootstrap method will attempt to find the class in a file with the same name using the include path.

Additional information

The ExtensionBootstrapper boostrap() method only calls class_exists to try and load the class. If Composer has not been configured to use the include path this call will return false. The project may not have control of how PHPUnit is started or the location of the extension such as in the case where Eclipse via the PDT is launching PHPUnit. Because of this it would be unreasonable to expect someone to change the composer.json file, even if they knew that is what needed to be done.

Composer Info output: brain/monkey 2.6.1 Mocking utility for PHP functions and WordPress plugin API json-mapper/json-mapper 2.22.2 Map JSON structures to PHP classes monolog/monolog 3.7.0 Sends your logs to files, sockets, inboxes, databases and various web services antecedent/patchwork 2.1.28 Method redefinition (monkey-patching) functionality for PHP. composer/installers 2.3.0 A multi-framework Composer library installer consolidation/annotated-command 4.10.0 Initialize Symfony Console commands from annotated command class methods. consolidation/config 3.0.0 Provide configuration services for a commandline tool. consolidation/log 3.1.0 Improved Psr-3 / Psr\Log logger based on Symfony Console components. consolidation/output-formatters 4.5.0 Format text by applying transformations provided by plug-in formatters. consolidation/robo 5.0.0 Modern task runner dflydev/dot-access-data 3.0.3 Given a deep data structure, access data by dot notation. doctrine/annotations 2.0.1 Docblock Annotations Parser doctrine/lexer 3.0.1 PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers. grasmash/expander 3.0.0 Expands internal property references in PHP arrays file. guzzlehttp/guzzle 7.9.2 Guzzle is a PHP HTTP client library guzzlehttp/promises 2.0.3 Guzzle promises library guzzlehttp/psr7 2.7.0 PSR-7 message implementation that also provides common utility methods hamcrest/hamcrest-php 2.0.1 This is the PHP port of Hamcrest Matchers mockery/mockery 1.6.12 Mockery is a simple yet flexible PHP mock object framework myclabs/deep-copy 1.12.0 Create deep copies (clones) of your objects myclabs/php-enum 1.8.4 PHP Enum implementation nikic/php-parser 5.1.0 A PHP parser written in PHP phar-io/manifest 2.0.4 Component for reading phar.io manifest information from a PHP Archive (PHAR) phar-io/version 3.2.1 Library for handling version information and constraints phootwork/collection 3.2.2 The phootwork library fills gaps in the php language and provides better solutions than the existing o... phootwork/lang 3.2.2 Missing PHP language constructs psr/cache 3.0.0 Common interface for caching libraries psr/container 2.0.2 Common Container Interface (PHP FIG PSR-11) psr/event-dispatcher 1.0.0 Standard interfaces for event handling. psr/http-client 1.0.3 Common interface for HTTP clients psr/http-factory 1.1.0 PSR-17: Common interfaces for PSR-7 HTTP message factories psr/http-message 2.0 Common interface for HTTP messages psr/log 3.0.0 Common interface for logging libraries psr/simple-cache 3.0.0 Common interfaces for simple caching phpowermove/docblock 4.0 PHP Docblock parser and generator. An API to read and write Docblocks. phpstan/phpstan 1.11.9 PHPStan - PHP Static Analysis Tool ralouphie/getallheaders 3.0.3 A polyfill for getallheaders. sebastian/cli-parser 3.0.2 Library for parsing CLI options sebastian/code-unit-reverse-lookup 4.0.1 Looks up which function or method a line of code belongs to sebastian/code-unit 3.0.1 Collection of value objects that represent the PHP code units sebastian/comparator 6.0.1 Provides the functionality to compare PHP values for equality sebastian/complexity 4.0.1 Library for calculating the complexity of PHP code units sebastian/diff 6.0.2 Diff implementation sebastian/environment 7.2.0 Provides functionality to handle HHVM/PHP environments sebastian/exporter 6.1.3 Provides the functionality to export PHP variables for visualization sebastian/global-state 7.0.2 Snapshotting of global state sebastian/lines-of-code 3.0.1 Library for counting the lines of code in PHP source code sebastian/object-enumerator 6.0.1 Traverses array structures and object graphs to enumerate all referenced objects sebastian/object-reflector 4.0.1 Allows reflection of object attributes, including inherited and non-public ones phpunit/php-code-coverage 11.0.5 Library that provides collection, processing, and rendering functionality for PHP code coverage inform... phpunit/php-file-iterator 5.0.1 FilterIterator implementation that filters files based on a list of suffixes. phpunit/php-invoker 5.0.1 Invoke callables with a timeout phpunit/php-text-template 4.0.1 Simple template engine. phpunit/php-timer 7.0.1 Utility class for timing phpunit/phpunit 11.3.0 The PHP Unit Testing framework. sebastian/recursion-context 6.0.2 Provides functionality to recursively process PHP variables sebastian/type 5.0.1 Collection of value objects that represent the types of the PHP type system sebastian/version 5.0.1 Library that helps with managing the version number of Git-hosted PHP projects symfony/cache-contracts 3.5.0 Generic abstractions related to caching symfony/cache 7.1.3 Provides extended PSR-6, PSR-16 (and tags) implementations symfony/console 7.1.3 Eases the creation of beautiful and testable command line interfaces symfony/deprecation-contracts 3.5.0 A generic function and convention to trigger deprecation notices symfony/event-dispatcher-contracts 3.5.0 Generic abstractions related to dispatching event symfony/event-dispatcher 7.1.1 Provides tools that allow your application components to communicate with each other by dispatching ev... symfony/filesystem 7.1.2 Provides basic utilities for the filesystem symfony/finder 7.1.3 Finds files and directories via an intuitive fluent interface symfony/polyfill-ctype 1.30.0 Symfony polyfill for ctype functions symfony/polyfill-intl-grapheme 1.30.0 Symfony polyfill for intl's grapheme_* functions symfony/polyfill-intl-normalizer 1.30.0 Symfony polyfill for intl's Normalizer class and related functions symfony/polyfill-mbstring 1.30.0 Symfony polyfill for the Mbstring extension symfony/polyfill-php73 1.30.0 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions symfony/polyfill-php81 1.30.0 Symfony polyfill backporting some PHP 8.1+ features to lower PHP versions symfony/process 7.1.3 Executes commands in sub-processes symfony/service-contracts 3.5.0 Generic abstractions related to writing services symfony/string 7.1.3 Provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme cluste... symfony/var-exporter 7.1.2 Allows exporting any serializable PHP data structure to plain PHP code symfony/yaml 7.1.1 Loads and dumps YAML files league/container 4.2.2 A fast and intuitive dependency injection container. theseer/tokenizer 1.2.3 A small library for converting tokenized PHP source code into XML and potentially other formats

sebastianbergmann commented 1 month ago

If the class_exists() method is unable to locate the file, the bootstrap method will attempt to find the class in a file with the same name using the include path.

This is not the expected behaviour, sorry. The autoloader needs to know how to load your extension.

BlairCooper commented 1 month ago

The --printer option prior to 10.x supported this, at least a variation of this. Disappointed that the response to #5905 was to use an extension only to now be told PHPUnit won't support external/3rd Party extensions.