jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/399441 )
Change subject: Move CachingMetadata array serialization into class
......................................................................
Move CachingMetadata array serialization into class
This moves the serialization of CachingMetadata into an array structure
(or null) from CheckingResultsBuilder into the CachingMetadata itself,
and also adds matching deserialization.
Bug: T182992
Change-Id: I10548d60091fa1464ca7702282d0d4d71417ab78
---
M src/ConstraintCheck/Api/CheckingResultsBuilder.php
M src/ConstraintCheck/Cache/CachingMetadata.php
M tests/phpunit/Cache/CachingMetadataTest.php
3 files changed, 78 insertions(+), 5 deletions(-)
Approvals:
jenkins-bot: Verified
Thiemo Kreuz (WMDE): Looks good to me, approved
diff --git a/src/ConstraintCheck/Api/CheckingResultsBuilder.php
b/src/ConstraintCheck/Api/CheckingResultsBuilder.php
index 0a194ae..a90bf03 100644
--- a/src/ConstraintCheck/Api/CheckingResultsBuilder.php
+++ b/src/ConstraintCheck/Api/CheckingResultsBuilder.php
@@ -149,11 +149,9 @@
if ( $checkResult->getContext()->getType() ===
Context::TYPE_STATEMENT ) {
$result['claim'] =
$checkResult->getContext()->getSnakStatement()->getGuid();
}
- $cachingMetadata =
$checkResult->getMetadata()->getCachingMetadata();
- if ( $cachingMetadata->isCached() ) {
- $result['cached'] = [
- 'maximumAgeInSeconds' =>
$cachingMetadata->getMaximumAgeInSeconds(),
- ];
+ $cachingMetadataArray =
$checkResult->getMetadata()->getCachingMetadata()->toArray();
+ if ( $cachingMetadataArray !== null ) {
+ $result['cached'] = $cachingMetadataArray;
}
return $result;
diff --git a/src/ConstraintCheck/Cache/CachingMetadata.php
b/src/ConstraintCheck/Cache/CachingMetadata.php
index 18217be..328be5a 100644
--- a/src/ConstraintCheck/Cache/CachingMetadata.php
+++ b/src/ConstraintCheck/Cache/CachingMetadata.php
@@ -38,6 +38,19 @@
}
/**
+ * Deserializes the metadata from an array (or null if the value is
fresh).
+ * @param array|null $array As returned by toArray.
+ * @return CachingMetadata
+ */
+ public static function ofArray( array $array = null ) {
+ $ret = new self;
+ if ( $array !== null ) {
+ $ret->maxAge = $array['maximumAgeInSeconds'];
+ }
+ return $ret;
+ }
+
+ /**
* @param self[] $metadatas
* @return self
*/
@@ -70,4 +83,16 @@
}
}
+ /**
+ * Serializes the metadata into an array (or null if the value is
fresh).
+ * @return array|null
+ */
+ public function toArray() {
+ return $this->isCached() ?
+ [
+ 'maximumAgeInSeconds' => $this->maxAge,
+ ] :
+ null;
+ }
+
}
diff --git a/tests/phpunit/Cache/CachingMetadataTest.php
b/tests/phpunit/Cache/CachingMetadataTest.php
index a11f1a0..23434a6 100644
--- a/tests/phpunit/Cache/CachingMetadataTest.php
+++ b/tests/phpunit/Cache/CachingMetadataTest.php
@@ -84,4 +84,54 @@
] );
}
+ public function testToArray_fresh() {
+ $cm = CachingMetadata::fresh();
+
+ $array = $cm->toArray();
+
+ $this->assertNull( $array );
+ }
+
+ public function testToArray_ofMaximumAgeInSeconds() {
+ $cm = CachingMetadata::ofMaximumAgeInSeconds( 42 );
+
+ $array = $cm->toArray();
+
+ $this->assertSame( [ 'maximumAgeInSeconds' => 42 ], $array );
+ }
+
+ public function testOfArray_fresh() {
+ $array = null;
+
+ $cm = CachingMetadata::ofArray( $array );
+
+ $this->assertFalse( $cm->isCached() );
+ }
+
+ public function testOfArray_ofMaximumAgeInSeconds() {
+ $array = [ 'maximumAgeInSeconds' => 45 ];
+
+ $cm = CachingMetadata::ofArray( $array );
+
+ $this->assertTrue( $cm->isCached() );
+ $this->assertSame( 45, $cm->getMaximumAgeInSeconds() );
+ }
+
+ /**
+ * @dataProvider arrayRoundTripProvider
+ */
+ public function testOfArray_RoundTrip( CachingMetadata $cm ) {
+ $array = $cm->toArray();
+ $cm_ = CachingMetadata::ofArray( $array );
+
+ $this->assertEquals( $cm, $cm_ );
+ }
+
+ public function arrayRoundTripProvider() {
+ return [
+ [ CachingMetadata::fresh() ],
+ [ CachingMetadata::ofMaximumAgeInSeconds( 52 ) ],
+ ];
+ }
+
}
--
To view, visit https://gerrit.wikimedia.org/r/399441
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I10548d60091fa1464ca7702282d0d4d71417ab78
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: Thiemo Kreuz (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits