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

Test is marked as skipped when using another class as data provider #4757

Closed rela589n closed 3 years ago

rela589n commented 3 years ago
Q A
PHPUnit version 9.5.8
PHP version 8.0.9
Installation Method Composer

composer info | sort

Vendor Version Description
asm89/stack-cors v2.0.3 Cross-origin resource sharing library and stack middleware
barryvdh/laravel-ide-helper v2.10.0 Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.
barryvdh/reflection-docblock v2.0.6
brick/math 0.9.2 Arbitrary-precision arithmetic library
composer/ca-bundle 1.2.10 Lets you find a path to the system CA bundle, and includes a fallback to the Mozilla CA bundle.
composer/composer 2.1.5 Composer helps you declare, manage and install dependencies of PHP projects. It ensures you have the right stack everywhere.
composer/metadata-minifier 1.0.0 Small utility library that handles metadata minification and expansion.
composer/package-versions-deprecated 1.11.99.2 Composer plugin that provides efficient querying for installed package versions (no runtime IO)
composer/semver 3.2.5 Semver library that offers utilities, version constraint parsing and validation.
composer/spdx-licenses 1.5.5 SPDX licenses list and validation library.
composer/xdebug-handler 2.0.2 Restarts a process without Xdebug.
dflydev/dot-access-data v3.0.0 Given a deep data structure, access data by dot notation.
doctrine/cache 2.1.1 PHP Doctrine Cache library is a popular cache implementation that supports many different drivers such as redis, memcache, apc, mongodb and others.
doctrine/dbal 3.1.1 Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.
doctrine/deprecations v0.5.3 A small layer on top of trigger_error(E_USER_DEPRECATED) or PSR-3 logging with options to disable all deprecations or selectively for packages.
doctrine/event-manager 1.1.1 The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects.
doctrine/inflector 2.0.3 PHP Doctrine Inflector is a small library that can perform string manipulations with regard to upper/lowercase and singular/plural forms of words.
doctrine/instantiator 1.4.0 A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer 1.2.1 PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.
dragonmantank/cron-expression v3.1.0 CRON for PHP: Calculate the next or previous run date and determine if a CRON expression is due
egulias/email-validator 2.1.25 A library for validating emails against several RFCs
evenement/evenement v3.0.1 Événement is a very simple event dispatching library for PHP
facade/flare-client-php 1.8.1 Send PHP errors to Flare
facade/ignition 2.11.2 A beautiful error page for Laravel applications.
facade/ignition-contracts 1.0.2 Solution contracts for Ignition
fakerphp/faker v1.15.0 Faker is a PHP library that generates fake data for you.
fideloper/proxy 4.4.1 Set trusted proxies for Laravel
filp/whoops 2.14.0 php error handling for cool kids
fruitcake/laravel-cors v2.0.4 Adds CORS (Cross-Origin Resource Sharing) headers support in your Laravel application
graham-campbell/result-type v1.0.1 An Implementation Of The Result Type
guzzlehttp/guzzle 7.3.0 Guzzle is a PHP HTTP client library
guzzlehttp/promises 1.4.1 Guzzle promises library
guzzlehttp/psr7 1.8.2 PSR-7 message implementation that also provides common utility methods
hamcrest/hamcrest-php v2.0.1 This is the PHP port of Hamcrest Matchers
irazasyed/telegram-bot-sdk v3.4.1 The Unofficial Telegram Bot API PHP SDK
jaggedsoft/php-binance-api v0.5.28 PHP Binance API is an asynchronous PHP library for the Binance API designed to be easy to use.
justinrainbow/json-schema 5.2.11 A library to validate a json schema.
laravel/framework v8.53.1 The Laravel Framework.
laravel/tinker v2.6.1 Powerful REPL for the Laravel framework.
league/commonmark 2.0.1 Highly-extensible PHP Markdown parser which fully supports the CommonMark spec and GitHub-Flavored Markdown (GFM)
league/config v1.1.0 Define configuration arrays with strict schemas and access values with dot notation
league/event 2.2.0 Event package
league/flysystem 1.1.4 Filesystem abstraction: Many filesystems, one API.
league/mime-type-detection 1.7.0 Mime-type detection for Flysystem
mockery/mockery 1.4.3 Mockery is a simple yet flexible PHP mock object framework
monolog/monolog 2.3.2 Sends your logs to files, sockets, inboxes, databases and various web services
myclabs/deep-copy 1.10.2 Create deep copies (clones) of your objects
nesbot/carbon 2.51.1 An API extension for DateTime that supports 281 different languages.
nette/schema v1.2.1 📐 Nette Schema: validating data structures against a given Schema.
nette/utils v3.2.2 🛠 Nette Utils: lightweight utilities for string & array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.
nikic/php-parser v4.12.0 A PHP parser written in PHP
nunomaduro/collision v5.6.0 Cli error handling for console/command-line PHP applications.
opis/closure 3.6.2 A library that can be used to serialize closures (anonymous functions) and arbitrary objects.
phar-io/manifest 2.0.3 Component for reading phar.io manifest information from a PHP Archive (PHAR)
phar-io/version 3.1.0 Library for handling version information and constraints
phpdocumentor/reflection-common 2.2.0 Common reflection classes used by phpdocumentor to reflect the code structure
phpdocumentor/reflection-docblock 5.2.2 With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.
phpdocumentor/type-resolver 1.4.0 A PSR-5 based resolver of Class names, Types and Structural Element Names
phpoption/phpoption 1.7.5 Option Type for PHP
phpspec/prophecy 1.13.0 Highly opinionated mocking framework for PHP 5.3+
phpunit/php-code-coverage 9.2.6 Library that provides collection, processing, and rendering functionality for PHP code coverage information.
phpunit/php-file-iterator 3.0.5 FilterIterator implementation that filters files based on a list of suffixes.
phpunit/php-invoker 3.1.1 Invoke callables with a timeout
phpunit/php-text-template 2.0.4 Simple template engine.
phpunit/php-timer 5.0.3 Utility class for timing
phpunit/phpunit 9.5.8 The PHP Unit Testing framework.
psr/container 1.1.1 Common Container Interface (PHP FIG PSR-11)
psr/event-dispatcher 1.0.0 Standard interfaces for event handling.
psr/http-client 1.0.1 Common interface for HTTP clients
psr/http-message 1.0.1 Common interface for HTTP messages
psr/log 1.1.4 Common interface for logging libraries
psr/simple-cache 1.0.1 Common interfaces for simple caching
psy/psysh v0.10.8 An interactive shell for modern PHP.
ralouphie/getallheaders 3.0.3 A polyfill for getallheaders.
ramsey/collection 1.2.0 A PHP 7.2+ library for representing and manipulating collections.
ramsey/uuid 4.1.1 A PHP library for generating and working with universally unique identifiers (UUIDs).
ratchet/pawl v0.3.5 Asynchronous WebSocket client
ratchet/rfc6455 v0.3 RFC6455 WebSocket protocol handler
react/cache v1.1.1 Async, Promise-based cache interface for ReactPHP
react/dns v1.8.0 Async DNS resolver for ReactPHP
react/event-loop v1.2.0 ReactPHP's core reactor event loop that libraries can use for evented I/O.
react/promise v2.8.0 A lightweight implementation of CommonJS Promises/A for PHP
react/promise-timer v1.7.0 A trivial implementation of timeouts for Promises, built on top of ReactPHP.
react/socket v1.9.0 Async, streaming plaintext TCP/IP and secure TLS socket server and client connections for ReactPHP
react/stream v1.2.0 Event-driven readable and writable streams for non-blocking I/O in ReactPHP
roave/security-advisories dev-latest 52a1261 Prevents installation of composer packages with known security vulnerabilities: no API, simply require it
sebastian/cli-parser 1.0.1 Library for parsing CLI options
sebastian/code-unit 1.0.8 Collection of value objects that represent the PHP code units
sebastian/code-unit-reverse-lookup 2.0.3 Looks up which function or method a line of code belongs to
sebastian/comparator 4.0.6 Provides the functionality to compare PHP values for equality
sebastian/complexity 2.0.2 Library for calculating the complexity of PHP code units
sebastian/diff 4.0.4 Diff implementation
sebastian/environment 5.1.3 Provides functionality to handle HHVM/PHP environments
sebastian/exporter 4.0.3 Provides the functionality to export PHP variables for visualization
sebastian/global-state 5.0.3 Snapshotting of global state
sebastian/lines-of-code 1.0.3 Library for counting the lines of code in PHP source code
sebastian/object-enumerator 4.0.4 Traverses array structures and object graphs to enumerate all referenced objects
sebastian/object-reflector 2.0.4 Allows reflection of object attributes, including inherited and non-public ones
sebastian/recursion-context 4.0.4 Provides functionality to recursively process PHP variables
sebastian/resource-operations 3.0.3 Provides a list of PHP built-in functions that operate on resources
sebastian/type 2.3.4 Collection of value objects that represent the types of the PHP type system
sebastian/version 3.0.2 Library that helps with managing the version number of Git-hosted PHP projects
seld/jsonlint 1.8.3 JSON Linter
seld/phar-utils 1.1.1 PHAR file format utilities, for when PHP phars you up
swiftmailer/swiftmailer v6.2.7 Swiftmailer, free feature-rich PHP mailer
symfony/console v5.3.6 Eases the creation of beautiful and testable command line interfaces
symfony/css-selector v5.3.4 Converts CSS selectors to XPath expressions
symfony/deprecation-contracts v2.4.0 A generic function and convention to trigger deprecation notices
symfony/error-handler v5.3.4 Provides tools to manage errors and ease debugging PHP code
symfony/event-dispatcher v5.3.4 Provides tools that allow your application components to communicate with each other by dispatching events and listening to them
symfony/event-dispatcher-contracts v2.4.0 Generic abstractions related to dispatching event
symfony/filesystem v5.3.4 Provides basic utilities for the filesystem
symfony/finder v5.3.4 Finds files and directories via an intuitive fluent interface
symfony/http-client-contracts v2.4.0 Generic abstractions related to HTTP clients
symfony/http-foundation v5.3.6 Defines an object-oriented layer for the HTTP specification
symfony/http-kernel v5.3.6 Provides a structured process for converting a Request into a Response
symfony/mime v5.3.4 Allows manipulating MIME messages
symfony/polyfill-ctype v1.23.0 Symfony polyfill for ctype functions
symfony/polyfill-iconv v1.23.0 Symfony polyfill for the Iconv extension
symfony/polyfill-intl-grapheme v1.23.1 Symfony polyfill for intl's grapheme_* functions
symfony/polyfill-intl-idn v1.23.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-intl-normalizer v1.23.0 Symfony polyfill for intl's Normalizer class and related functions
symfony/polyfill-mbstring v1.23.1 Symfony polyfill for the Mbstring extension
symfony/polyfill-php72 v1.23.0 Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions
symfony/polyfill-php73 v1.23.0 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
symfony/polyfill-php80 v1.23.1 Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions
symfony/polyfill-php81 v1.23.0 Symfony polyfill backporting some PHP 8.1+ features to lower PHP versions
symfony/process v5.3.4 Executes commands in sub-processes
symfony/routing v5.3.4 Maps an HTTP request to a set of configuration variables
symfony/service-contracts v2.4.0 Generic abstractions related to writing services
symfony/string v5.3.3 Provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme clusters in a unified way
symfony/translation v5.3.4 Provides tools to internationalize your application
symfony/translation-contracts v2.4.0 Generic abstractions related to translation
symfony/var-dumper v5.3.6 Provides mechanisms for walking through any arbitrary PHP variable
theseer/tokenizer 1.2.1 A small library for converting tokenized PHP source code into XML and potentially other formats
tijsverkoyen/css-to-inline-styles 2.2.3 CssToInlineStyles is a class that enables you to convert HTML-pages/files into HTML-pages/files with inline styles. This is very useful when you're sending emails.
vlucas/phpdotenv v5.3.0 Loads environment variables from .env to getenv(), $_ENV and $_SERVER automagically.
voku/portable-ascii 1.5.6 Portable ASCII library - performance optimized (ascii) string functions for php.
webmozart/assert 1.10.0 Assertions to validate method input/output with nice error messages.

