Skip to content

Commit 06a2d11

Browse files
committed
Enhancement: Turn PHPT tests into PHPUnit test cases
1 parent 9705aae commit 06a2d11

15 files changed

+801
-1241
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
"autoload-dev": {
2424
"psr-4": {
25-
"phpweb\\Test\\EndToEnd\\": "tests/EndToEnd/"
25+
"phpweb\\Test\\": "tests/"
2626
}
2727
},
2828
"config": {

tests/Unit/CleanAntiSpamTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpweb\Test\Unit;
6+
7+
use PHPUnit\Framework;
8+
9+
#[Framework\Attributes\CoversFunction('clean_AntiSPAM')]
10+
final class CleanAntiSpamTest extends Framework\TestCase
11+
{
12+
public static function setUpBeforeClass(): void
13+
{
14+
require_once __DIR__ . '/../../include/email-validation.inc';
15+
}
16+
17+
#[Framework\Attributes\DataProvider('provideEmailAndExpectedEmail')]
18+
public function testCleanAntiSpamReturnsCleanedEmail(
19+
string $email,
20+
string $expectedEmail,
21+
): void
22+
{
23+
$cleanedEmail = clean_AntiSPAM($email);
24+
25+
self::assertSame($expectedEmail, $cleanedEmail);
26+
}
27+
28+
/**
29+
* @return \Generator<string, array{0: string, 1: string}>
30+
*/
31+
public static function provideEmailAndExpectedEmail(): \Generator
32+
{
33+
$values = [
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
'wrong-address-with@@@@-remove_me-and-some-i-hate_SPAM-stuff' => 'wrong-address-with@@@@and-somestuff',
44+
45+
];
46+
47+
foreach ($values as $email => $expectedEmail) {
48+
yield $email => [
49+
$email,
50+
$expectedEmail,
51+
];
52+
}
53+
}
54+
}

tests/Unit/GenChallengeTest.php

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpweb\Test\Unit;
6+
7+
use PHPUnit\Framework;
8+
9+
#[Framework\Attributes\CoversFunction('gen_challenge')]
10+
#[Framework\Attributes\UsesFunction('gen_minus')]
11+
#[Framework\Attributes\UsesFunction('gen_plus')]
12+
#[Framework\Attributes\UsesFunction('print_infix')]
13+
#[Framework\Attributes\UsesFunction('print_prefix')]
14+
final class GenChallengeTest extends Framework\TestCase
15+
{
16+
public static function setUpBeforeClass(): void
17+
{
18+
require_once __DIR__ . '/../../manual/spam_challenge.php';
19+
20+
mt_srand(9001);
21+
}
22+
23+
public function testGenChallengeReturnsChallenge(): void {
24+
$challenges = array_map(static function (): array {
25+
[$function, $argumentOne, $argumentTwo, $question] = gen_challenge();
26+
27+
return [
28+
'function' => $function,
29+
'argumentOne' => $argumentOne,
30+
'argumentTwo' => $argumentTwo,
31+
'question' => $question,
32+
];
33+
}, range(1, 20));
34+
35+
$expected = [
36+
[
37+
'function' => 'min',
38+
'argumentOne' => 'two',
39+
'argumentTwo' => 'one',
40+
'question' => 'min(two, one)',
41+
],
42+
[
43+
'function' => 'minus',
44+
'argumentOne' => 'five',
45+
'argumentTwo' => 'five',
46+
'question' => 'five minus five',
47+
],
48+
[
49+
'function' => 'minus',
50+
'argumentOne' => 'four',
51+
'argumentTwo' => 'four',
52+
'question' => 'four minus four',
53+
],
54+
[
55+
'function' => 'min',
56+
'argumentOne' => 'nine',
57+
'argumentTwo' => 'seven',
58+
'question' => 'min(nine, seven)',
59+
],
60+
[
61+
'function' => 'minus',
62+
'argumentOne' => 'seven',
63+
'argumentTwo' => 'six',
64+
'question' => 'seven minus six',
65+
],
66+
[
67+
'function' => 'max',
68+
'argumentOne' => 'three',
69+
'argumentTwo' => 'six',
70+
'question' => 'max(three, six)',
71+
],
72+
[
73+
'function' => 'max',
74+
'argumentOne' => 'six',
75+
'argumentTwo' => 'five',
76+
'question' => 'max(six, five)',
77+
],
78+
[
79+
'function' => 'max',
80+
'argumentOne' => 'four',
81+
'argumentTwo' => 'three',
82+
'question' => 'max(four, three)',
83+
],
84+
[
85+
'function' => 'min',
86+
'argumentOne' => 'two',
87+
'argumentTwo' => 'nine',
88+
'question' => 'min(two, nine)',
89+
],
90+
[
91+
'function' => 'plus',
92+
'argumentOne' => 'eight',
93+
'argumentTwo' => 'one',
94+
'question' => 'eight plus one',
95+
],
96+
[
97+
'function' => 'plus',
98+
'argumentOne' => 'three',
99+
'argumentTwo' => 'five',
100+
'question' => 'three plus five',
101+
],
102+
[
103+
'function' => 'min',
104+
'argumentOne' => 'eight',
105+
'argumentTwo' => 'three',
106+
'question' => 'min(eight, three)',
107+
],
108+
[
109+
'function' => 'max',
110+
'argumentOne' => 'zero',
111+
'argumentTwo' => 'nine',
112+
'question' => 'max(zero, nine)',
113+
],
114+
[
115+
'function' => 'min',
116+
'argumentOne' => 'five',
117+
'argumentTwo' => 'nine',
118+
'question' => 'min(five, nine)',
119+
],
120+
[
121+
'function' => 'minus',
122+
'argumentOne' => 'six',
123+
'argumentTwo' => 'four',
124+
'question' => 'six minus four',
125+
],
126+
[
127+
'function' => 'max',
128+
'argumentOne' => 'one',
129+
'argumentTwo' => 'one',
130+
'question' => 'max(one, one)',
131+
],
132+
[
133+
'function' => 'plus',
134+
'argumentOne' => 'five',
135+
'argumentTwo' => 'zero',
136+
'question' => 'five plus zero',
137+
],
138+
[
139+
'function' => 'minus',
140+
'argumentOne' => 'nine',
141+
'argumentTwo' => 'eight',
142+
'question' => 'nine minus eight',
143+
],
144+
[
145+
'function' => 'minus',
146+
'argumentOne' => 'three',
147+
'argumentTwo' => 'one',
148+
'question' => 'three minus one',
149+
],
150+
[
151+
'function' => 'min',
152+
'argumentOne' => 'three',
153+
'argumentTwo' => 'one',
154+
'question' => 'min(three, one)',
155+
],
156+
];
157+
158+
self::assertSame($expected, $challenges);
159+
}
160+
}

tests/Unit/IsEmailableAddressTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpweb\Test\Unit;
6+
7+
use PHPUnit\Framework;
8+
9+
#[Framework\Attributes\CoversFunction('is_emailable_address')]
10+
final class IsEmailableAddressTest extends Framework\TestCase
11+
{
12+
public static function setUpBeforeClass(): void
13+
{
14+
require_once __DIR__ . '/../../include/email-validation.inc';
15+
}
16+
17+
#[Framework\Attributes\DataProvider('provideInvalidEmail')]
18+
public function testIsEmailableAddressReturnsFalseWhenEmailIsInvalid(string $email): void
19+
{
20+
$isEmailableAddress = is_emailable_address($email);
21+
22+
self::assertFalse($isEmailableAddress);
23+
}
24+
25+
/**
26+
* @return \Generator<string, array{0: string}>
27+
*/
28+
public static function provideInvalidEmail(): \Generator
29+
{
30+
$values = [
31+
32+
33+
34+
'wrong-address-with@@@@-remove_me-and-some-i-hate_SPAM-stuff',
35+
36+
];
37+
38+
foreach ($values as $value) {
39+
yield $value => [
40+
$value,
41+
];
42+
}
43+
}
44+
45+
#[Framework\Attributes\DataProvider('provideValidEmail')]
46+
public function testIsEmailableAddressReturnsTrueWhenEmailIsValid(string $email): void
47+
{
48+
$isEmailableAddress = is_emailable_address($email);
49+
50+
self::assertTrue($isEmailableAddress);
51+
}
52+
53+
/**
54+
* @return \Generator<string, array{0: string}>
55+
*/
56+
public static function provideValidEmail(): \Generator
57+
{
58+
$values = [
59+
60+
61+
62+
63+
64+
65+
];
66+
67+
foreach ($values as $value) {
68+
yield $value => [
69+
$value,
70+
];
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)