dataItemFactory = new DataItemFactory(); } public function testCanConstruct() { $this->assertInstanceOf( MustExistsConstraint::class, new MustExistsConstraint() ); } public function testGetType() { $instance = new MustExistsConstraint(); $this->assertEquals( MustExistsConstraint::TYPE_INSTANT, $instance->getType() ); } public function testHasViolation() { $instance = new MustExistsConstraint(); $this->assertFalse( $instance->hasViolation() ); } public function testCheckConstraint_must_exists() { $constraint = [ 'must_exists' => true ]; $expectedErrMsg = 'smw-constraint-violation-must-exists'; $title = $this->getMockBuilder( '\MediaWiki\Title\Title' ) ->disableOriginalConstructor() ->getMock(); $title->expects( $this->atLeastOnce() ) ->method( 'exists' ) ->willReturn( false ); $dataItem = $this->getMockBuilder( '\SMW\DIWikiPage' ) ->disableOriginalConstructor() ->getMock(); $dataItem->expects( $this->atLeastOnce() ) ->method( 'getDIType' ) ->willReturn( \SMWDataItem::TYPE_WIKIPAGE ); $dataItem->expects( $this->atLeastOnce() ) ->method( 'getTitle' ) ->willReturn( $title ); $dataValue = $this->getMockBuilder( '\SMWDataValue' ) ->disableOriginalConstructor() ->setMethods( [ 'getProperty', 'getDataItem', 'addError' ] ) ->getMockForAbstractClass(); $dataValue->expects( $this->atLeastOnce() ) ->method( 'addError' ) ->with( $this->callback( function ( $error ) use ( $expectedErrMsg ) { return $this->checkConstraintError( $error, $expectedErrMsg ); } ) ); $dataValue->expects( $this->atLeastOnce() ) ->method( 'getProperty' ) ->willReturn( $this->dataItemFactory->newDIProperty( 'Bar' ) ); $dataValue->expects( $this->atLeastOnce() ) ->method( 'getDataItem' ) ->willReturn( $dataItem ); $instance = new MustExistsConstraint(); $instance->checkConstraint( $constraint, $dataValue ); $this->assertTrue( $instance->hasViolation() ); } public function testCheckConstraint_must_exists_ThrowsException() { $constraint = [ 'must_exists' => true ]; $instance = new MustExistsConstraint(); $this->expectException( '\RuntimeException' ); $instance->checkConstraint( $constraint, 'Foo' ); } public function checkConstraintError( $error, $expectedErrMsg ) { if ( strpos( $error->__toString(), $expectedErrMsg ) !== false ) { return true; } return false; } }