Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using @testWith annotation may generate PHP Warning: Uninitialized string offset 0 #5795

Closed
fredericgboutin-yapla opened this issue Apr 4, 2024 · 4 comments

Comments

@fredericgboutin-yapla
Copy link

Q A
PHPUnit version 10.5.16 (also in 9.x)
PHP version 8.2.14
Installation Method Composer

Summary

Using @testWith might generate PHP Warning: Uninitialized string offset: 0

Current behavior

You launch phpunit and before you see the header you see 2 Warnings

How to reproduce

This is quite easy. You have to use @testwith in a way that it is followed with a blank line. For example,

/**
 * @testWith		 [1]
 * 			 [2]
 * 			 [3]
 * 
 * @testdox This test should make phpunit spit a PHP Warning !
 */
public function testExample($arg): void
{
	$this->assertIsInt($arg);
}	

This is probably related to the explode("\n", ...) that includes empty lines in between annotations

foreach (explode("\n", $annotationContent) as $candidateRow) {

Expected behavior

No PHP warning.

Workaround

You can move the @testWith statement at the end of the comment section.

/**
 * @testdox This test should NOT make phpunit spit a PHP Warning anymore...
 * 
 * @testWith		 [1]
 * 			 [2]
 * 			 [3]
 */
public function testExample($arg): void
{
	$this->assertIsInt($arg);
}
@staabm
Copy link
Contributor

staabm commented Apr 6, 2024

will look into it

@sebastianbergmann
Copy link
Owner

sebastianbergmann commented Apr 6, 2024

Please note that bugs in PHPUnit 9 are no longer fixed. And while I agree that we should fix this for supported versions, please also note that annotations are deprecated and that you should use attributes instead sooner rather than later.

@staabm
Copy link
Contributor

staabm commented Apr 6, 2024

@fredericgboutin-yapla I tried reproducing the issue but on my machine the code sample does not emit warnings. see #5802 (also doesnt reproduce on CI).

are you sure he warnings are emitted by a plain phpunit setup and not because of some other thing? do you have a backtrace of the warning?

@fredericgboutin-yapla
Copy link
Author

fredericgboutin-yapla commented Apr 8, 2024

Hum, that's surprising.

I'm running PHP from the official Docker image using php.ini-development configuration.

> php vendor/bin/phpunit tests/ExampleTest.php

PHP Warning:  Uninitialized string offset 0 in /var/www/html/vendor/phpunit/phpunit/src/Metadata/Api/DataProvider.php on line 259

Warning: Uninitialized string offset 0 in /var/www/html/vendor/phpunit/phpunit/src/Metadata/Api/DataProvider.php on line 259
PHPUnit 10.5.17 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.14
Configuration: /var/www/html/phpunit.xml

...                                                                 3 / 3 (100%)

Time: 00:00.017, Memory: 6.00 MB

Example
 ✔ This test should NOT make phpunit spit a PHP Warning anymore... with data set 0
 ✔ This test should NOT make phpunit spit a PHP Warning anymore... with data set 1
 ✔ This test should NOT make phpunit spit a PHP Warning anymore... with data set 2

OK (3 tests, 3 assertions)
<?php

class ExampleTest extends PHPUnit\Framework\TestCase
{
    /**
     * @testWith [1]
     *           [2]
     *           [3]
     * 
     * @testdox This test should NOT make phpunit spit a PHP Warning anymore...
     */
    public function testExample($arg): void
    {
        $this->assertIsInt($arg);
    }
}

But sure, here is the backtrace JSON encoded for readability

[
    {
        "file": "\/var\/www\/html\/vendor\/bin\/phpunit",
        "line": 122,
        "args": [
            "\/var\/www\/html\/vendor\/phpunit\/phpunit\/phpunit"
        ],
        "function": "include"
    },
    {
        "file": "\/var\/www\/html\/vendor\/phpunit\/phpunit\/phpunit",
        "line": 104,
        "function": "run",
        "class": "PHPUnit\\TextUI\\Application",
        "object": {},
        "type": "->",
        "args": [
            [
                "vendor\/bin\/phpunit",
                "tests\/ExampleTest.php"
            ]
        ]
    },
    {
        "file": "\/var\/www\/html\/vendor\/phpunit\/phpunit\/src\/TextUI\/Application.php",
        "line": 108,
        "function": "buildTestSuite",
        "class": "PHPUnit\\TextUI\\Application",
        "object": {},
        "type": "->",
        "args": [
            {}
        ]
    },
    {
        "file": "\/var\/www\/html\/vendor\/phpunit\/phpunit\/src\/TextUI\/Application.php",
        "line": 349,
        "function": "build",
        "class": "PHPUnit\\TextUI\\Configuration\\TestSuiteBuilder",
        "object": {},
        "type": "->",
        "args": [
            {}
        ]
    },
    {
        "file": "\/var\/www\/html\/vendor\/phpunit\/phpunit\/src\/TextUI\/Configuration\/TestSuiteBuilder.php",
        "line": 55,
        "function": "testSuiteFromPath",
        "class": "PHPUnit\\TextUI\\Configuration\\TestSuiteBuilder",
        "object": {},
        "type": "->",
        "args": [
            "\/var\/www\/html\/tests\/ExampleTest.php",
            [
                "Test.php",
                ".phpt"
            ]
        ]
    },
    {
        "file": "\/var\/www\/html\/vendor\/phpunit\/phpunit\/src\/TextUI\/Configuration\/TestSuiteBuilder.php",
        "line": 119,
        "function": "fromClassReflector",
        "class": "PHPUnit\\Framework\\TestSuite",
        "type": "::",
        "args": [
            {
                "name": "ExampleTest"
            }
        ]
    },
    {
        "file": "\/var\/www\/html\/vendor\/phpunit\/phpunit\/src\/Framework\/TestSuite.php",
        "line": 134,
        "function": "addTestMethod",
        "class": "PHPUnit\\Framework\\TestSuite",
        "object": {},
        "type": "->",
        "args": [
            {
                "name": "ExampleTest"
            },
            {
                "name": "testExample",
                "class": "ExampleTest"
            }
        ]
    },
    {
        "file": "\/var\/www\/html\/vendor\/phpunit\/phpunit\/src\/Framework\/TestSuite.php",
        "line": 486,
        "function": "build",
        "class": "PHPUnit\\Framework\\TestBuilder",
        "object": {},
        "type": "->",
        "args": [
            {
                "name": "ExampleTest"
            },
            "testExample"
        ]
    },
    {
        "file": "\/var\/www\/html\/vendor\/phpunit\/phpunit\/src\/Framework\/TestBuilder.php",
        "line": 38,
        "function": "providedData",
        "class": "PHPUnit\\Metadata\\Api\\DataProvider",
        "object": {},
        "type": "->",
        "args": [
            "ExampleTest",
            "testExample"
        ]
    },
    {
        "file": "\/var\/www\/html\/vendor\/phpunit\/phpunit\/src\/Metadata\/Api\/DataProvider.php",
        "line": 61,
        "function": "dataProvidedByTestWithAnnotation",
        "class": "PHPUnit\\Metadata\\Api\\DataProvider",
        "object": {},
        "type": "->",
        "args": [
            "ExampleTest",
            "testExample"
        ]
    },
    {
        "file": "\/var\/www\/html\/vendor\/phpunit\/phpunit\/src\/Metadata\/Api\/DataProvider.php",
        "line": 259,
        "function": "throwError",
        "args": [
            2,
            "Uninitialized string offset 0",
            "\/var\/www\/html\/vendor\/phpunit\/phpunit\/src\/Metadata\/Api\/DataProvider.php",
            259
        ]
    }
]

@sebastianbergmann sebastianbergmann changed the title Using @testWith might generate "PHP Warning: Uninitialized string offset 0" Using @testWith annotation may generate PHP Warning: Uninitialized string offset 0 Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants