Browse Source

Fix up context tests

reader
J. King 2 years ago
parent
commit
4a87926dd5
  1. 11
      lib/Context/Context.php
  2. 20
      lib/Context/RootMembers.php
  3. 103
      tests/cases/Misc/TestContext.php

11
lib/Context/Context.php

@ -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);
}
} }

20
lib/Context/RootMembers.php

@ -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);
}
}

103
tests/cases/Misc/TestContext.php

@ -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");
} else {
$this->assertEquals(null, $c->$method, "Context property $method is not initially falsy");
}
} }
} $this->assertSame($parent, $c->$method(...$input), "Context method did not return the root after setting");
$this->assertTrue($c->$method());
public function testSetContextOptions(): void { if (in_array($method, $this->times)) {
$v = [ if (is_array($default)) {
'reverse' => true, array_walk_recursive($c->$method, function(&$v, $k) {
'limit' => 10, if ($v !== null) {
'offset' => 5, $this->assertInstanceOf(\DateTimeImmutable::class, $v, "Context property contains an non-normalized date");
'folder' => 42, }
'folders' => [12,22], $v = ValueInfo::normalize($v, ValueInfo::T_STRING, null, "iso8601");
'folderShallow' => 42, });
'foldersShallow' => [0,1], array_walk_recursive($output, function(&$v) {
'tag' => 44, $v = ValueInfo::normalize($v, ValueInfo::T_STRING, null, "iso8601");
'tags' => [44, 2112], });
'tagName' => "XLIV", $this->assertSame($c->$method, $output, "Context property did not return the expected results after setting");
'tagNames' => ["XLIV", "MMCXII"], } else {
'subscription' => 2112, $this->assertTime($c->$method, $output, "Context property did not return the expected results after setting");
'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; } else {
$this->assertArrayHasKey($method, $v, "Context method $method not included in test"); $this->assertSame($c->$method, $output, "Context property did not return the expected results after setting");
$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());
} }
// clear the context option
$c->$method(...array_fill(0, sizeof($input), null));
$this->assertFalse($c->$method(), "Context method did not return false after clearing");
} }
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 {

Loading…
Cancel
Save