Skip to content

Commit

Permalink
Enhancement: Turn PHPT tests into PHPUnit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Feb 13, 2024
1 parent 9705aae commit 66dfe86
Show file tree
Hide file tree
Showing 15 changed files with 801 additions and 1,241 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"autoload-dev": {
"psr-4": {
"phpweb\\Test\\EndToEnd\\": "tests/EndToEnd/"
"phpweb\\Test\\": "tests/"
}
},
"config": {
Expand Down
54 changes: 54 additions & 0 deletions tests/Unit/CleanAntiSpamTest.php
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,
];
}
}
}
160 changes: 160 additions & 0 deletions tests/Unit/GenChallengeTest.php
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);
}
}
73 changes: 73 additions & 0 deletions tests/Unit/IsEmailableAddressTest.php
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,
];
}
}
}
Loading

0 comments on commit 66dfe86

Please sign in to comment.