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

@runClassInSeparateProcess fails for tests with a @dataProvider #2830

Closed
KintradimCrux opened this issue Oct 25, 2017 · 5 comments · Fixed by elecena/nano#2
Closed

@runClassInSeparateProcess fails for tests with a @dataProvider #2830

KintradimCrux opened this issue Oct 25, 2017 · 5 comments · Fixed by elecena/nano#2

Comments

@KintradimCrux
Copy link

Q A
PHPUnit version 6.2.2
PHP version 7.1.6
Installation Method Composer

The template in vendor/phpunit/phpunit/src/Util/PHP/Template/TestCaseClass.tpl.dist is creating an instance of the test class with a NULL name instead of using TestSuite for that. The result of running this test is an Exception with message "PHPUnit\Framework\TestCase::$name must not be null" when the runTest() method of the test class is called.

To detect the presence of the isolated environment, i use an autoload.php which sets an environment variable containing the main process PID, if it does not exist yet.

autoload.php:

<?php
$loader = require __DIR__.'/vendor/autoload.php';
if (false === getenv('_MAINPID')) {
    putenv('_MAINPID=' . posix_getpid());
}
return $loader;

RunClassInSeparateProcessTest.php:

<?php
use PHPUnit\Framework\TestCase;
/**
 * @runClassInSeparateProcess
 */
class RunClassInSeparateProcessTest extends TestCase {
    public function nullDataProvider(){
        return array(array(null, null, null));
    }
    /**
     * @dataProvider nullDataProvider
     */
    public function test_with_dataProvider(){
        $this->assertNotEquals((int) getenv('_MAINPID'), posix_getpid());
    }
}
@mihailogajic
Copy link
Contributor

@KintradimCrux did you try to use @runTestsInSeparateProcesses annotation instead of
@ runClassInSeparateProcess ?

@KintradimCrux
Copy link
Author

@mihailogajic thats not the question.

While writing tests for a project i found that @ runClassInSeparateProcess is not running tests without a data provider in a separate process because of missing initialization (see #2724) and also is unable to run tests with a data provider in a separate process because of an exception.

I generally like the idea of running all test cases within a class in the same separate process, where @ beforeClass is run in the separate process once before starting all the tests, and @ afterClass is run in the separate process once after all tests have finished, so i'd like this annotation to work as it is supposed to.

@sebastianbergmann
Copy link
Owner

@KintradimCrux: Does #3082 solve your problem?

@KintradimCrux and @mihailogajic: Can you provide a minimal, self-contained, reproducing test case that shows the problem that #3082 is supposed to solve? I do not understand the original example given above with all its PID "magic".

@IanEmerson20
Copy link

public function inviteUserToOrganization(Organizations $organization, string $email_address)
{
$html_message =$this->templating_service->render('mails/allowed_login_notification.html.twig', [
'org_obj' => $organization,
'email_address' => $email_address
]);
$subject = "Welcome to {$organization->getDisplayName()}";
$this->notification_service->notifyRecipient($html_message, $subject, $email_address);

	$user = $this->user_model->findOneBy([ 'email' => $email_address]);

    // die(var_dump(!$user));
    if(!$user) {
		$user = $this->user_model->createUserFromEmailAddress($email_address);
	}

	return $user;
}

How can I do this one

@KintradimCrux
Copy link
Author

@sebastianbergmann It's not "magic", it's setting an environment variable with the PID of the main process to see if the isolated environment runs under the same PID, which it shouldn't. To tell if #3082 fixes the problem, i need to check this first, since all i can see on Codecov is "No pull request found".

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

Successfully merging a pull request may close this issue.

4 participants