Lucas Werkmeister (WMDE) has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/391277 )
Change subject: Add CachingMetadata value object
......................................................................
Add CachingMetadata value object
A CachingMetadata object encapsulates information about how a value was
cached, without any information about the value itself. It can be
bundled together with a primitive value, or added to a CheckResult. It
is also possible to merge several pieces of information into one, for
results that are derived from more than one other result.
Bug: T179844
Change-Id: I27f19ef3b97618700435dacedab783df5e08461e
---
A includes/ConstraintCheck/Cache/CachingMetadata.php
A tests/phpunit/Cache/CachingMetadataTest.php
2 files changed, 160 insertions(+), 0 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQualityConstraints
refs/changes/77/391277/1
diff --git a/includes/ConstraintCheck/Cache/CachingMetadata.php
b/includes/ConstraintCheck/Cache/CachingMetadata.php
new file mode 100644
index 0000000..8fc39d0
--- /dev/null
+++ b/includes/ConstraintCheck/Cache/CachingMetadata.php
@@ -0,0 +1,76 @@
+<?php
+
+namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Cache;
+
+use Wikimedia\Assert\Assert;
+
+/**
+ * Information about whether and how a value was cached.
+ *
+ * @author Lucas Werkmeister
+ * @license GNU GPL v2+
+ */
+class CachingMetadata {
+
+ /**
+ * @var int|bool The maximum age in seconds,
+ * or false to indicate that the value wasn’t cached.
+ */
+ private $maxAge;
+
+ /**
+ * @return self Indication that a value is fresh, i. e. not cached.
+ */
+ public static function fresh() {
+ $ret = new self;
+ $ret->maxAge = false;
+ return $ret;
+ }
+
+ /**
+ * @param int $maxAge The maximum age of the cached value (in seconds).
+ * @return self Indication that a value is possibly outdated by up to
this many seconds.
+ */
+ public static function ofMaximumAgeInSeconds( $maxAge ) {
+ Assert::parameterType( 'integer', $maxAge, '$maxAge' );
+ Assert::parameter( $maxAge > 0, '$maxAge', '$maxage > 0' );
+ $ret = new self;
+ $ret->maxAge = $maxAge;
+ return $ret;
+ }
+
+ /**
+ * @param self[] $metadatas
+ * @return self
+ */
+ public static function merge( array $metadatas ) {
+ Assert::parameterElementType( self::class, $metadatas,
'$metadatas' );
+ $ret = new self;
+ $ret->maxAge = false;
+ foreach ( $metadatas as $metadata ) {
+ $ret->maxAge = max( $ret->maxAge, $metadata->maxAge );
+ }
+ return $ret;
+ }
+
+ /**
+ * @return bool Whether the value is cached or not (fresh).
+ */
+ public function isCached() {
+ return $this->maxAge !== false;
+ }
+
+ /**
+ * @return int The maximum age of the cached value (in seconds), in
other words:
+ * the value might be outdated by up to this many seconds.
+ * For a fresh value, returns 0.
+ */
+ public function getMaximumAgeInSeconds() {
+ if ( is_int( $this->maxAge ) ) {
+ return $this->maxAge;
+ } else {
+ return 0;
+ }
+ }
+
+}
diff --git a/tests/phpunit/Cache/CachingMetadataTest.php
b/tests/phpunit/Cache/CachingMetadataTest.php
new file mode 100644
index 0000000..7b895e3
--- /dev/null
+++ b/tests/phpunit/Cache/CachingMetadataTest.php
@@ -0,0 +1,84 @@
+<?php
+
+namespace WikibaseQuality\ConstraintReport\Test\Cache;
+
+use InvalidArgumentException;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachingMetadata;
+use Wikimedia\Assert\ParameterElementTypeException;
+use Wikimedia\Assert\ParameterTypeException;
+
+/**
+ * @covers
\WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachingMetadata
+ *
+ * @group WikibaseQualityConstraints
+ *
+ * @author Lucas Werkmeister
+ * @license GNU GPL v2+
+ */
+class CachingMetadataTest extends \PHPUnit_Framework_TestCase {
+
+ public function testFresh() {
+ $cm = CachingMetadata::fresh();
+
+ $this->assertFalse( $cm->isCached() );
+ $this->assertSame( 0, $cm->getMaximumAgeInSeconds() );
+ }
+
+ public function testOfMaximumAgeInSeconds() {
+ $cm = CachingMetadata::ofMaximumAgeInSeconds( 42 );
+
+ $this->assertTrue( $cm->isCached() );
+ $this->assertSame( 42, $cm->getMaximumAgeInSeconds() );
+ }
+
+ public function testOfMaximumAgeInSeconds_zero() {
+ $this->setExpectedException( InvalidArgumentException::class );
+
+ CachingMetadata::ofMaximumAgeInSeconds( 0 );
+ }
+
+ public function testOfMaximumAgeInSeconds_negative() {
+ $this->setExpectedException( InvalidArgumentException::class );
+
+ CachingMetadata::ofMaximumAgeInSeconds( -42 );
+ }
+
+ public function testOfMaximumAgeInSeconds_string() {
+ $this->setExpectedException( ParameterTypeException::class );
+
+ CachingMetadata::ofMaximumAgeInSeconds( '42' );
+ }
+
+ public function testMerge() {
+ $cm = CachingMetadata::merge( [
+ CachingMetadata::fresh(),
+ CachingMetadata::ofMaximumAgeInSeconds( 10 ),
+ CachingMetadata::ofMaximumAgeInSeconds( 42 ),
+ CachingMetadata::fresh(),
+ CachingMetadata::ofMaximumAgeInSeconds( 13 ),
+ ] );
+
+ $this->assertTrue( $cm->isCached() );
+ $this->assertSame( 42, $cm->getMaximumAgeInSeconds() );
+ }
+
+ public function testMerge_fresh() {
+ $cm = CachingMetadata::merge( [
+ CachingMetadata::fresh(),
+ CachingMetadata::fresh(),
+ ] );
+
+ $this->assertFalse( $cm->isCached() );
+ $this->assertSame( 0, $cm->getMaximumAgeInSeconds() );
+ }
+
+ public function testMerge_invalid() {
+ $this->setExpectedException(
ParameterElementTypeException::class );
+
+ CachingMetadata::merge( [
+ 10,
+ 42,
+ 13,
+ ] );
+ }
+}
--
To view, visit https://gerrit.wikimedia.org/r/391277
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I27f19ef3b97618700435dacedab783df5e08461e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits