gplx/mediawiki/extensions/SemanticMediaWiki/tests/phpunit/Constraint/Constraints/NamespaceConstraintTest.php

100 lines
2.3 KiB
PHP

<?php
namespace SMW\Tests\Constraint\Constraints;
use SMW\Constraint\Constraints\NamespaceConstraint;
use SMW\DataItemFactory;
use SMW\Tests\TestEnvironment;
/**
* @covers \SMW\Constraint\Constraints\NamespaceConstraint
* @group semantic-mediawiki
*
* @license GPL-2.0-or-later
* @since 3.1
*
* @author mwjames
*/
class NamespaceConstraintTest extends \PHPUnit\Framework\TestCase {
private $testEnvironment;
private $dataItemFactory;
protected function setUp(): void {
$this->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;
}
}