sebastianbergmann / phpunit

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

str_repeat(): Second argument has to be greater than or equal to 0 #1976

Closed jhmnieuwenhuis closed 7 years ago

jhmnieuwenhuis commented 8 years ago

When I run phpunit version 5.0.10 on a symfony 2.8 project running on centos 7 with php7-Rc7

I get the error: str_repeat(): Second argument has to be greater than or equal to 0.

I can see that the test has run, but I am unable to see where the error occurs....

output

$ ./phpunitdir/phpunit.phar --debug -v -c app PHPUnit 5.0.10 by Sebastian Bergmann and contributors.

Runtime: PHP 7.0.0RC7 with Xdebug 2.4.0rc1 Configuration: /var/www/html/symblogudm/app/phpunit.xml.dist

Starting test 'Blog\CoreBundle\Tests\Controller\PostControllerTest::testIndex'. Fstr_repeat(): Second argument has to be greater than or equal to 0

Regards,

Hans Nieuwenhuis

sebastianbergmann commented 8 years ago

Can you provide a minimal, self-contained, reproducing test case?

jhmnieuwenhuis commented 8 years ago

I noticed that is seems to go wrong on displaying the results. The error is even shown if there are no tests to be executed.

As a workarount I am now using the --tap option

phpunit -c app --tap

Then I get no error

sebastianbergmann commented 8 years ago

The only place in PHPUnit's code where str_repeat() is used is https://github.com/sebastianbergmann/phpunit/blob/5.0/src/TextUI/ResultPrinter.php#L555. And I fail to see how $this->maxColumn - $this->column could be negative.

jhmnieuwenhuis commented 8 years ago

Me too, but somehow it does. I suppose the --tap option causes the ResultPrinter.php not to be used.

May be it is caused by the php7 Rc7 prerelease ??

I'll test again as soon php7 is officially released and the i'll report back to You.

Thanks !

sebastianbergmann commented 8 years ago

Can you share the contents of /var/www/html/symblogudm/app/phpunit.xml.dist?

jhmnieuwenhuis commented 8 years ago

<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="autoload.php"

<php>
    <ini name="error_reporting" value="-1" />
