Skip to content

Commit

Permalink
Use exceptions instead of assertions for type checking (to work aroun…
Browse files Browse the repository at this point in the history
…d phpunit behavior, possibly sebastianbergmann/phpunit#1897)
  • Loading branch information
asmecher committed Nov 23, 2018
1 parent 2352dd5 commit 5134fde
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
22 changes: 11 additions & 11 deletions classes/metadata/MetadataProperty.inc.php
Expand Up @@ -91,8 +91,8 @@ function __construct($name, $assocTypes = array(), $allowedTypes = METADATA_PROP
$translated = false, $cardinality = METADATA_PROPERTY_CARDINALITY_ONE, $displayName = null, $validationMessage = null, $mandatory = false) {

// Validate name and assoc type array
assert(is_string($name));
assert(is_array($assocTypes));
if (!is_string($name)) throw new InvalidArgumentException('$name should be a string.');
if (!is_array($assocTypes)) throw new InvalidArgumentException('$assocTypes should be an array.');

// A single type will be transformed to an
// array of types so that we can handle them
Expand Down Expand Up @@ -120,7 +120,7 @@ function __construct($name, $assocTypes = array(), $allowedTypes = METADATA_PROP
}

// Validate type
assert(in_array($allowedTypeId, MetadataProperty::getSupportedTypes()));
if (!in_array($allowedTypeId, MetadataProperty::getSupportedTypes())) throw new InvalidArgumentException('Allowed types must be supported types!');

// Transform the type array in a
// structure that is easy to handle
Expand All @@ -131,33 +131,33 @@ function __construct($name, $assocTypes = array(), $allowedTypes = METADATA_PROP
switch($allowedTypeId) {
case METADATA_PROPERTY_TYPE_COMPOSITE:
// Validate the assoc id of the composite.
assert(is_integer($allowedTypeParam));
if (!is_integer($allowedTypeParam)) throw new InvalidArgumentException('Allowed type parameter should be an integer.');
// Properties that allow composite types cannot be translated.
assert(!$translated);
if ($translated) throw new InvalidArgumentException('Properties that allow composite types cannot be translated.');
break;

case METADATA_PROPERTY_TYPE_VOCABULARY:
// Validate the symbolic name of the vocabulary.
assert(is_string($allowedTypeParam));
if (!is_string($allowedTypeParam)) throw new InvalidArgumentException('Allowed type parameter should be a string.');
break;

default:
// No other types support an additional parameter
assert(is_null($allowedTypeParam));
if (!is_null($allowedTypeParam)) throw new InvalidArgumentException('An additional parameter was supplied for an unsupported metadata property type.');
}
}

// Validate translation and cardinality
assert(is_bool($translated));
assert(in_array($cardinality, MetadataProperty::getSupportedCardinalities()));
if (!is_bool($translated)) throw new InvalidArgumentException('$translated must be a boolean');
if (!in_array($cardinality, MetadataProperty::getSupportedCardinalities())) throw new InvalidArgumentException('$cardinality must be a supported cardinality.');

// Default display name
if (is_null($displayName)) $displayName = 'metadata.property.displayName.'.$name;
assert(is_string($displayName));
if (!is_string($displayName)) throw new InvalidArgumentException('$displayName must be a string.');

// Default validation message
if (is_null($validationMessage)) $validationMessage = 'metadata.property.validationMessage.'.$name;
assert(is_string($validationMessage));
if (!is_string($validationMessage)) throw new InvalidArgumentException('$validationMessage must be a string.');


// Initialize the class
Expand Down
9 changes: 0 additions & 9 deletions tests/classes/core/PKPRouterTestCase.inc.php
Expand Up @@ -78,15 +78,6 @@ public function testIsCacheable() {
self::assertFalse($this->router->isCacheable($this->request));
}

/**
* @covers PKPRouter::getCacheFilename
* @expectedException PHPUnit_Framework_Error
*/
public function testGetCacheFilename() {
$this->request = new PKPRequest();
$this->router->getCacheFilename($this->request);
}

/**
* @covers PKPRouter::getRequestedContextPath
* @covers PKPRouter::getRequestedContextPaths
Expand Down
14 changes: 7 additions & 7 deletions tests/classes/metadata/MetadataPropertyTest.php
Expand Up @@ -67,7 +67,7 @@ public function testMetadataPropertyConstructor() {
/**
* Tests special error conditions while setting composite types
* @covers MetadataProperty::__construct
* @expectedException PHPUnit_Framework_Error
* @expectedException InvalidArgumentException
*/
public function testCompositeWithoutParameter() {
$metadataProperty = new MetadataProperty('testElement', array(0x001), METADATA_PROPERTY_TYPE_COMPOSITE, false, METADATA_PROPERTY_CARDINALITY_MANY);
Expand All @@ -76,7 +76,7 @@ public function testCompositeWithoutParameter() {
/**
* Tests special error conditions while setting composite types
* @covers MetadataProperty::__construct
* @expectedException PHPUnit_Framework_Error
* @expectedException InvalidArgumentException
*/
public function testCompositeWithWrongParameter() {
$metadataProperty = new MetadataProperty('testElement', array(0x001), array(METADATA_PROPERTY_TYPE_COMPOSITE => 'string'), false, METADATA_PROPERTY_CARDINALITY_MANY);
Expand All @@ -85,7 +85,7 @@ public function testCompositeWithWrongParameter() {
/**
* Tests special error conditions while setting controlled vocab types
* @covers MetadataProperty::__construct
* @expectedException PHPUnit_Framework_Error
* @expectedException InvalidArgumentException
*/
public function testControlledVocabWithoutParameter() {
$metadataProperty = new MetadataProperty('testElement', array(0x001), METADATA_PROPERTY_TYPE_VOCABULARY);
Expand All @@ -94,7 +94,7 @@ public function testControlledVocabWithoutParameter() {
/**
* Tests special error conditions while setting controlled vocab types
* @covers MetadataProperty::__construct
* @expectedException PHPUnit_Framework_Error
* @expectedException InvalidArgumentException
*/
public function testControlledVocabWithWrongParameter() {
$metadataProperty = new MetadataProperty('testElement', array(0x001), array(METADATA_PROPERTY_TYPE_VOCABULARY => 0x002), false, METADATA_PROPERTY_CARDINALITY_MANY);
Expand All @@ -103,7 +103,7 @@ public function testControlledVocabWithWrongParameter() {
/**
* Tests special error conditions while setting non-parameterized type
* @covers MetadataProperty::__construct
* @expectedException PHPUnit_Framework_Error
* @expectedException InvalidArgumentException
*/
public function testNonParameterizedTypeWithParameter() {
$metadataProperty = new MetadataProperty('testElement', array(0x001), array(METADATA_PROPERTY_TYPE_STRING => 0x002), false, METADATA_PROPERTY_CARDINALITY_MANY);
Expand All @@ -112,7 +112,7 @@ public function testNonParameterizedTypeWithParameter() {
/**
* Tests special error conditions while setting an unsupported type
* @covers MetadataProperty::getSupportedTypes
* @expectedException PHPUnit_Framework_Error
* @expectedException InvalidArgumentException
*/
public function testSetUnsupportedType() {
$metadataProperty = new MetadataProperty('testElement', array(0x001), 0x99999999, true, METADATA_PROPERTY_CARDINALITY_MANY);
Expand All @@ -121,7 +121,7 @@ public function testSetUnsupportedType() {
/**
* Tests special error conditions while setting an unsupported cardinality
* @covers MetadataProperty::getSupportedCardinalities
* @expectedException PHPUnit_Framework_Error
* @expectedException InvalidArgumentException
*/
public function testSetUnsupportedCardinality() {
$metadataProperty = new MetadataProperty('testElement', array(0x001), METADATA_PROPERTY_TYPE_COMPOSITE, true, 0x99999999);
Expand Down

0 comments on commit 5134fde

Please sign in to comment.