From a06e8bfaee7de3bc8c2691e8d6462a52d5345e09 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 18 Jul 2023 20:18:42 +0200 Subject: Zotlabs: Improve type safety for AccessList class. Add type annotations for constructor and set* methods, and throw an exception if the passed in arrays are missing required keys. This means that both invalid input types and missing keys will throw and exception rather than just die with a runtime error. There's not checks to verify that the contents of the required array keys are valid or make sense, though. They are just assigned, and returned as is by the get method when requested. Also, the set_from_array method is not well tested at the moment. --- tests/unit/Access/AccessListTest.php | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) (limited to 'tests') diff --git a/tests/unit/Access/AccessListTest.php b/tests/unit/Access/AccessListTest.php index 2f185db17..635f09930 100644 --- a/tests/unit/Access/AccessListTest.php +++ b/tests/unit/Access/AccessListTest.php @@ -60,14 +60,24 @@ class AccessListTest extends UnitTestCase { } /** - * @expectedException PHPUnit\Framework\Error\Error + * AccessList constructor should throw an exception if input is not + * an array. */ -/* - public function testPHPErrorOnInvalidConstructor() { + public function testConstructorThrowsOnInvalidInputType() { + $this->expectException("TypeError"); $accessList = new AccessList('invalid'); - // Causes: "Illegal string offset 'channel_allow_cid'" } -*/ + + /** + * AccessList constructor should throw an exception on + * invalid input. + */ + public function testConstructorThrowsOnMissingKeysInArray() { + $this->expectException("Exception"); + $this->expectExceptionMessage("Invalid AccessList object"); + $accessList = new AccessList(['something_else' => 'should_this_fail?']); + } + /** * Test that the defaults are as expected when constructed with * an empty array. @@ -111,16 +121,22 @@ class AccessListTest extends UnitTestCase { } /** - * @expectedException PHPUnit\Framework\Error\Error + * The set method should throw an exception if input is not an array. */ -/* - public function testPHPErrorOnInvalidSet() { + public function testSetThrowsOnInvalidInputType() { + $this->expectException('TypeError'); $accessList = new AccessList([]); $accessList->set('invalid'); - // Causes: "Illegal string offset 'allow_cid'" } -*/ + + public function testSetThrowsOnMissingKeysInArray() { + $this->expectException('Exception'); + $this->expectExceptionMessage('Invalid AccessList object'); + + $accessList = new AccessList([]); + $accessList->set(['something_else' => 'should_this_fail?']); + } /** * The set_from_array() function calls some other functions, too which are -- cgit v1.2.3