-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #154. Signed-off-by: Sean Molenaar <[email protected]>
- Loading branch information
1 parent
6aa62f4
commit 2601d7b
Showing
1 changed file
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
class PhpunitAT95 < Formula | ||
desc "Programmer-oriented testing framework for PHP" | ||
homepage "https://phpunit.de" | ||
url "https://phar.phpunit.de/phpunit-9.5.28.phar" | ||
sha256 "d8b2b96468c0b66ceeb2c345ed14925248089fe76cb76c7b591ab99aa04d319a" | ||
license "BSD-3-Clause" | ||
|
||
livecheck do | ||
skip "static version" | ||
end | ||
|
||
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', | ||
) | ||
); | ||
} | ||
} | ||
} | ||
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('[email protected]') | ||
); | ||
} | ||
public function testCannotBeCreatedFromInvalidEmailAddress(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
Email::fromString('invalid'); | ||
} | ||
public function testCanBeUsedAsString(): void | ||
{ | ||
$this->assertEquals( | ||
'[email protected]', | ||
Email::fromString('[email protected]') | ||
); | ||
} | ||
} | ||
EOS | ||
assert_match(/^OK \(3 tests, 3 assertions\)$/, | ||
shell_output("#{bin}/phpunit --bootstrap src/autoload.php tests/EmailTest.php")) | ||
end | ||
end |