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

DataProviders get executed beforehand #3046

Closed
healerz opened this issue Mar 22, 2018 · 1 comment
Closed

DataProviders get executed beforehand #3046

healerz opened this issue Mar 22, 2018 · 1 comment

Comments

@healerz
Copy link

healerz commented Mar 22, 2018

Q A
PHPUnit version 7.0.2
PHP version 7.1.13
Installation Method Composer

DataProviders get called before the execution of the first test, which leads to issues if we wanna check for a tight timespan.

To illustrate the issue:

class TimespanTest extends TestCase
{
    public function testWaitFor1Minute(): void
    {
        sleep(60);
    }

    /**
     * @dataProvider timespanProvider
     */
    public function testIsInTimespan(string $before, string $after): void
    {
        $now = (new \DateTime())->format('Y-m-d H:i:s');

        self::assertLessThan($now, $before);
        self::assertGreaterThan($now, $after);
    }

    public function timespanProvider(): array
    {
        $format = "Y-m-d H:i:s";
        $oneMinuteBeforeNow = (new \DateTime())->sub(new \DateInterval('PT1M'))->format($format);
        $oneMinuteAfterNow = (new \DateTime())->add(new \DateInterval('PT1M'))->format($format);

        return [
            [$oneMinuteBeforeNow, $oneMinuteAfterNow]
        ];
    } 
}

In my opinion this should not result in a failing test. And i do not see any benefit in executing all the data providers of the whole project beforehand.

@sebastianbergmann
Copy link
Owner

Duplicate of #10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants