jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/378421 )
Change subject: Simplify Eris usage in tests ...................................................................... Simplify Eris usage in tests Added ErisTest trait that controls correct setUp and tearDown Change-Id: Ib43a6812e3efb02b1cfff0d7144f0bc94b0704ae --- M tests/phpunit/composer/DataModel/Services/Diff/FormDifferPatcherTest.php M tests/phpunit/composer/DataModel/Services/Diff/LexemeDifferPatcherTest.php M tests/phpunit/composer/ErisGenerators/CartesianProductTest.php A tests/phpunit/composer/ErisGenerators/ErisTest.php A tests/phpunit/composer/ErisGenerators/PHPUnitTestCaseWrapper.php 5 files changed, 129 insertions(+), 37 deletions(-) Approvals: WMDE-leszek: Looks good to me, approved jenkins-bot: Verified diff --git a/tests/phpunit/composer/DataModel/Services/Diff/FormDifferPatcherTest.php b/tests/phpunit/composer/DataModel/Services/Diff/FormDifferPatcherTest.php index 4443d8c..45205cc 100644 --- a/tests/phpunit/composer/DataModel/Services/Diff/FormDifferPatcherTest.php +++ b/tests/phpunit/composer/DataModel/Services/Diff/FormDifferPatcherTest.php @@ -2,7 +2,6 @@ namespace Wikibase\Lexeme\Tests\DataModel\Services\Diff; -use Eris\Facade; use Wikibase\DataModel\Entity\ItemId; use Wikibase\DataModel\Entity\PropertyId; use Wikibase\DataModel\Snak\PropertySomeValueSnak; @@ -12,25 +11,22 @@ use Wikibase\Lexeme\DataModel\Services\Diff\FormDiffer; use Wikibase\Lexeme\DataModel\Services\Diff\FormPatcher; use Wikibase\Lexeme\Tests\DataModel\NewForm; +use Wikibase\Lexeme\Tests\ErisGenerators\ErisTest; use Wikibase\Lexeme\Tests\ErisGenerators\WikibaseLexemeGenerators; class FormDifferPatcherTest extends \PHPUnit_Framework_TestCase { - public function testProperty_PatchingLexemeWithGeneratedDiffAlwaysRestoresItToTheTargetState() { - if ( !class_exists( Facade::class ) ) { - $this->markTestSkipped( 'Package `giorgiosironi/eris` is not installed. Skipping' ); - } + use ErisTest; + public function testProperty_PatchingLexemeWithGeneratedDiffAlwaysRestoresItToTheTargetState() { $differ = new FormDiffer(); $patcher = new FormPatcher(); - //Lines below is needed to reproduce failures. In case of failure seed will be in the output - //$seed = 1504876177284329; - //putenv("ERIS_SEED=$seed"); + //Line below is needed to reproduce failures. In case of failure seed will be in the output + //$this->eris()->seed(1504876177284329)->forAll( ... - $eris = new Facade(); - - $eris->forAll( + $this->eris() + ->forAll( WikibaseLexemeGenerators::form( new FormId( 'F1' ) ), WikibaseLexemeGenerators::form( new FormId( 'F1' ) ) ) diff --git a/tests/phpunit/composer/DataModel/Services/Diff/LexemeDifferPatcherTest.php b/tests/phpunit/composer/DataModel/Services/Diff/LexemeDifferPatcherTest.php index 0b7e26e..d500d3b 100644 --- a/tests/phpunit/composer/DataModel/Services/Diff/LexemeDifferPatcherTest.php +++ b/tests/phpunit/composer/DataModel/Services/Diff/LexemeDifferPatcherTest.php @@ -2,14 +2,12 @@ namespace Wikibase\Lexeme\Tests\DataModel\Services\Diff; -use Wikibase\DataModel\Entity\ItemId; -use Wikibase\Lexeme\DataModel\Form; use Wikibase\Lexeme\DataModel\FormId; -use Eris\Facade; use Wikibase\Lexeme\DataModel\Lexeme; use Wikibase\Lexeme\DataModel\LexemeId; use Wikibase\Lexeme\DataModel\Services\Diff\LexemeDiffer; use Wikibase\Lexeme\DataModel\Services\Diff\LexemePatcher; +use Wikibase\Lexeme\Tests\ErisGenerators\ErisTest; use Wikibase\Lexeme\Tests\ErisGenerators\WikibaseLexemeGenerators; use Wikibase\Lexeme\Tests\DataModel\NewForm; use Wikibase\Lexeme\Tests\DataModel\NewLexeme; @@ -24,21 +22,17 @@ */ class LexemeDifferPatcherTest extends \PHPUnit_Framework_TestCase { - public function testProperty_PatchingLexemeWithGeneratedDiffAlwaysRestoresItToTheTargetState() { - if ( !class_exists( Facade::class ) ) { - $this->markTestSkipped( 'Package `giorgiosironi/eris` is not installed. Skipping' ); - } + use ErisTest; + public function testProperty_PatchingLexemeWithGeneratedDiffAlwaysRestoresItToTheTargetState() { $differ = new LexemeDiffer(); $patcher = new LexemePatcher(); - //Lines below is needed to reproduce failures. In case of failure seed will be in the output - //$seed = 1504876177284329; - //putenv("ERIS_SEED=$seed"); + //Line below is needed to reproduce failures. In case of failure seed will be in the output + //$this->eris()->seed(1504876177284329)->forAll( ... - $eris = new Facade(); - - $eris->forAll( + $this->eris() + ->forAll( WikibaseLexemeGenerators::lexeme( new LexemeId( 'L1' ) ), WikibaseLexemeGenerators::lexeme( new LexemeId( 'L1' ) ) ) diff --git a/tests/phpunit/composer/ErisGenerators/CartesianProductTest.php b/tests/phpunit/composer/ErisGenerators/CartesianProductTest.php index 3bde892..195c199 100644 --- a/tests/phpunit/composer/ErisGenerators/CartesianProductTest.php +++ b/tests/phpunit/composer/ErisGenerators/CartesianProductTest.php @@ -8,6 +8,8 @@ class CartesianProductTest extends \PHPUnit_Framework_TestCase { + use ErisTest; + public function testCanHandleOneGeneratedValueOptions() { $this->skipTestIfErisIsNotInstalled(); @@ -143,19 +145,6 @@ return new GeneratedValueOptions( [ GeneratedValueSingle::fromJustValue( $value ) ] ); - } - - protected function skipTestIfErisIsNotInstalled() { - if ( !self::erisIsInstalled() ) { - $this->markTestSkipped( 'Package `giorgiosironi/eris` is not installed. Skipping' ); - } - } - - /** - * @return bool - */ - private static function erisIsInstalled() { - return class_exists( Facade::class ); } } diff --git a/tests/phpunit/composer/ErisGenerators/ErisTest.php b/tests/phpunit/composer/ErisGenerators/ErisTest.php new file mode 100644 index 0000000..5aae42f --- /dev/null +++ b/tests/phpunit/composer/ErisGenerators/ErisTest.php @@ -0,0 +1,53 @@ +<?php + +namespace Wikibase\Lexeme\Tests\ErisGenerators; + +use Eris\Facade; + +/** + * Helper trait to simplify Eris usage in Mediawiki PHPUnit tests + * + * IMPORTANT: This trait can only be applied to \PHPUnit_Framework_TestCase + */ +trait ErisTest { + + private $testCaseWrapper; + + protected function eris() { + $this->skipTestIfErisIsNotInstalled(); + + $this->testCaseWrapper = new PHPUnitTestCaseWrapper( $this ); + + $this->testCaseWrapper->seedingRandomNumberGeneration(); + $this->testCaseWrapper->minimumEvaluationRatio( 0.5 ); + + return $this->testCaseWrapper; + } + + /** + * @after + */ + public function erisTeardown() { + if ( !self::erisIsInstalled() ) { + return; + } + + if ( $this->testCaseWrapper ) { + $this->testCaseWrapper->dumpSeedForReproducing(); + } + } + + protected function skipTestIfErisIsNotInstalled() { + if ( !self::erisIsInstalled() ) { + $this->markTestSkipped( 'Package `giorgiosironi/eris` is not installed. Skipping' ); + } + } + + /** + * @return bool + */ + private static function erisIsInstalled() { + return class_exists( Facade::class ); + } + +} diff --git a/tests/phpunit/composer/ErisGenerators/PHPUnitTestCaseWrapper.php b/tests/phpunit/composer/ErisGenerators/PHPUnitTestCaseWrapper.php new file mode 100644 index 0000000..9b76100 --- /dev/null +++ b/tests/phpunit/composer/ErisGenerators/PHPUnitTestCaseWrapper.php @@ -0,0 +1,60 @@ +<?php + +namespace Wikibase\Lexeme\Tests\ErisGenerators; + +use Eris\TestTrait; + +class PHPUnitTestCaseWrapper { + + use TestTrait { + limitTo as private traitLimitTo; + minimumEvaluationRatio as private traitMinimumEvaluationRatio; + shrinkingTimeLimit as private traitShrinkingTimeLimit; + withRand as private traitWithRand; + seedingRandomNumberGeneration as private traitSeedingRandomNumberGeneration; + dumpSeedForReproducing as private traitDumpSeedForReproducing; + } + + /** + * @var \PHPUnit_Framework_TestCase + */ + private $testCase; + + public function __construct( \PHPUnit_Framework_TestCase $testCase ) { + $this->testCase = $testCase; + } + + public function __call( $name, $arguments ) { + return call_user_func_array( [ $this->testCase, $name ], $arguments ); + } + + public function seed( $seed ) { + $this->seed = $seed; + return $this; + } + + public function limitTo( $limit ) { + return $this->traitLimitTo( $limit ); + } + + public function minimumEvaluationRatio( $ratio ) { + return $this->traitMinimumEvaluationRatio( $ratio ); + } + + public function shrinkingTimeLimit( $shrinkingTimeLimit ) { + return $this->traitShrinkingTimeLimit( $shrinkingTimeLimit ); + } + + public function withRand( $randFunction ) { + return $this->traitWithRand( $randFunction ); + } + + public function seedingRandomNumberGeneration() { + return $this->traitSeedingRandomNumberGeneration(); + } + + public function dumpSeedForReproducing() { + return $this->traitDumpSeedForReproducing(); + } + +} -- To view, visit https://gerrit.wikimedia.org/r/378421 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ib43a6812e3efb02b1cfff0d7144f0bc94b0704ae Gerrit-PatchSet: 6 Gerrit-Project: mediawiki/extensions/WikibaseLexeme Gerrit-Branch: master Gerrit-Owner: Aleksey Bekh-Ivanov (WMDE) <aleksey.bekh-iva...@wikimedia.de> Gerrit-Reviewer: Jonas Kress (WMDE) <jonas.kr...@wikimedia.de> Gerrit-Reviewer: WMDE-leszek <leszek.mani...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits