1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 21:22:40 +00:00

Fix up context tests

This commit is contained in:
J. King 2022-04-21 14:37:28 -04:00
parent 6f1332c559
commit 4a87926dd5
3 changed files with 61 additions and 73 deletions

View file

@ -7,13 +7,12 @@ declare(strict_types=1);
namespace JKingWeb\Arsse\Context; namespace JKingWeb\Arsse\Context;
class Context extends AbstractContext { class Context extends AbstractContext {
use RootMembers;
use BooleanMembers; use BooleanMembers;
use ExclusionMembers; use ExclusionMembers;
/** @var ExclusionContext */ /** @var ExclusionContext */
public $not; public $not;
public $limit = 0;
public $offset = 0;
public function __construct() { public function __construct() {
$this->not = new ExclusionContext($this); $this->not = new ExclusionContext($this);
@ -28,12 +27,4 @@ class Context extends AbstractContext {
public function __destruct() { public function __destruct() {
unset($this->not); unset($this->not);
} }
public function limit(int $spec = null) {
return $this->act(__FUNCTION__, func_num_args(), $spec);
}
public function offset(int $spec = null) {
return $this->act(__FUNCTION__, func_num_args(), $spec);
}
} }

View file

@ -0,0 +1,20 @@
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\Context;
trait RootMembers {
public $limit = 0;
public $offset = 0;
public function limit(int $spec = null) {
return $this->act(__FUNCTION__, func_num_args(), $spec);
}
public function offset(int $spec = null) {
return $this->act(__FUNCTION__, func_num_args(), $spec);
}
}

View file

@ -7,6 +7,7 @@ declare(strict_types=1);
namespace JKingWeb\Arsse\TestCase\Misc; namespace JKingWeb\Arsse\TestCase\Misc;
use JKingWeb\Arsse\Context\Context; use JKingWeb\Arsse\Context\Context;
use JKingWeb\Arsse\Context\ExclusionContext;
use JKingWeb\Arsse\Misc\ValueInfo; use JKingWeb\Arsse\Misc\ValueInfo;
/** /**
@ -15,76 +16,46 @@ use JKingWeb\Arsse\Misc\ValueInfo;
*/ */
class TestContext extends \JKingWeb\Arsse\Test\AbstractTest { class TestContext extends \JKingWeb\Arsse\Test\AbstractTest {
protected $ranges = ['modifiedRange', 'markedRange', 'articleRange', 'editionRange']; protected $ranges = ['modifiedRange', 'markedRange', 'articleRange', 'editionRange'];
protected $times = ['modifiedRange', 'markedRange'];
public function testVerifyInitialState(): void { /** @dataProvider provideContextOptions */
$c = new Context; public function testSetContextOptions(string $method, array $input, $output, bool $not): void {
foreach ((new \ReflectionObject($c))->getMethods(\ReflectionMethod::IS_PUBLIC) as $m) { $parent = new Context;
if ($m->isStatic() || strpos($m->name, "__") === 0) { $c = ($not) ? $parent->not : $parent;
continue; $default = (new \ReflectionProperty($c, $method))->getDefaultValue();
} $this->assertFalse($c->$method(), "Context method did not initially return false");
$method = $m->name; if (in_array($method, $this->ranges)) {
$this->assertFalse($c->$method(), "Context method $method did not initially return false"); $this->assertEquals([null, null], $c->$method, "Context property is not initially a two-member falsy array");
if (in_array($method, $this->ranges)) { } else {
$this->assertEquals([null, null], $c->$method, "Context property $method is not initially a two-member falsy array"); $this->assertEquals(null, $c->$method, "Context property is not initially falsy");
}
$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 { } else {
$this->assertEquals(null, $c->$method, "Context property $method is not initially falsy"); $this->assertTime($c->$method, $output, "Context property did not return the expected results after setting");
} }
} else {
$this->assertSame($c->$method, $output, "Context property did not return the expected results after setting");
} }
} // clear the context option
$c->$method(...array_fill(0, sizeof($input), null));
public function testSetContextOptions(): void { $this->assertFalse($c->$method(), "Context method did not return false after clearing");
$v = [
'reverse' => true,
'limit' => 10,
'offset' => 5,
'folder' => 42,
'folders' => [12,22],
'folderShallow' => 42,
'foldersShallow' => [0,1],
'tag' => 44,
'tags' => [44, 2112],
'tagName' => "XLIV",
'tagNames' => ["XLIV", "MMCXII"],
'subscription' => 2112,
'subscriptions' => [44, 2112],
'article' => 255,
'edition' => 65535,
'unread' => true,
'starred' => true,
'hidden' => true,
'editions' => [1,2],
'articles' => [1,2],
'label' => 2112,
'labels' => [2112, 1984],
'labelName' => "Rush",
'labelNames' => ["Rush", "Orwell"],
'labelled' => true,
'annotated' => true,
'searchTerms' => ["foo", "bar"],
'annotationTerms' => ["foo", "bar"],
'titleTerms' => ["foo", "bar"],
'authorTerms' => ["foo", "bar"],
'not' => (new Context)->subscription(5),
];
$c = new Context;
foreach ((new \ReflectionObject($c))->getMethods(\ReflectionMethod::IS_PUBLIC) as $m) {
if ($m->isStatic() || strpos($m->name, "__") === 0 || in_array($m->name, $this->ranges)) {
continue;
}
$method = $m->name;
$this->assertArrayHasKey($method, $v, "Context method $method not included in test");
$this->assertInstanceOf(Context::class, $c->$method($v[$method]));
$this->assertTrue($c->$method());
$this->assertSame($c->$method, $v[$method], "Context method $method did not return the expected results");
// clear the context option
$c->$method(null);
$this->assertFalse($c->$method());
}
} }
public function provideContextOptions(): iterable { public function provideContextOptions(): iterable {
return [ $tests = [
'reverse' => [[true], true],
'limit' => [[10], 10], 'limit' => [[10], 10],
'offset' => [[5], 5], 'offset' => [[5], 5],
'folder' => [[42], 42], 'folder' => [[42], 42],
@ -119,6 +90,12 @@ class TestContext extends \JKingWeb\Arsse\Test\AbstractTest {
'articleRange' => [[1, 100], [1, 100]], 'articleRange' => [[1, 100], [1, 100]],
'editionRange' => [[1, 100], [1, 100]], 'editionRange' => [[1, 100], [1, 100]],
]; ];
foreach($tests as $k => $t) {
yield $k => [$k, ...$t, false];
if (method_exists(ExclusionContext::class, $k)) {
yield "$k (not)" => [$k, ...$t, true];
}
}
} }
public function testCleanIdArrayValues(): void { public function testCleanIdArrayValues(): void {