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

Change subject: Start using NewItem in tests
......................................................................

Start using NewItem in tests

This uses the NewItem builder introduced in I158ec2719a to construct
test items in the test methods instead of loading them from JSON files.
Creating the items within the test case is more readable than delegating
to external files, even though it is also more verbose.

Various other minor improvements (removal of unnecessary members,
extraction of other members, formatting improvements, …) are also
included.

Change-Id: Iefdb86fd793f45f1f0245313db56c70c0caa7805
---
M tests/phpunit/Checker/ConnectionChecker/ItemCheckerTest.php
M tests/phpunit/Checker/QualifierChecker/MandatoryQualifiersCheckerTest.php
2 files changed, 53 insertions(+), 46 deletions(-)


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

diff --git a/tests/phpunit/Checker/ConnectionChecker/ItemCheckerTest.php 
b/tests/phpunit/Checker/ConnectionChecker/ItemCheckerTest.php
index 82d8fb1..c659abd 100644
--- a/tests/phpunit/Checker/ConnectionChecker/ItemCheckerTest.php
+++ b/tests/phpunit/Checker/ConnectionChecker/ItemCheckerTest.php
@@ -13,6 +13,7 @@
 use WikibaseQuality\ConstraintReport\Tests\ConstraintParameters;
 use WikibaseQuality\ConstraintReport\Tests\ResultAssertions;
 use WikibaseQuality\Tests\Helper\JsonFileEntityLookup;