Summary

There's a test using data provider:

/** @dataProvider comparisonDataProvider */
public function testComparesValues(int $first, int $second, string $method): void
{
// some body
}

Data provider returns instance of iterator:

public function comparisonDataProvider()
{
        // either way makes test skipped
        return (new ComparisonProvider());
        return (new ComparisonProvider())->getIterator();
        return iterator_to_array((new ComparisonProvider())->getIterator());
}

Class ComparisonProvider located in tests/Unit/Provider/ComparisonProvider.php:

<?php

declare(strict_types=1);

namespace Tests\Unit\Provider;

use Generator;
use IteratorAggregate;
use JetBrains\PhpStorm\Immutable;

#[Immutable]
final class ComparisonProvider implements IteratorAggregate
{
    private const COMPARISON_METHODS = ['gt', 'gte', 'lt', 'lte', 'eq'];

    public function getIterator(): Generator
    {
        foreach (
            [
                [11, 12],
                [0, 1],
                [2, 1],
                [10, 0],
                [5, 5],
                [31, 31],
            ] as $pair
        ) {
            foreach (self::COMPARISON_METHODS as $method) {
                yield [...$pair, $method];
            }
        }
    }
}

Current behavior

Currently test using shown data provider is skipped.

1) Tests\Unit\Domain\ValueObject\MoneyTest::testComparesValues
Test for Tests\Unit\Domain\ValueObject\MoneyTest::testComparesValues skipped by data provider
PHPUnit\Framework\SkippedTestError: <no message>

