jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/362190 )
Change subject: Introduce `NewForm` builder for tests
......................................................................
Introduce `NewForm` builder for tests
Change-Id: I9a074288ca94073e21eb58eb4a701eccecf421ff
---
A tests/phpunit/composer/DataModel/NewForm.php
M tests/phpunit/composer/DataModel/NewLexeme.php
M tests/phpunit/composer/DataModel/Serialization/LexemeSerializerTest.php
3 files changed, 104 insertions(+), 15 deletions(-)
Approvals:
WMDE-leszek: Looks good to me, approved
jenkins-bot: Verified
Objections:
Thiemo Mättig (WMDE): There's a problem with this change, please improve
diff --git a/tests/phpunit/composer/DataModel/NewForm.php
b/tests/phpunit/composer/DataModel/NewForm.php
new file mode 100644
index 0000000..668091c
--- /dev/null
+++ b/tests/phpunit/composer/DataModel/NewForm.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Wikibase\Lexeme\Tests\DataModel;
+
+use Wikibase\Lexeme\DataModel\Form;
+use Wikibase\Lexeme\DataModel\FormId;
+
+class NewForm {
+
+ /**
+ * @var FormId
+ */
+ private $formId;
+
+ /**
+ * @var string
+ */
+ private $representation;
+
+ private function __construct() {
+ $this->formId = $this->newRandomItemId();
+ }
+
+ /**
+ * @param string $representation
+ * @return self
+ */
+ public static function havingRepresentation( $representation ) {
+ $result = new self();
+ return $result->andRepresentation( $representation );
+ }
+
+ /**
+ * @return Form
+ */
+ public function build() {
+ return new Form( $this->formId, $this->representation, [] );
+ }
+
+ private function newRandomItemId() {
+ return new FormId( 'F' . mt_rand( 1, mt_getrandmax() ) );
+ }
+
+ public function andRepresentation( $representation ) {
+ $result = clone $this;
+ $result->representation = $representation;
+ return $result;
+ }
+
+}
diff --git a/tests/phpunit/composer/DataModel/NewLexeme.php
b/tests/phpunit/composer/DataModel/NewLexeme.php
index 68a1859..093e93d 100644
--- a/tests/phpunit/composer/DataModel/NewLexeme.php
+++ b/tests/phpunit/composer/DataModel/NewLexeme.php
@@ -5,6 +5,7 @@
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\DataModel\Snak\Snak;
use Wikibase\DataModel\Term\TermList;
+use Wikibase\Lexeme\DataModel\Form;
use Wikibase\Lexeme\DataModel\Lexeme;
use Wikibase\Lexeme\DataModel\LexemeId;
use Wikibase\Lexeme\DataModel\Sense;
@@ -41,13 +42,27 @@
*/
private $senses = [];
- public function __construct() {
- $this->lexicalCategory = $this->newRandomItemId();
- $this->language = $this->newRandomItemId();
- }
+ /**
+ * @var Form[]
+ */
+ private $forms = [];
public static function create() {
return new self();
+ }
+
+ /**
+ * @param $form
+ * @return self
+ */
+ public static function havingForm( $form ) {
+ $result = new self();
+ return $result->withForm( $form );
+ }
+
+ public function __construct() {
+ $this->lexicalCategory = $this->newRandomItemId();
+ $this->language = $this->newRandomItemId();
}
public function build() {
@@ -57,7 +72,7 @@
$this->lexicalCategory,
$this->language,
null,
- [],
+ $this->forms,
$this->senses
);
@@ -135,11 +150,8 @@
}
public function __clone() {
- $statements = [];
- foreach ( $this->statements as $statement ) {
- $statements[] = clone $statement;
- }
- $this->statements = $statements;
+ $this->statements = $this->cloneArrayOfObjects(
$this->statements );
+ $this->forms = $this->cloneArrayOfObjects( $this->forms );
}
/**
@@ -159,4 +171,32 @@
return $result;
}
+ /**
+ * @param Form|NewForm $form
+ * @return self
+ */
+ public function withForm( $form ) {
+ $result = clone $this;
+
+ if ( $form instanceof NewForm ) {
+ $form = $form->build();
+ }
+
+ $result->forms[] = $form;
+
+ return $result;
+ }
+
+ /**
+ * @param array $objects
+ * @return array
+ */
+ private function cloneArrayOfObjects( array $objects ) {
+ $result = [];
+ foreach ( $objects as $object ) {
+ $result[] = clone $object;
+ }
+ return $result;
+ }
+
}
diff --git
a/tests/phpunit/composer/DataModel/Serialization/LexemeSerializerTest.php
b/tests/phpunit/composer/DataModel/Serialization/LexemeSerializerTest.php
index 5fec03b..f562a8e 100644
--- a/tests/phpunit/composer/DataModel/Serialization/LexemeSerializerTest.php
+++ b/tests/phpunit/composer/DataModel/Serialization/LexemeSerializerTest.php
@@ -12,14 +12,12 @@
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
use Wikibase\DataModel\Statement\Statement;
use Wikibase\DataModel\Statement\StatementList;
-use Wikibase\DataModel\Term\Term;
use Wikibase\DataModel\Term\TermList;
use Wikibase\Lexeme\DataModel\Lexeme;
use Wikibase\Lexeme\DataModel\Form;
use Wikibase\Lexeme\DataModel\FormId;
-use Wikibase\Lexeme\DataModel\Sense;
-use Wikibase\Lexeme\DataModel\SenseId;
use Wikibase\Lexeme\DataModel\Serialization\LexemeSerializer;
+use Wikibase\Lexeme\Tests\DataModel\NewForm;
use Wikibase\Lexeme\Tests\DataModel\NewLexeme;
use Wikibase\Lexeme\Tests\DataModel\NewSense;
@@ -179,8 +177,9 @@
}
public function
testLexemeFormWithRepresentation_SerializesFromRepresentation() {
- $lexeme = NewLexeme::create()->build();
- $lexeme->setForms( [ new Form( null, 'some representation', []
) ] );
+ $lexeme = NewLexeme::havingForm(
+ NewForm::havingRepresentation( 'some representation' )
+ )->build();
$serialization = $this->newSerializer()->serialize( $lexeme );
--
To view, visit https://gerrit.wikimedia.org/r/362190
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I9a074288ca94073e21eb58eb4a701eccecf421ff
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseLexeme
Gerrit-Branch: master
Gerrit-Owner: Aleksey Bekh-Ivanov (WMDE) <[email protected]>
Gerrit-Reviewer: Jonas Kress (WMDE) <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: WMDE-leszek <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits