如何断言结果是PHPUnit中的电子邮件?

I would like to be able to test that a result is an email (myMail@provider.com ..)

I have tried to test if it's string using this :

$this->assertInternalType('string', $myExpectedEmail);

But I think it's not very accurate ... I tried this :

$this->assertInternalType('email', $myExpectedEmail);

But I get this error:

Type specified for PHPUnit_Framework_Constraint_IsType <email> is not a valid type.

Any idea/best practice of how to test email format in PHPunit ?

You could use a regular expression for validate an email address. Something like:

    $this->assertRegExp('/^.+\@\S+\.\S+$/', $myExpectedEmail);

hope this help

If You want to check if result is the same email as expected (string comparison) You should use:

$this->assertSame($expectedEmailString, $emailToTest);

If You need to check if string is a valid email You can use:

$this->assertSame($emailToTest, filter_var($emailToTest, FILTER_VALIDATE_EMAIL));

http://php.net/manual/en/function.filter-var.php

http://php.net/manual/en/filter.filters.validate.php

Easy and clean solution Enjoy :)