Skip to content

Commit

Permalink
Updated PHPUnit tests that should generate errors
Browse files Browse the repository at this point in the history
  • Loading branch information
joelit committed Oct 10, 2024
1 parent dfa938b commit e0ee622
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 27 deletions.
21 changes: 16 additions & 5 deletions tests/ConceptPropertyValueLiteralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,22 @@ public function testGetLabelThatIsADate()
*/
public function testGetLabelThatIsABrokenDate()
{
$this->expectWarning();
$vocab = $this->model->getVocabulary('dates');
$concept = $vocab->getConceptInfo("http://www.skosmos.skos/date/d2", "en");
$props = $concept->getProperties();
$propvals = $props['http://www.skosmos.skos/date/ownDate']->getValues();
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
});

try {
$vocab = $this->model->getVocabulary('dates');

$this->expectException(\ErrorException::class);
$this->expectExceptionMessage("Failed to parse time string (1986-21-00) at position 6 (1): Unexpected character");

$concept = $vocab->getConceptInfo("http://www.skosmos.skos/date/d2", "en");
$props = $concept->getProperties();
$propvals = $props['http://www.skosmos.skos/date/ownDate']->getValues();
} finally {
restore_error_handler();
}
}

/**
Expand Down
20 changes: 16 additions & 4 deletions tests/ConceptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,22 @@ public function testGetDateWithCreatedAndModified()
*/
public function testGetTimestampInvalidWarning()
{
$this->expectError();
$vocab = $this->model->getVocabulary('test');
$concept = $vocab->getConceptInfo("http://www.skosmos.skos/test/ta114", "en");
$props = $concept->getDate(); # this should throw a E_USER_WARNING exception
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
});

try {
$vocab = $this->model->getVocabulary('test');

$this->expectException(\ErrorException::class);
$this->expectExceptionMessage("Failed to parse time string (1986-21-00) at position 6 (1): Unexpected character");

$concept = $vocab->getConceptInfo("http://www.skosmos.skos/test/ta114", "en");
$props = $concept->getDate(); # this should throw an ErrorException

} finally {
restore_error_handler();
}
}

/**
Expand Down
73 changes: 55 additions & 18 deletions tests/VocabularyConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ protected function setUp(): void
setlocale(LC_ALL, 'en_GB.utf8');
$this->model = new Model('/../../tests/testconfig.ttl');
$this->assertNotNull($this->model->getVocabulary('test')->getConfig()->getPluginRegister(), "The PluginRegister of the model was not initialized!");


}

/**
Expand Down Expand Up @@ -164,10 +166,17 @@ public function testGetDefaultLanguage()
*/
public function testGetDefaultLanguageWhenNotSet()
{
$vocab = $this->model->getVocabulary('testdiff');
$this->expectError();
$this->expectErrorMessage("Default language for vocabulary 'testdiff' unknown, choosing 'en'.");
$lang = $vocab->getConfig()->getDefaultLanguage();
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
});
try {
$vocab = $this->model->getVocabulary('testdiff');
$this->expectException(\ErrorException::class);
$this->expectExceptionMessage("Default language for vocabulary 'testdiff' unknown, choosing 'en'.");
$lang = $vocab->getConfig()->getDefaultLanguage();
} finally {
restore_error_handler();
}
}

/**
Expand Down Expand Up @@ -210,10 +219,19 @@ public function testGetDataURLs()
*/
public function testGetDataURLsNotGuessable()
{
$vocab = $this->model->getVocabulary('test');
$this->expectWarning();
$this->expectWarningMessage("Could not guess format for <http://skosmos.skos/dump/test/>.");
$url = $vocab->getConfig()->getDataURLs();
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
});

try {
$vocab = $this->model->getVocabulary('test');
$this->expectException(\ErrorException::class);
$this->expectExceptionMessage("Could not guess format for <http://skosmos.skos/dump/test/>.");

$url = $vocab->getConfig()->getDataURLs();
} finally {
restore_error_handler();
}
}

/**
Expand Down Expand Up @@ -243,11 +261,21 @@ public function testGetDataURLsMarc()
*/
public function testGetDataURLsMarcNotDefined()
{
$vocab = $this->model->getVocabulary('marc-undefined');
$this->expectWarning();
$this->expectWarningMessage("Could not guess format for <http://skosmos.skos/dump/test/marc-undefined.mrcx>.");
$url = $vocab->getConfig()->getDataURLs();
$this->assertEquals(array(), $url);
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
});

try {
$vocab = $this->model->getVocabulary('marc-undefined');

$this->expectException(\ErrorException::class);
$this->expectExceptionMessage("Could not guess format for <http://skosmos.skos/dump/test/marc-undefined.mrcx>.");

$url = $vocab->getConfig()->getDataURLs();
$this->assertEquals(array(), $url);
} finally {
restore_error_handler();
}
}

/**
Expand Down Expand Up @@ -660,11 +688,20 @@ public function testGetPropertyOrderDefault()
*/
public function testGetPropertyOrderUnknown()
{
$vocab = $this->model->getVocabulary('testUnknownPropertyOrder');
$this->expectWarning();
$this->expectWarningMessage("Property order for vocabulary 'testUnknownPropertyOrder' unknown, using default order");
$params = $vocab->getConfig()->getPropertyOrder();
$this->assertEquals(VocabularyConfig::DEFAULT_PROPERTY_ORDER, $params);
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
});

try {
$vocab = $this->model->getVocabulary('testUnknownPropertyOrder');
$this->expectException(\ErrorException::class);
$this->expectExceptionMessage("Property order for vocabulary 'testUnknownPropertyOrder' unknown, using default order");

$params = $vocab->getConfig()->getPropertyOrder();
$this->assertEquals(VocabularyConfig::DEFAULT_PROPERTY_ORDER, $params);
} finally {
restore_error_handler();
}
}

/**
Expand Down

0 comments on commit e0ee622

Please sign in to comment.