2017-06-18 14:23:37 +00:00
|
|
|
<?php
|
2017-11-17 01:23:18 +00:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-06-18 14:23:37 +00:00
|
|
|
declare(strict_types=1);
|
2017-12-22 03:47:19 +00:00
|
|
|
namespace JKingWeb\Arsse\TestCase\Misc;
|
2017-06-18 14:23:37 +00:00
|
|
|
|
2019-02-26 03:41:12 +00:00
|
|
|
use JKingWeb\Arsse\Context\Context;
|
2022-04-21 18:37:28 +00:00
|
|
|
use JKingWeb\Arsse\Context\ExclusionContext;
|
2022-04-29 20:35:46 +00:00
|
|
|
use JKingWeb\Arsse\Context\UnionContext;
|
2022-04-24 16:25:37 +00:00
|
|
|
use JKingWeb\Arsse\Misc\Date;
|
2019-02-22 17:34:06 +00:00
|
|
|
use JKingWeb\Arsse\Misc\ValueInfo;
|
2017-06-18 14:23:37 +00:00
|
|
|
|
2022-04-20 23:11:04 +00:00
|
|
|
/**
|
|
|
|
* @covers \JKingWeb\Arsse\Context\Context<extended>
|
|
|
|
* @covers \JKingWeb\Arsse\Context\ExclusionContext<extended>
|
2022-04-29 20:35:46 +00:00
|
|
|
* @covers \JKingWeb\Arsse\Context\UnionContext<extended>
|
2022-04-20 23:11:04 +00:00
|
|
|
*/
|
2017-12-22 03:47:19 +00:00
|
|
|
class TestContext extends \JKingWeb\Arsse\Test\AbstractTest {
|
2022-04-20 02:53:36 +00:00
|
|
|
protected $ranges = ['modifiedRange', 'markedRange', 'articleRange', 'editionRange'];
|
2022-04-21 18:37:28 +00:00
|
|
|
protected $times = ['modifiedRange', 'markedRange'];
|
2022-04-20 02:53:36 +00:00
|
|
|
|
2022-04-21 18:37:28 +00:00
|
|
|
/** @dataProvider provideContextOptions */
|
|
|
|
public function testSetContextOptions(string $method, array $input, $output, bool $not): void {
|
|
|
|
$parent = new Context;
|
|
|
|
$c = ($not) ? $parent->not : $parent;
|
2022-04-30 21:11:18 +00:00
|
|
|
$default = (new \ReflectionClass($c))->getDefaultProperties()[$method];
|
2022-04-21 18:37:28 +00:00
|
|
|
$this->assertFalse($c->$method(), "Context method did not initially return false");
|
|
|
|
if (in_array($method, $this->ranges)) {
|
|
|
|
$this->assertEquals([null, null], $c->$method, "Context property is not initially a two-member falsy array");
|
|
|
|
} else {
|
2022-04-22 23:22:50 +00:00
|
|
|
$this->assertFalse((bool) $c->$method, "Context property is not initially falsy");
|
2017-06-18 14:23:37 +00:00
|
|
|
}
|
2022-04-21 18:37:28 +00:00
|
|
|
$this->assertSame($parent, $c->$method(...$input), "Context method did not return the root after setting");
|
|
|
|
$this->assertTrue($c->$method());
|
|
|
|
if (in_array($method, $this->times)) {
|
|
|
|
if (is_array($default)) {
|
|
|
|
array_walk_recursive($c->$method, function(&$v, $k) {
|
|
|
|
if ($v !== null) {
|
|
|
|
$this->assertInstanceOf(\DateTimeImmutable::class, $v, "Context property contains an non-normalized date");
|
|
|
|
}
|
|
|
|
$v = ValueInfo::normalize($v, ValueInfo::T_STRING, null, "iso8601");
|
|
|
|
});
|
|
|
|
array_walk_recursive($output, function(&$v) {
|
|
|
|
$v = ValueInfo::normalize($v, ValueInfo::T_STRING, null, "iso8601");
|
|
|
|
});
|
|
|
|
$this->assertSame($c->$method, $output, "Context property did not return the expected results after setting");
|
|
|
|
} else {
|
|
|
|
$this->assertTime($c->$method, $output, "Context property did not return the expected results after setting");
|
2017-07-21 02:40:09 +00:00
|
|
|
}
|
2022-04-21 18:37:28 +00:00
|
|
|
} else {
|
|
|
|
$this->assertSame($c->$method, $output, "Context property did not return the expected results after setting");
|
2017-06-18 14:23:37 +00:00
|
|
|
}
|
2022-04-21 18:37:28 +00:00
|
|
|
// clear the context option
|
|
|
|
$c->$method(...array_fill(0, sizeof($input), null));
|
|
|
|
$this->assertFalse($c->$method(), "Context method did not return false after clearing");
|
2017-06-18 14:23:37 +00:00
|
|
|
}
|
2017-07-07 02:53:17 +00:00
|
|
|
|
2022-04-20 23:11:04 +00:00
|
|
|
public function provideContextOptions(): iterable {
|
2022-04-21 18:37:28 +00:00
|
|
|
$tests = [
|
2022-04-20 23:11:04 +00:00
|
|
|
'limit' => [[10], 10],
|
|
|
|
'offset' => [[5], 5],
|
|
|
|
'folder' => [[42], 42],
|
|
|
|
'folders' => [[[12,22]], [12,22]],
|
|
|
|
'folderShallow' => [[42], 42],
|
|
|
|
'foldersShallow' => [[[0,1]], [0,1]],
|
|
|
|
'tag' => [[44], 44],
|
|
|
|
'tags' => [[[44, 2112]], [44, 2112]],
|
|
|
|
'tagName' => [["XLIV"], "XLIV"],
|
|
|
|
'tagNames' => [[["XLIV", "MMCXII"]], ["XLIV", "MMCXII"]],
|
|
|
|
'subscription' => [[2112], 2112],
|
|
|
|
'subscriptions' => [[[44, 2112]], [44, 2112]],
|
|
|
|
'article' => [[255], 255],
|
|
|
|
'edition' => [[65535], 65535],
|
|
|
|
'unread' => [[true], true],
|
|
|
|
'starred' => [[true], true],
|
|
|
|
'hidden' => [[true], true],
|
|
|
|
'editions' => [[[1,2]], [1,2]],
|
|
|
|
'articles' => [[[1,2]], [1,2]],
|
|
|
|
'label' => [[2112], 2112],
|
|
|
|
'labels' => [[[2112, 1984]], [2112, 1984]],
|
|
|
|
'labelName' => [["Rush"], "Rush"],
|
|
|
|
'labelNames' => [[["Rush", "Orwell"]], ["Rush", "Orwell"]],
|
|
|
|
'labelled' => [[true], true],
|
|
|
|
'annotated' => [[true], true],
|
|
|
|
'searchTerms' => [[["foo", "bar"]], ["foo", "bar"]],
|
|
|
|
'annotationTerms' => [[["foo", "bar"]], ["foo", "bar"]],
|
|
|
|
'titleTerms' => [[["foo", "bar"]], ["foo", "bar"]],
|
|
|
|
'authorTerms' => [[["foo", "bar"]], ["foo", "bar"]],
|
|
|
|
'modifiedRange' => [["2020-03-06T22:08:03Z", "2022-12-31T06:33:12Z"], ["2020-03-06T22:08:03Z", "2022-12-31T06:33:12Z"]],
|
|
|
|
'markedRange' => [["2020-03-06T22:08:03Z", "2022-12-31T06:33:12Z"], ["2020-03-06T22:08:03Z", "2022-12-31T06:33:12Z"]],
|
|
|
|
'articleRange' => [[1, 100], [1, 100]],
|
|
|
|
'editionRange' => [[1, 100], [1, 100]],
|
|
|
|
];
|
2022-06-01 03:21:33 +00:00
|
|
|
foreach ($tests as $k => $t) {
|
2022-04-23 21:24:25 +00:00
|
|
|
yield $k => array_merge([$k], $t, [false]);
|
2022-04-21 18:37:28 +00:00
|
|
|
if (method_exists(ExclusionContext::class, $k)) {
|
2022-04-23 21:24:25 +00:00
|
|
|
yield "$k (not)" => array_merge([$k], $t, [true]);
|
2022-04-21 18:37:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-20 23:11:04 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testCleanIdArrayValues(): void {
|
2019-04-02 13:32:31 +00:00
|
|
|
$methods = ["articles", "editions", "tags", "labels", "subscriptions"];
|
|
|
|
$in = [1, "2", 3.5, 4.0, 4, "ook", 0, -20, true, false, null, new \DateTime(), -1.0];
|
|
|
|
$out = [1, 2, 4];
|
|
|
|
$c = new Context;
|
|
|
|
foreach ($methods as $method) {
|
|
|
|
$this->assertSame($out, $c->$method($in)->$method, "Context method $method did not return the expected results");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testCleanFolderIdArrayValues(): void {
|
2019-04-02 13:32:31 +00:00
|
|
|
$methods = ["folders", "foldersShallow"];
|
|
|
|
$in = [1, "2", 3.5, 4.0, 4, "ook", 0, -20, true, false, null, new \DateTime(), -1.0];
|
|
|
|
$out = [1, 2, 4, 0];
|
2017-07-07 02:53:17 +00:00
|
|
|
$c = new Context;
|
2017-08-29 14:50:31 +00:00
|
|
|
foreach ($methods as $method) {
|
2017-07-07 02:53:17 +00:00
|
|
|
$this->assertSame($out, $c->$method($in)->$method, "Context method $method did not return the expected results");
|
|
|
|
}
|
|
|
|
}
|
2019-02-22 17:34:06 +00:00
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testCleanStringArrayValues(): void {
|
2019-04-02 13:32:31 +00:00
|
|
|
$methods = ["searchTerms", "annotationTerms", "titleTerms", "authorTerms", "tagNames", "labelNames"];
|
2019-02-22 17:34:06 +00:00
|
|
|
$now = new \DateTime;
|
|
|
|
$in = [1, 3.0, "ook", 0, true, false, null, $now, ""];
|
|
|
|
$out = ["1", "3", "ook", "0", valueInfo::normalize($now, ValueInfo::T_STRING)];
|
|
|
|
$c = new Context;
|
|
|
|
foreach ($methods as $method) {
|
|
|
|
$this->assertSame($out, $c->$method($in)->$method, "Context method $method did not return the expected results");
|
|
|
|
}
|
|
|
|
}
|
2019-02-26 03:41:12 +00:00
|
|
|
|
2022-04-24 16:25:37 +00:00
|
|
|
public function testCleanDateRangeArrayValues(): void {
|
|
|
|
$methods = ["modifiedRanges", "markedRanges"];
|
|
|
|
$in = [null, 1, [1, 2, 3], [1], [null, null], ["ook", null], ["2022-09-13T06:46:28 America/Los_angeles", new \DateTime("2022-01-23T00:33:49Z")], [0, null], [null, 0]];
|
|
|
|
$out = [[Date::normalize("2022-09-13T13:46:28Z"), Date::normalize("2022-01-23T00:33:49Z")], [Date::normalize(0), null], [null, Date::normalize(0)]];
|
|
|
|
$c = new Context;
|
|
|
|
foreach ($methods as $method) {
|
|
|
|
$this->assertEquals($out, $c->$method($in)->$method, "Context method $method did not return the expected results");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testCloneAContext(): void {
|
2019-02-26 03:41:12 +00:00
|
|
|
$c1 = new Context;
|
|
|
|
$c2 = clone $c1;
|
|
|
|
$this->assertEquals($c1, $c2);
|
|
|
|
$this->assertEquals($c1->not, $c2->not);
|
|
|
|
$this->assertNotSame($c1, $c2);
|
|
|
|
$this->assertNotSame($c1->not, $c2->not);
|
2019-02-26 04:37:14 +00:00
|
|
|
$this->assertSame($c1, $c1->not->article(null));
|
|
|
|
$this->assertSame($c2, $c2->not->article(null));
|
2019-02-26 03:41:12 +00:00
|
|
|
}
|
2022-04-29 20:35:46 +00:00
|
|
|
|
|
|
|
public function testExerciseAUnionContext(): void {
|
|
|
|
$c1 = new UnionContext;
|
|
|
|
$c2 = new Context;
|
|
|
|
$c3 = new UnionContext;
|
|
|
|
$this->assertSame(0, sizeof($c1));
|
|
|
|
$c1[] = $c2;
|
|
|
|
$c1[2] = $c3;
|
|
|
|
$this->assertSame(2, sizeof($c1));
|
|
|
|
$this->assertSame($c2, $c1[0]);
|
|
|
|
$this->assertSame($c3, $c1[2]);
|
|
|
|
$this->assertSame(null, $c1[1]);
|
|
|
|
unset($c1[0]);
|
|
|
|
$this->assertFalse(isset($c1[0]));
|
|
|
|
$this->assertTrue(isset($c1[2]));
|
|
|
|
$c1[] = $c2;
|
|
|
|
$act = [];
|
2022-06-01 03:21:33 +00:00
|
|
|
foreach ($c1 as $k => $v) {
|
2022-04-29 20:35:46 +00:00
|
|
|
$act[$k] = $v;
|
|
|
|
}
|
|
|
|
$exp = [2 => $c3, $c2];
|
|
|
|
$this->assertSame($exp, $act);
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|