1
0
mirror of https://github.com/php/web-php.git synced 2026-03-23 23:02:13 +01:00

Test UserNotes\Sorter

We add a copy of the latest run-tests.php from the PHP-8.0 branch, and
tests for the `UserNotes\Sorter` with full code coverage.  We also run tests
on GitHub Actions.

Co-authored-by: Andreas Möller <am@localheinz.com>

Closes GH-606.
This commit is contained in:
Christoph M. Becker
2022-07-05 12:50:23 +02:00
committed by GitHub
parent c1dce9083b
commit 870dee1e16
6 changed files with 4008 additions and 0 deletions

3780
tests/run-tests.php Normal file

File diff suppressed because it is too large Load Diff

109
tests/sort_notes_001.phpt Normal file
View File

@@ -0,0 +1,109 @@
--TEST--
sort some notes
--INI--
precision=-1
--FILE--
<?php
require_once __DIR__ . "/../src/UserNotes/Sorter.php";
$notes = [
[
"votes" => ["up" => 0, "down" => 2],
"xwhen" => 1613487094,
], [
"votes" => ["up" => 0, "down" => 0],
"xwhen" => 1508180150,
], [
"votes" => ["up" => 14, "down" => 3],
"xwhen" => 1508179844,
], [
"votes" => ["up" => 14, "down" => 3],
"xwhen" => 1508179844,
],
];
$sorter = new phpweb\UserNotes\Sorter();
$sorter->sort($notes);
var_dump(array_map(function (array $note): array {
return [
"ts" => $note["xwhen"],
"upvotes" => $note["votes"]["up"],
"downvotes" => $note["votes"]["down"],
"score" => $note["score"],
"total" => $note["total"],
"rating" => $note["rating"],
"sort" => $note["sort"],
];
}, $notes));
?>
--EXPECT--
array(4) {
[2]=>
array(7) {
["ts"]=>
int(1508179844)
["upvotes"]=>
int(14)
["downvotes"]=>
int(3)
["score"]=>
int(11)
["total"]=>
int(17)
["rating"]=>
float(0.8235294117647058)
["sort"]=>
float(87.41176470588235)
}
[3]=>
array(7) {
["ts"]=>
int(1508179844)
["upvotes"]=>
int(14)
["downvotes"]=>
int(3)
["score"]=>
int(11)
["total"]=>
int(17)
["rating"]=>
float(0.8235294117647058)
["sort"]=>
float(87.41176470588235)
}
[1]=>
array(7) {
["ts"]=>
int(1508180150)
["upvotes"]=>
int(0)
["downvotes"]=>
int(0)
["score"]=>
int(0)
["total"]=>
int(0)
["rating"]=>
float(0.5)
["sort"]=>
float(45.49231350387337)
}
[0]=>
array(7) {
["ts"]=>
int(1613487094)
["upvotes"]=>
int(0)
["downvotes"]=>
int(2)
["score"]=>
int(-2)
["total"]=>
int(2)
["rating"]=>
int(0)
["sort"]=>
float(43.4)
}
}

27
tests/sort_notes_002.phpt Normal file
View File

@@ -0,0 +1,27 @@
--TEST--
sort no notes
--INI--
precision=-1
--FILE--
<?php
require_once __DIR__ . "/../src/UserNotes/Sorter.php";
$notes = [];
$sorter = new phpweb\UserNotes\Sorter();
$sorter->sort($notes);
var_dump(array_map(function (array $note): array {
return [
"ts" => $note["xwhen"],
"upvotes" => $note["votes"]["up"],
"downvotes" => $note["votes"]["down"],
"score" => $note["score"],
"total" => $note["total"],
"rating" => $note["rating"],
"sort" => $note["sort"],
];
}, $notes));
?>
--EXPECT--
array(0) {
}

49
tests/sort_notes_003.phpt Normal file
View File

@@ -0,0 +1,49 @@
--TEST--
sort a single note with no votes
--INI--
precision=-1
--FILE--
<?php
require_once __DIR__ . "/../src/UserNotes/Sorter.php";
$notes = [
[
"votes" => ["up" => 0, "down" => 0],
"xwhen" => 1613487094,
],
];
$sorter = new phpweb\UserNotes\Sorter();
$sorter->sort($notes);
var_dump(array_map(function (array $note): array {
return [
"ts" => $note["xwhen"],
"upvotes" => $note["votes"]["up"],
"downvotes" => $note["votes"]["down"],
"score" => $note["score"],
"total" => $note["total"],
"rating" => $note["rating"],
"sort" => $note["sort"],
];
}, $notes));
?>
--EXPECT--
array(1) {
[0]=>
array(7) {
["ts"]=>
int(1613487094)
["upvotes"]=>
int(0)
["downvotes"]=>
int(0)
["score"]=>
int(0)
["total"]=>
int(0)
["rating"]=>
float(0.5)
["sort"]=>
float(41.4)
}
}