</php>
<testsuites>
    <testsuite name="Project Test Suite">
        <directory>../src/*/*Bundle/Tests</directory>
        <directory>../src/*/Bundle/*Bundle/Tests</directory>
        <directory>../src/*Bundle/Tests</directory>
    </testsuite>
</testsuites>

<!--
<php>
    <server name="KERNEL_DIR" value="/path/to/your/app/" />
</php>
-->

<filter>
    <whitelist>
        <directory>../src</directory>
        <exclude>
            <directory>../src/*Bundle/Resources</directory>
            <directory>../src/*Bundle/Tests</directory>
            <directory>../src/*/*Bundle/Resources</directory>
            <directory>../src/*/*Bundle/Tests</directory>
            <directory>../src/*/Bundle/*Bundle/Resources</directory>
            <directory>../src/*/Bundle/*Bundle/Tests</directory>
        </exclude>
    </whitelist>
</filter>

sebastianbergmann commented 8 years ago

Thank you for sharing your configuration. I do not see anything in there that would explain the problem.

Are you sure that this does not come from a usage of str_repeat() in your code or code that your code uses?

jhmnieuwenhuis commented 8 years ago

I do not think so. It is a standard symfony 2.8 installation and there is only one simple test. And the error also shows if I comment the code inside the testIndex function....

<?php namespace Blog\CoreBundle\Tests\Controller; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

/**

jessedc commented 8 years ago

I am seeing this issue appearing at the end of my test suite whilst running in a BuildKite environment on an unbuntu machine.

It appears to me that Console is returning 0 for getNumberOfColumns() within my particular environment.

As you can see below if console returns 0 for $maxNumberOfColumns the default value of 80 will be overwritten (see source).

$console  = new Console;
$maxNumberOfColumns = $console->getNumberOfColumns();

if ($numberOfColumns == 'max' || $numberOfColumns > $maxNumberOfColumns) {
    $numberOfColumns = $maxNumberOfColumns;
}

The consequence of this is the $this->maxColumn value ends up being a negative number as it is setup in startTestSuite() and is based off numberOfColumns.

This causes the second parameter of str_repeat() to be negative in this case.

The console class is using stty size and stty to check for an non interactive shell column width and I suspect the regex is matching the 0 0 value returned by my environment.

For what it's worth the output of my test runs within the CI environment don't contain the nice line endings you'd expect from an 80 character $numberOfColumns value either.

vendor/bin/phpunit -d memory_limit=1024M --configuration phpunit.xml --verbose
PHPUnit 5.1.3 by Sebastian Bergmann and contributors.

Runtime:       PHP 5.6.16-2+deb.sury.org~trusty+1
Configuration: /var/lib/buildkite-agent/builds/api/phpunit.xml

.......................................................SIIIIIIII........................................................III.....................................................................................................S..........III....................................................................................................................................................

  [ErrorException]                                                    
  str_repeat(): Second argument has to be greater than or equal to 0 

The stack trace of the error is reported in my logs as follows:

[2015-12-21 23:20:21] lumen.ERROR: exception 'ErrorException' with message 'str_repeat(): Second argument has to be greater than or equal to 0' in /vendor/phpunit/phpunit/src/TextUI/ResultPrinter.php:603
Stack trace:
#0 [internal function]: Laravel\Lumen\Application->Laravel\Lumen\{closure}(2, 'str_repeat(): S...', '/var/lib/buildk...', 603, Array)
#1 /vendor/phpunit/phpunit/src/TextUI/ResultPrinter.php(603): str_repeat(' ', -403)
#2 /vendor/phpunit/phpunit/src/TextUI/ResultPrinter.php(572): PHPUnit_TextUI_ResultPrinter->writeProgress('.')
#3 /vendor/phpunit/phpunit/src/Framework/TestResult.php(391): PHPUnit_TextUI_ResultPrinter->endTest(Object(GeocodeValidatorTest), 5.5074691772461E-05)
#4 /vendor/phpunit/phpunit/src/Framework/TestResult.php(833): PHPUnit_Framework_TestResult->endTest(Object(GeocodeValidatorTest), 5.5074691772461E-05)
#5 /vendor/phpunit/phpunit/src/Framework/TestCase.php(726): PHPUnit_Framework_TestResult->run(Object(GeocodeValidatorTest))
#6 /vendor/phpunit/phpunit/src/Framework/TestSuite.php(747): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#7 /vendor/phpunit/phpunit/src/Framework/TestSuite.php(747): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#8 /vendor/phpunit/phpunit/src/Framework/TestSuite.php(747): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#9 /vendor/phpunit/phpunit/src/TextUI/TestRunner.php(429): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#10 vendor/phpunit/phpunit/src/TextUI/Command.php(155): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#11 vendor/phpunit/phpunit/src/TextUI/Command.php(106): PHPUnit_TextUI_Command->run(Array, true)
#12 vendor/phpunit/phpunit/phpunit(47): PHPUnit_TextUI_Command::main()

My work around

I am yet to investigate why the buildkite tty reports a zero column width but I was able to solve the issue by setting the tty column width manually at the appropriate point in my setup scripts.

stty cols 80

Preventing the Issue

It might be worth preventing Console from returning a zero value in the regex cases.

julieproductops commented 8 years ago

Thank you jessedc, that post saved me a lot of time. I am adding in my vote for your suggested change. This problem (where str_repeat()'s 2nd argument was a negative value) also occurs when attempting to execute phpunit on a remote host via Ansible, which uses SSH, so I'm providing diagnosis and workaround to other Ansible/phpunit users:

I used Ansible's debug feature to reveal its stty settings as follows:

- name: Show stty settings shell: stty -a register: stty

- name: printstuff debug: var=stty

Truncated output from above reveals rows = 0:

"stdout_lines": [ "speed 9600 baud; rows 0; columns 0; line = 0;",

Adding a simple command to my ansible task as follows fixed the problem:

name: Run integration test suite shell: stty cols 80; sudo vendor/phpunit/phpunit/phpunit --verbose --configuration app/ --log-junit=MyTestResults.xml /MyBundle/Tests/Integration

SebastienTainon commented 8 years ago

The same issue happens on Scrutinizer:

PHP Fatal error:  Uncaught exception 'Symfony\Component\Debug\Exception\ContextErrorException'  with message 'Warning: str_repeat(): Second argument has to be greater than or equal to 0' in /home/scrutinizer/build/vendor/phpunit/phpunit/src/TextUI/ResultPrinter.php:600

Running stty cols 80; vendor/phpunit/phpunit/phpunit -c app fixes the issue.

It would be good to check the shell columns to avoid this error though.

sebastianbergmann commented 8 years ago

Can you please check whether https://github.com/sebastianbergmann/environment/commit/22aa49baa48886f40b060e061a7967436f44a249 fixes the issue? Thanks!

SebastienTainon commented 8 years ago

I've just tried with "sebastian/environment": "dev-master#22aa49baa48886f40b060e061a7967436f44a249", and sending only vendor/phpunit/phpunit/phpunit -c app without specifying the stty columns to Scrutinizer works great! Thanks :)

archergod commented 8 years ago

I am getting this error on my Test Case, when I use it in Docker image that I created.

PHP Warning: str_repeat(): Second argument has to be greater than or equal to 0 in /usr/share/php/PHPUnit/TextUI/ResultPrinter.php on line 600

Docker Image is : https://hub.docker.com/r/archergod/phpall/

Basically the docker image is ubuntu docker, that I add PHP 7, mysql and PHPunit using apt-get install

Though on same test on my local Windows machines, with PHPUnit 5.6 with PHP 7 it works without any warning.

Ocramius commented 7 years ago

Got this on https://travis-ci.org/Ocramius/ProxyManager/jobs/177276352#L574

Possibly related to one of these dependencies being downgraded when dealing with the DEPENDENCIES="low" test scenario: https://travis-ci.org/Ocramius/ProxyManager/jobs/177276352#L476-L526

Latest dependencies don't cause the issue

sebastianbergmann commented 7 years ago

@Ocramius Any suggestion on what I should do here?

Ocramius commented 7 years ago

Probably bumping dependencies

On 19 Nov 2016 5:44 p.m., "Sebastian Bergmann" notifications@github.com wrote:

@Ocramius https://github.com/Ocramius Any suggestion on what I should do here?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/sebastianbergmann/phpunit/issues/1976#issuecomment-261724505, or mute the thread https://github.com/notifications/unsubscribe-auth/AAJakP4lCKjw7AJwC9AuHdQGerb3V5dJks5q_ydzgaJpZM4GrdUx .

SelormBruce commented 5 years ago

follow the link and change -1 to 0