-
Notifications
You must be signed in to change notification settings - Fork 554
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Turn PHPT tests into PHPUnit test cases
- Loading branch information
1 parent
9705aae
commit 66dfe86
Showing
15 changed files
with
801 additions
and
1,241 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
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,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpweb\Test\Unit; | ||
|
||
use PHPUnit\Framework; | ||
|
||
#[Framework\Attributes\CoversFunction('clean_AntiSPAM')] | ||
final class CleanAntiSpamTest extends Framework\TestCase | ||
{ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
require_once __DIR__ . '/../../include/email-validation.inc'; | ||
} | ||
|
||
#[Framework\Attributes\DataProvider('provideEmailAndExpectedEmail')] | ||
public function testCleanAntiSpamReturnsCleanedEmail( | ||
string $email, | ||
string $expectedEmail, | ||
): void | ||
{ | ||
$cleanedEmail = clean_AntiSPAM($email); | ||
|
||
self::assertSame($expectedEmail, $cleanedEmail); | ||
} | ||
|
||
/** | ||
* @return \Generator<string, array{0: string, 1: string}> | ||
*/ | ||
public static function provideEmailAndExpectedEmail(): \Generator | ||
{ | ||
$values = [ | ||
'asasasd324324@php.net' => 'asasasd324324@php.net', | ||
'jcastagnetto-delete-this-@yahoo.com' => 'jcastagnetto@yahoo.com', | ||
'jcastagnetto-i-hate-spam@NOSPAMyahoo.com' => 'jcastagnetto@yahoo.com', | ||
'jcastagnetto-NO-SPAM@yahoo.com' => 'jcastagnetto@yahoo.com', | ||
'jcastagnetto@NoSpam-yahoo.com' => 'jcastagnetto@yahoo.com', | ||
'jesusmc@scripps.edu' => 'jesusmc@scripps.edu', | ||
'jmcastagnetto@chek2.com' => 'jmcastagnetto@chek2.com', | ||
'jmcastagnetto@yahoo.com' => 'jmcastagnetto@yahoo.com', | ||
'some-wrong@asdas.com' => 'some-wrong@asdas.com', | ||
'wrong-address-with@@@@-remove_me-and-some-i-hate_SPAM-stuff' => 'wrong-address-with@@@@and-somestuff', | ||
'wrong-email-address@lists.php.net' => 'wrong-email-address@lists.php.net', | ||
]; | ||
|
||
foreach ($values as $email => $expectedEmail) { | ||
yield $email => [ | ||
$email, | ||
$expectedEmail, | ||
]; | ||
} | ||
} | ||
} |
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,160 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpweb\Test\Unit; | ||
|
||
use PHPUnit\Framework; | ||
|
||
#[Framework\Attributes\CoversFunction('gen_challenge')] | ||
#[Framework\Attributes\UsesFunction('gen_minus')] | ||
#[Framework\Attributes\UsesFunction('gen_plus')] | ||
#[Framework\Attributes\UsesFunction('print_infix')] | ||
#[Framework\Attributes\UsesFunction('print_prefix')] | ||
final class GenChallengeTest extends Framework\TestCase | ||
{ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
require_once __DIR__ . '/../../manual/spam_challenge.php'; | ||
|
||
mt_srand(9001); | ||
} | ||
|
||
public function testGenChallengeReturnsChallenge(): void { | ||
$challenges = array_map(static function (): array { | ||
[$function, $argumentOne, $argumentTwo, $question] = gen_challenge(); | ||
|
||
return [ | ||
'function' => $function, | ||
'argumentOne' => $argumentOne, | ||
'argumentTwo' => $argumentTwo, | ||
'question' => $question, | ||
]; | ||
}, range(1, 20)); | ||
|
||
$expected = [ | ||
[ | ||
'function' => 'min', | ||
'argumentOne' => 'two', | ||
'argumentTwo' => 'one', | ||
'question' => 'min(two, one)', | ||
], | ||
[ | ||
'function' => 'minus', | ||
'argumentOne' => 'five', | ||
'argumentTwo' => 'five', | ||
'question' => 'five minus five', | ||
], | ||
[ | ||
'function' => 'minus', | ||
'argumentOne' => 'four', | ||
'argumentTwo' => 'four', | ||
'question' => 'four minus four', | ||
], | ||
[ | ||
'function' => 'min', | ||
'argumentOne' => 'nine', | ||
'argumentTwo' => 'seven', | ||
'question' => 'min(nine, seven)', | ||
], | ||
[ | ||
'function' => 'minus', | ||
'argumentOne' => 'seven', | ||
'argumentTwo' => 'six', | ||
'question' => 'seven minus six', | ||
], | ||
[ | ||
'function' => 'max', | ||
'argumentOne' => 'three', | ||
'argumentTwo' => 'six', | ||
'question' => 'max(three, six)', | ||
], | ||
[ | ||
'function' => 'max', | ||
'argumentOne' => 'six', | ||
'argumentTwo' => 'five', | ||
'question' => 'max(six, five)', | ||
], | ||
[ | ||
'function' => 'max', | ||
'argumentOne' => 'four', | ||
'argumentTwo' => 'three', | ||
'question' => 'max(four, three)', | ||
], | ||
[ | ||
'function' => 'min', | ||
'argumentOne' => 'two', | ||
'argumentTwo' => 'nine', | ||
'question' => 'min(two, nine)', | ||
], | ||
[ | ||
'function' => 'plus', | ||
'argumentOne' => 'eight', | ||
'argumentTwo' => 'one', | ||
'question' => 'eight plus one', | ||
], | ||
[ | ||
'function' => 'plus', | ||
'argumentOne' => 'three', | ||
'argumentTwo' => 'five', | ||
'question' => 'three plus five', | ||
], | ||
[ | ||
'function' => 'min', | ||
'argumentOne' => 'eight', | ||
'argumentTwo' => 'three', | ||
'question' => 'min(eight, three)', | ||
], | ||
[ | ||
'function' => 'max', | ||
'argumentOne' => 'zero', | ||
'argumentTwo' => 'nine', | ||
'question' => 'max(zero, nine)', | ||
], | ||
[ | ||
'function' => 'min', | ||
'argumentOne' => 'five', | ||
'argumentTwo' => 'nine', | ||
'question' => 'min(five, nine)', | ||
], | ||
[ | ||
'function' => 'minus', | ||
'argumentOne' => 'six', | ||
'argumentTwo' => 'four', | ||
'question' => 'six minus four', | ||
], | ||
[ | ||
'function' => 'max', | ||
'argumentOne' => 'one', | ||
'argumentTwo' => 'one', | ||
'question' => 'max(one, one)', | ||
], | ||
[ | ||
'function' => 'plus', | ||
'argumentOne' => 'five', | ||
'argumentTwo' => 'zero', | ||
'question' => 'five plus zero', | ||
], | ||
[ | ||
'function' => 'minus', | ||
'argumentOne' => 'nine', | ||
'argumentTwo' => 'eight', | ||
'question' => 'nine minus eight', | ||
], | ||
[ | ||
'function' => 'minus', | ||
'argumentOne' => 'three', | ||
'argumentTwo' => 'one', | ||
'question' => 'three minus one', | ||
], | ||
[ | ||
'function' => 'min', | ||
'argumentOne' => 'three', | ||
'argumentTwo' => 'one', | ||
'question' => 'min(three, one)', | ||
], | ||
]; | ||
|
||
self::assertSame($expected, $challenges); | ||
} | ||
} |
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,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpweb\Test\Unit; | ||
|
||
use PHPUnit\Framework; | ||
|
||
#[Framework\Attributes\CoversFunction('is_emailable_address')] | ||
final class IsEmailableAddressTest extends Framework\TestCase | ||
{ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
require_once __DIR__ . '/../../include/email-validation.inc'; | ||
} | ||
|
||
#[Framework\Attributes\DataProvider('provideInvalidEmail')] | ||
public function testIsEmailableAddressReturnsFalseWhenEmailIsInvalid(string $email): void | ||
{ | ||
$isEmailableAddress = is_emailable_address($email); | ||
|
||
self::assertFalse($isEmailableAddress); | ||
} | ||
|
||
/** | ||
* @return \Generator<string, array{0: string}> | ||
*/ | ||
public static function provideInvalidEmail(): \Generator | ||
{ | ||
$values = [ | ||
'jcastagnetto-i-hate-spam@NOSPAMyahoo.test', | ||
'jcastagnetto@NoSpam-yahoo.com', | ||
'jmcastagnetto@chek2.com', | ||
'wrong-address-with@@@@-remove_me-and-some-i-hate_SPAM-stuff', | ||
'wrong-email-address@lists.php.net', | ||
]; | ||
|
||
foreach ($values as $value) { | ||
yield $value => [ | ||
$value, | ||
]; | ||
} | ||
} | ||
|
||
#[Framework\Attributes\DataProvider('provideValidEmail')] | ||
public function testIsEmailableAddressReturnsTrueWhenEmailIsValid(string $email): void | ||
{ | ||
$isEmailableAddress = is_emailable_address($email); | ||
|
||
self::assertTrue($isEmailableAddress); | ||
} | ||
|
||
/** | ||
* @return \Generator<string, array{0: string}> | ||
*/ | ||
public static function provideValidEmail(): \Generator | ||
{ | ||
$values = [ | ||
'asasasd324324@php.net', | ||
'jcastagnetto-delete-this-@yahoo.com', | ||
'jcastagnetto-NO-SPAM@yahoo.com', | ||
'jesusmc@scripps.edu', | ||
'jmcastagnetto@yahoo.com', | ||
'some-wrong@asdas.com', | ||
]; | ||
|
||
foreach ($values as $value) { | ||
yield $value => [ | ||
$value, | ||
]; | ||
} | ||
} | ||
} |
Oops, something went wrong.