testEnvironment = new TestEnvironment(); $this->dataItemFactory = new DataItemFactory(); } protected function tearDown(): void { $this->testEnvironment->tearDown(); } public function testCanConstruct() { $this->assertInstanceOf( NamespaceConstraint::class, new NamespaceConstraint() ); } public function testGetType() { $instance = new NamespaceConstraint(); $this->assertEquals( NamespaceConstraint::TYPE_INSTANT, $instance->getType() ); } public function testHasViolation() { $instance = new NamespaceConstraint(); $this->assertFalse( $instance->hasViolation() ); } public function testCheckConstraint_allowed_namespaces() { $constraint = [ 'allowed_namespaces' => [ 'NS_HELP' ] ]; $expectedErrMsg = 'smw-constraint-violation-allowed-namespace-no-match'; $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( $this->dataItemFactory->newDIWikiPage( 'Foo' ) ); $instance = new NamespaceConstraint(); $instance->checkConstraint( $constraint, $dataValue ); $this->assertTrue( $instance->hasViolation() ); } public function checkConstraintError( $error, $expectedErrMsg ) { if ( strpos( $error->__toString(), $expectedErrMsg ) !== false ) { return true; } return false; } }