+use Wikibase\Repo\Tests\NewItem;
 use Wikibase\Repo\WikibaseRepo;
 
 /**
@@ -31,41 +32,27 @@
        use ConstraintParameters, ResultAssertions;
 
        /**
-        * @var JsonFileEntityLookup
-        */
-       private $lookup;
-
-       /**
-        * @var ConnectionCheckerHelper
-        */
-       private $connectionCheckerHelper;
-
-       /**
         * @var ItemChecker
         */
        private $checker;
 
        protected function setUp() {
                parent::setUp();
-               $this->lookup = new JsonFileEntityLookup( __DIR__ );
-               $this->connectionCheckerHelper = new ConnectionCheckerHelper();
                $this->checker = new ItemChecker(
-                       $this->lookup,
+                       new JsonFileEntityLookup( __DIR__ ),
                        $this->getConstraintParameterParser(),
-                       $this->connectionCheckerHelper,
+                       new ConnectionCheckerHelper(),
                        $this->getConstraintParameterRenderer()
                );
        }
 
        protected function tearDown() {
-               unset( $this->lookup );
-               unset( $this->connectionCheckerHelper );
                unset( $this->checker );
                parent::tearDown();
        }
 
        public function testItemConstraintInvalid() {
-               $entity = $this->lookup->getEntity( new ItemId( 'Q4' ) );
+               $entity = NewItem::withId( 'Q4' )->build();
                $constraintParameters = [
                        'property' => 'P2'
                ];
@@ -77,7 +64,9 @@
        }
 
        public function testItemConstraintProperty() {
-               $entity = $this->lookup->getEntity( new ItemId( 'Q5' ) );
+               $entity = NewItem::withId( 'Q5' )
+                       ->andPropertyValueSnak( 'P2', new EntityIdValue( new 
ItemId( 'Q42' ) ) )
+                       ->build();
                $constraintParameters = [
                        'property' => 'P2'
                ];
@@ -90,7 +79,9 @@
        }
 
        public function testItemConstraintPropertyButNotItem() {
-               $entity = $this->lookup->getEntity( new ItemId( 'Q5' ) );
+               $entity = NewItem::withId( 'Q5' )
+                       ->andPropertyValueSnak( 'P2', new EntityIdValue( new 
ItemId( 'Q42' ) ) )
+                       ->build();
                $constraintParameters = [
                        'property' => 'P2',
                        'item' => 'Q1'
@@ -104,7 +95,9 @@
        }
 
        public function testItemConstraintPropertyAndItem() {
-               $entity = $this->lookup->getEntity( new ItemId( 'Q5' ) );
+               $entity = NewItem::withId( 'Q5' )
+                       ->andPropertyValueSnak( 'P2', new EntityIdValue( new 
ItemId( 'Q42' ) ) )
+                       ->build();
                $constraintParameters = [
                        'property' => 'P2',
                        'item' => 'Q42'
@@ -117,7 +110,9 @@
        }
 
        public function testItemConstraintPropertyAndItemWithStatement() {
-               $entity = $this->lookup->getEntity( new ItemId( 'Q5' ) );
+               $entity = NewItem::withId( 'Q5' )
+                       ->andPropertyValueSnak( 'P2', new EntityIdValue( new 
ItemId( 'Q42' ) ) )
+                       ->build();
 
                $snakSerializer = 
WikibaseRepo::getDefaultInstance()->getSerializerFactory()->newSnakSerializer();
                $config = $this->getDefaultConfig();
diff --git 
a/tests/phpunit/Checker/QualifierChecker/MandatoryQualifiersCheckerTest.php 
b/tests/phpunit/Checker/QualifierChecker/MandatoryQualifiersCheckerTest.php
index 2464651..ad17a37 100644
--- a/tests/phpunit/Checker/QualifierChecker/MandatoryQualifiersCheckerTest.php
+++ b/tests/phpunit/Checker/QualifierChecker/MandatoryQualifiersCheckerTest.php
@@ -2,15 +2,16 @@
 
 namespace WikibaseQuality\ConstraintReport\Test\QualifierChecker;
 
-use Wikibase\DataModel\Entity\Item;
-use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\DataModel\Snak\PropertyNoValueSnak;
+use Wikibase\DataModel\Snak\SnakList;
 use Wikibase\DataModel\Statement\Statement;
-use Wikibase\DataModel\Statement\StatementListProvider;
 use WikibaseQuality\ConstraintReport\Constraint;
 use 
WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\MandatoryQualifiersChecker;
 use WikibaseQuality\ConstraintReport\Tests\ConstraintParameters;
 use WikibaseQuality\ConstraintReport\Tests\ResultAssertions;
 use WikibaseQuality\Tests\Helper\JsonFileEntityLookup;
+use Wikibase\Repo\Tests\NewItem;
 
 /**
  * @covers 
\WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\MandatoryQualifiersChecker
@@ -28,43 +29,54 @@
        use ConstraintParameters, ResultAssertions;
 
        /**
-        * @var JsonFileEntityLookup
+        * @var MandatoryQualifiersChecker
         */
-       private $lookup;
+       private $checker;
 
        protected function setUp() {
                parent::setUp();
-               $this->lookup = new JsonFileEntityLookup( __DIR__ );
+               $this->checker = new MandatoryQualifiersChecker(
+                       $this->getConstraintParameterParser(),
+                       $this->getConstraintParameterRenderer()
+               );
        }
 
        protected function tearDown() {
-               unset( $this->lookup );
+               unset( $this->checker );
                parent::tearDown();
        }
 
-       /**
-        * @param StatementListProvider $entity
-        *
-        * @return Statement|false
-        */
-       private function getFirstStatement( StatementListProvider $entity ) {
-               $statements = $entity->getStatements()->toArray();
-               return reset( $statements );
-       }
-
        public function testMandatoryQualifiersConstraintValid() {
-               /** @var Item $entity */
-               $entity = $this->lookup->getEntity( new ItemId( 'Q5' ) );
-               $qualifierChecker = new MandatoryQualifiersChecker( 
$this->getConstraintParameterParser(), $this->getConstraintParameterRenderer() 
);
-               $checkResult = $qualifierChecker->checkConstraint( 
$this->getFirstStatement( $entity ), $this->getConstraintMock( [ 'property' => 
'P2' ] ), $entity );
+               $statement = new Statement(
+                       new PropertyNoValueSnak( new PropertyId( 'P1' ) ),
+                       new SnakList( [
+                               new PropertyNoValueSnak( new PropertyId( 'P2' ) 
)
+                       ] )
+               );
+               $entity = NewItem::withId( 'Q5' )
+                       ->andStatement( $statement )
+                       ->build();
+               $parameters = [ 'property' => 'P2' ];
+
+               $checkResult = $this->checker->checkConstraint( $statement, 
$this->getConstraintMock( $parameters ), $entity );
+
                $this->assertCompliance( $checkResult );
        }
 
        public function testMandatoryQualifiersConstraintInvalid() {
-               /** @var Item $entity */
-               $entity = $this->lookup->getEntity( new ItemId( 'Q5' ) );
-               $qualifierChecker = new MandatoryQualifiersChecker( 
$this->getConstraintParameterParser(), $this->getConstraintParameterRenderer() 
);
-               $checkResult = $qualifierChecker->checkConstraint( 
$this->getFirstStatement( $entity ), $this->getConstraintMock( [ 'property' => 
'P3' ] ), $entity );
+               $statement = new Statement(
+                       new PropertyNoValueSnak( new PropertyId( 'P1' ) ),
+                       new SnakList( [
+                               new PropertyNoValueSnak( new PropertyId( 'P2' ) 
)
+                       ] )
+               );
+               $entity = NewItem::withId( 'Q5' )
+                       ->andStatement( $statement )
+                       ->build();
+               $parameters = [ 'property' => 'P3' ];
+
+               $checkResult = $this->checker->checkConstraint( $statement, 
$this->getConstraintMock( $parameters ), $entity );
+
                $this->assertViolation( $checkResult, 
'wbqc-violation-message-mandatory-qualifier' );
        }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iefdb86fd793f45f1f0245313db56c70c0caa7805
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <lucas.werkmeis...@wikimedia.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to