7b0008e87e
Closes #37962. Signed-off-by: Chongyu Zhu <i@lembacon.com>
110 lines
2.9 KiB
Ruby
110 lines
2.9 KiB
Ruby
class Phpunit < Formula
|
|
desc "Programmer-oriented testing framework for PHP"
|
|
homepage "https://phpunit.de"
|
|
url "https://phar.phpunit.de/phpunit-8.0.5.phar"
|
|
sha256 "3766a898b9cbb904d75554ae902ab207e6ee2fc788d5dfcdc31f5b9d2a2249d0"
|
|
|
|
bottle :unneeded
|
|
|
|
depends_on "php" => :test
|
|
|
|
def install
|
|
bin.install "phpunit-#{version}.phar" => "phpunit"
|
|
end
|
|
|
|
test do
|
|
(testpath/"src/autoload.php").write <<~EOS
|
|
<?php
|
|
spl_autoload_register(
|
|
function($class) {
|
|
static $classes = null;
|
|
if ($classes === null) {
|
|
$classes = array(
|
|
'email' => '/Email.php'
|
|
);
|
|
}
|
|
$cn = strtolower($class);
|
|
if (isset($classes[$cn])) {
|
|
require __DIR__ . $classes[$cn];
|
|
}
|
|
},
|
|
true,
|
|
false
|
|
);
|
|
EOS
|
|
|
|
(testpath/"src/Email.php").write <<~EOS
|
|
<?php
|
|
declare(strict_types=1);
|
|
|
|
final class Email
|
|
{
|
|
private $email;
|
|
|
|
private function __construct(string $email)
|
|
{
|
|
$this->ensureIsValidEmail($email);
|
|
|
|
$this->email = $email;
|
|
}
|
|
|
|
public static function fromString(string $email): self
|
|
{
|
|
return new self($email);
|
|
}
|
|
|
|
public function __toString(): string
|
|
{
|
|
return $this->email;
|
|
}
|
|
|
|
private function ensureIsValidEmail(string $email): void
|
|
{
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
throw new InvalidArgumentException(
|
|
sprintf(
|
|
'"%s" is not a valid email address',
|
|
$email
|
|
)
|
|
);
|
|
}
|
|
}
|
|
}
|
|
EOS
|
|
|
|
(testpath/"tests/EmailTest.php").write <<~EOS
|
|
<?php
|
|
declare(strict_types=1);
|
|
|
|
use PHPUnit\\Framework\\TestCase;
|
|
|
|
final class EmailTest extends TestCase
|
|
{
|
|
public function testCanBeCreatedFromValidEmailAddress(): void
|
|
{
|
|
$this->assertInstanceOf(
|
|
Email::class,
|
|
Email::fromString('user@example.com')
|
|
);
|
|
}
|
|
|
|
public function testCannotBeCreatedFromInvalidEmailAddress(): void
|
|
{
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
Email::fromString('invalid');
|
|
}
|
|
|
|
public function testCanBeUsedAsString(): void
|
|
{
|
|
$this->assertEquals(
|
|
'user@example.com',
|
|
Email::fromString('user@example.com')
|
|
);
|
|
}
|
|
}
|
|
|
|
EOS
|
|
assert_match /^OK \(3 tests, 3 assertions\)$/, shell_output("#{bin}/phpunit --bootstrap src/autoload.php tests/EmailTest")
|
|
end
|
|
end
|