下载地址: https://phar.phpunit.de/
File->Default Settings->Languages & Frameworks->PHP->Test Frameworks,点击+添加
项目下External Libraries右键选择Configure PHP Include Paths…
选择你刚才下载的phpunit.phar
src/Email.php
<?php
final class Email
{
private $email;
private function __construct($email)
{
$this->ensureIsValidEmail($email);
$this->email = $email;
}
public static function fromString($email)
{
return new self($email);
}
public function __toString()
{
return $this->email;
}
private function ensureIsValidEmail($email)
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException(
sprintf(
'"%s" is not a valid email address',
$email
)
);
}
}
}
tests/EmailTest.php
<?php
use PHPUnit\Framework\TestCase;
/**
* @covers Email
*/
final class EmailTest extends TestCase
{
public function testCanBeCreatedFromValidEmailAddress()
{
$this->assertInstanceOf(
Email::class,
Email::fromString('user@example.com')
);
}
public function testCannotBeCreatedFromInvalidEmailAddress()
{
$this->expectException(InvalidArgumentException::class);
Email::fromString('invalid');
}
public function testCanBeUsedAsString()
{
$this->assertEquals(
'user@example.com',
Email::fromString('user@example.com')
);
}
}
➜ phpunit --bootstrap src/Email.php tests/EmailTest
PHPUnit 6.5.0 by Sebastian Bergmann and contributors.
... 3 / 3 (100%)
Time: 70 ms, Memory: 10.00MB
OK (3 tests, 3 assertions)