Lucas Werkmeister (WMDE) has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/395559 )

Change subject: Add tests for CheckingResultsBuilder::getResults()
......................................................................

Add tests for CheckingResultsBuilder::getResults()

Bug: T181080
Change-Id: I06f9b6843618f7381b22838ff22be72e66f030ff
---
M tests/phpunit/Api/CheckingResultsBuilderTest.php
1 file changed, 87 insertions(+), 0 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQualityConstraints
 refs/changes/59/395559/1

diff --git a/tests/phpunit/Api/CheckingResultsBuilderTest.php 
b/tests/phpunit/Api/CheckingResultsBuilderTest.php
index e61754a..46f1749 100644
--- a/tests/phpunit/Api/CheckingResultsBuilderTest.php
+++ b/tests/phpunit/Api/CheckingResultsBuilderTest.php
@@ -21,6 +21,7 @@
 use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult;
 use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\NullResult;
 use WikibaseQuality\ConstraintReport\ConstraintParameterRenderer;
+use WikibaseQuality\ConstraintReport\Tests\DefaultConfig;
 use WikibaseQuality\ConstraintReport\Tests\Fake\FakeSnakContext;
 
 /**
@@ -29,6 +30,9 @@
  * @license GNU GPL v2+
  */
 class CheckingResultsBuilderTest extends \PHPUnit_Framework_TestCase {
+
+       const NONEXISTENT_ITEM = 'Q99';
+       const NONEXISTENT_CLAIM = 'Q99$dfb32791-ffd5-4420-a1d9-2bc2a0775968';
 
        use DefaultConfig;
 
@@ -61,6 +65,89 @@
                );
        }
 
+       public function testGetResults() {
+               $q1 = new ItemId( 'Q1' );
+               $q2 = new ItemId( 'Q2' );
+               $s1 = 'Q3$7f6d761c-bad5-47b6-a335-89635f102771';
+               $s2 = 'Q4$41dcb5ec-2ca5-4cfa-822b-a602038fc99f';
+               $constraintIDs = [ 'P1$47681880-d5f5-417d-96c3-570d6e94d234' ];
+               $mock = $this->getMockBuilder( 
DelegatingConstraintChecker::class )
+                       ->disableOriginalConstructor()
+                       ->setMethods( [ 'checkAgainstConstraintsOnEntityId', 
'checkAgainstConstraintsOnClaimId' ] );
+               $delegatingConstraintChecker = $mock->getMock();
+               $delegatingConstraintChecker->method( 
'checkAgainstConstraintsOnEntityId' )
+                       ->withConsecutive(
+                               [ $this->equalTo( $q1 ), $this->equalTo( 
$constraintIDs ), $this->callback( 'is_callable' ) ],
+                               [ $this->equalTo( $q2 ), $this->equalTo( 
$constraintIDs ), $this->callback( 'is_callable' ) ]
+                       )
+                       ->will( $this->returnCallback( function ( $entityId ) {
+                               return [ new CheckResult(
+                               new MainSnakContext(
+                                       new Item( $entityId ),
+                                       new Statement( new PropertyNoValueSnak( 
new PropertyId( 'P1' ) ) )
+                               ),
+                               new Constraint(
+                                       
'P1$47681880-d5f5-417d-96c3-570d6e94d234',
+                                       new PropertyId( 'P1' ),
+                                       'Q1',
+                                       []
+                               )
+                       ) ];
+                       } ) );
+               $delegatingConstraintChecker->method( 
'checkAgainstConstraintsOnClaimId' )
+                       ->withConsecutive(
+                               [ $this->equalTo( $s1 ), $this->equalTo( 
$constraintIDs ), $this->callback( 'is_callable' ) ],
+                               [ $this->equalTo( $s2 ), $this->equalTo( 
$constraintIDs ), $this->callback( 'is_callable' ) ]
+                       )
+                       ->will( $this->returnCallback( function ( $claimId ) {
+                               $entityId = new ItemId( substr( $claimId, 0, 2 
) );
+                               return [ new CheckResult(
+                               new MainSnakContext(
+                                       new Item( $entityId ),
+                                       new Statement( new PropertyNoValueSnak( 
new PropertyId( 'P1' ) ) )
+                               ),
+                               new Constraint(
+                                       
'P1$47681880-d5f5-417d-96c3-570d6e94d234',
+                                       new PropertyId( 'P1' ),
+                                       'Q1',
+                                       []
+                               )
+                       ) ];
+                       } ) );
+
+               $result = $this->getResultsBuilder( 
$delegatingConstraintChecker )->getResults(
+                       [ $q1, $q2 ],
+                       [ $s1, $s2 ],
+                       $constraintIDs
+               );
+
+               $this->assertSame( [ 'Q1', 'Q2', 'Q3', 'Q4' ], array_keys( 
$result ) );
+               foreach ( $result as $resultByQ ) {
+                       $this->assertSame( [ 'P1' ], array_keys ( 
$resultByQ['claims'] ) );
+                       $this->assertCount( 1, $resultByQ['claims']['P1'] );
+                       $this->assertCount( 1, 
$resultByQ['claims']['P1'][0]['mainsnak']['results'] );
+               }
+       }
+
+       public function testGetResults_Empty() {
+               $mock = $this->getMockBuilder( 
DelegatingConstraintChecker::class )
+                       ->disableOriginalConstructor()
+                       ->setMethods( [ 'checkAgainstConstraintsOnEntityId', 
'checkAgainstConstraintsOnClaimId' ] );
+               $delegatingConstraintChecker = $mock->getMock();
+               $delegatingConstraintChecker->method( 
'checkAgainstConstraintsOnEntityId' )
+                       ->willReturn( [] );
+               $delegatingConstraintChecker->method( 
'checkAgainstConstraintsOnClaimId' )
+                       ->willReturn( [] );
+
+               $result = $this->getResultsBuilder( 
$delegatingConstraintChecker )->getResults(
+                       [ new ItemId( self::NONEXISTENT_ITEM ) ],
+                       [ self::NONEXISTENT_CLAIM ],
+                       []
+               );
+
+               $this->assertEmpty( $result );
+       }
+
        public function testCheckResultToArray_NullResult() {
                $checkResult = new NullResult(
                        new FakeSnakContext( new PropertyNoValueSnak( new 
PropertyId( 'P1' ) ) )

-- 
To view, visit https://gerrit.wikimedia.org/r/395559
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I06f9b6843618f7381b22838ff22be72e66f030ff
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

Reply via email to