OK, but incomplete, skipped, or risky tests!
Tests: 28, Assertions: 45, Skipped: 1.

How to reproduce

See code from summary.

Expected behavior

Expected behavior is that test will be executed the same way if code from \Tests\Unit\Provider\ComparisonProvider::getIterator was just pasted into comparisonDataProvider:

Time: 00:00.034, Memory: 8.00 MB

OK (57 tests, 75 assertions)
sebastianbergmann commented 3 years ago

Thank you for your report.

Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting.

Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue.

rela589n commented 3 years ago

Actually code inside test case doesn't matter here. It could be just assert that true is true.

/** @dataProvider comparisonDataProvider */
public function testComparesValues(int $first, int $second, string $method): void
{
        self::assertNotNull($first);
        self::assertNotNull($second);
        self::assertNotNull($method);
}

I finally got what the problem is all about.

My code comparisonDataProvider was like this:

    public function comparisonDataProvider()
    {
        return (new ComparisonProvider());
        foreach (
            [
                [11, 12],
                [0, 1],
                [2, 1],
                [10, 0],
                [5, 5],
                [31, 31],
            ] as $pair
        ) {
            foreach (self::COMPARISON_METHODS as $method) {
                yield [...$pair, $method];
            }
        }
    }

I moved main body into another class, returned it's instance on the first line. Probably it is related more to PHP, because function has an yield which will never be executed, which caused such problem. Removing remaining dead code from method fixes things up.

I think this should be described in error message why phpunit is skipping test. It has taken me about an hour to figure out.