jenkins-bot has submitted this change and it was merged. ( 
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
---
M extension.json
A includes/ConstraintCheck/Cache/CachingMetadata.php
A tests/phpunit/Cache/CachingMetadataTest.php
3 files changed, 162 insertions(+), 0 deletions(-)

Approvals:
  jenkins-bot: Verified
  Thiemo Mättig (WMDE): Looks good to me, approved



diff --git a/extension.json b/extension.json
index edce6dd..2fe1608 100644
--- a/extension.json
+++ b/extension.json
@@ -374,6 +374,7 @@
                "WikibaseQuality\\ConstraintReport\\Api\\CheckConstraints": 
"api/CheckConstraints.php",
                "WikibaseQuality\\ConstraintReport\\CachingConstraintLookup": 
"includes/CachingConstraintLookup.php",
                "WikibaseQuality\\ConstraintReport\\Constraint": 
"includes/Constraint.php",
+               
"WikibaseQuality\\ConstraintReport\\ConstraintCheck\\Cache\\CachingMetadata": 
"includes/ConstraintCheck/Cache/CachingMetadata.php",
                
"WikibaseQuality\\ConstraintReport\\ConstraintCheck\\Checker\\CommonsLinkChecker":
 "includes/ConstraintCheck/Checker/CommonsLinkChecker.php",
                
"WikibaseQuality\\ConstraintReport\\ConstraintCheck\\Checker\\ConflictsWithChecker":
 "includes/ConstraintCheck/Checker/ConflictsWithChecker.php",
                
"WikibaseQuality\\ConstraintReport\\ConstraintCheck\\Checker\\DiffWithinRangeChecker":
 "includes/ConstraintCheck/Checker/DiffWithinRangeChecker.php",
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..c64a8b5
--- /dev/null
+++ b/tests/phpunit/Cache/CachingMetadataTest.php
@@ -0,0 +1,85 @@
+<?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: merged
Gerrit-Change-Id: I27f19ef3b97618700435dacedab783df5e08461e
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to