|
| 1 | +class PhpunitAT95 < Formula |
| 2 | + desc "Programmer-oriented testing framework for PHP" |
| 3 | + homepage "https://phpunit.de" |
| 4 | + url "https://phar.phpunit.de/phpunit-9.5.28.phar" |
| 5 | + sha256 "d8b2b96468c0b66ceeb2c345ed14925248089fe76cb76c7b591ab99aa04d319a" |
| 6 | + license "BSD-3-Clause" |
| 7 | + |
| 8 | + livecheck do |
| 9 | + skip "static version" |
| 10 | + end |
| 11 | + |
| 12 | + depends_on "php" => :test |
| 13 | + |
| 14 | + def install |
| 15 | + bin.install "phpunit-#{version}.phar" => "phpunit" |
| 16 | + end |
| 17 | + |
| 18 | + test do |
| 19 | + (testpath/"src/autoload.php").write <<~EOS |
| 20 | + <?php |
| 21 | + spl_autoload_register( |
| 22 | + function($class) { |
| 23 | + static $classes = null; |
| 24 | + if ($classes === null) { |
| 25 | + $classes = array( |
| 26 | + 'email' => '/Email.php' |
| 27 | + ); |
| 28 | + } |
| 29 | + $cn = strtolower($class); |
| 30 | + if (isset($classes[$cn])) { |
| 31 | + require __DIR__ . $classes[$cn]; |
| 32 | + } |
| 33 | + }, |
| 34 | + true, |
| 35 | + false |
| 36 | + ); |
| 37 | + EOS |
| 38 | + |
| 39 | + (testpath/"src/Email.php").write <<~EOS |
| 40 | + <?php |
| 41 | + declare(strict_types=1); |
| 42 | +
|
| 43 | + final class Email |
| 44 | + { |
| 45 | + private $email; |
| 46 | +
|
| 47 | + private function __construct(string $email) |
| 48 | + { |
| 49 | + $this->ensureIsValidEmail($email); |
| 50 | +
|
| 51 | + $this->email = $email; |
| 52 | + } |
| 53 | +
|
| 54 | + public static function fromString(string $email): self |
| 55 | + { |
| 56 | + return new self($email); |
| 57 | + } |
| 58 | +
|
| 59 | + public function __toString(): string |
| 60 | + { |
| 61 | + return $this->email; |
| 62 | + } |
| 63 | +
|
| 64 | + private function ensureIsValidEmail(string $email): void |
| 65 | + { |
| 66 | + if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { |
| 67 | + throw new InvalidArgumentException( |
| 68 | + sprintf( |
| 69 | + '"%s" is not a valid email address', |
| 70 | + $email |
| 71 | + ) |
| 72 | + ); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + EOS |
| 77 | + |
| 78 | + (testpath/"tests/EmailTest.php").write <<~EOS |
| 79 | + <?php |
| 80 | + declare(strict_types=1); |
| 81 | +
|
| 82 | + use PHPUnit\\Framework\\TestCase; |
| 83 | +
|
| 84 | + final class EmailTest extends TestCase |
| 85 | + { |
| 86 | + public function testCanBeCreatedFromValidEmailAddress(): void |
| 87 | + { |
| 88 | + $this->assertInstanceOf( |
| 89 | + Email::class, |
| 90 | + Email::fromString('[email protected]') |
| 91 | + ); |
| 92 | + } |
| 93 | +
|
| 94 | + public function testCannotBeCreatedFromInvalidEmailAddress(): void |
| 95 | + { |
| 96 | + $this->expectException(InvalidArgumentException::class); |
| 97 | +
|
| 98 | + Email::fromString('invalid'); |
| 99 | + } |
| 100 | +
|
| 101 | + public function testCanBeUsedAsString(): void |
| 102 | + { |
| 103 | + $this->assertEquals( |
| 104 | + |
| 105 | + Email::fromString('[email protected]') |
| 106 | + ); |
| 107 | + } |
| 108 | + } |
| 109 | +
|
| 110 | + EOS |
| 111 | + assert_match(/^OK \(3 tests, 3 assertions\)$/, |
| 112 | + shell_output("#{bin}/phpunit --bootstrap src/autoload.php tests/EmailTest.php")) |
| 113 | + end |
| 114 | +end |
0 commit comments