1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
<?php
declare(strict_types=1);
namespace SimpleSAML\Test\Utils;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Error;
use SimpleSAML\Utils\Attributes;
/**
* Tests for SimpleSAML\Utils\Attributes.
*
* @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no>
*/
class AttributesTest extends TestCase
{
/**
* Test the getExpectedAttribute() method with invalid attributes array.
* @return void
* @psalm-suppress InvalidArgument
* @deprecated Can be removed as soon as the codebase is fully typehinted
*/
public function testGetExpectedAttributeInvalidAttributesArray()
{
// check with empty array as input
$attributes = 'string';
$expected = 'string';
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'The attributes array is not an array, it is: ' . print_r($attributes, true) . '.'
);
Attributes::getExpectedAttribute($attributes, $expected);
}
/**
* Test the getExpectedAttributeMethod() method with invalid expected attribute parameter.
* @deprecated Remove this test as soon as the codebase is fully typehinted
* @psalm-suppress PossiblyFalseArgument
* @return void
*/
public function testGetExpectedAttributeInvalidAttributeName()
{
// check with invalid attribute name
$attributes = [];
$expected = false;
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'The expected attribute is not a string, it is: ' . print_r($expected, true) . '.'
);
Attributes::getExpectedAttribute($attributes, $expected);
}
/**
* Test the getExpectedAttributeMethod() method with a non-normalized attributes array.
* @return void
*/
public function testGetExpectedAttributeNonNormalizedArray(): void
{
// check with non-normalized attributes array
$attributes = [
'attribute' => 'value',
];
$expected = 'attribute';
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'The attributes array is not normalized, values should be arrays.'
);
Attributes::getExpectedAttribute($attributes, $expected);
}
/**
* Test the getExpectedAttribute() method with valid input but missing expected attribute.
* @return void
*/
public function testGetExpectedAttributeMissingAttribute(): void
{
// check missing attribute
$attributes = [
'attribute' => ['value'],
];
$expected = 'missing';
$this->expectException(Error\Exception::class);
$this->expectExceptionMessage("No such attribute '" . $expected . "' found.");
Attributes::getExpectedAttribute($attributes, $expected);
}
/**
* Test the getExpectedAttribute() method with an empty attribute.
* @return void
*/
public function testGetExpectedAttributeEmptyAttribute(): void
{
// check empty attribute
$attributes = [
'attribute' => [],
];
$expected = 'attribute';
$this->expectException(Error\Exception::class);
$this->expectExceptionMessage("Empty attribute '" . $expected . "'.'");
Attributes::getExpectedAttribute($attributes, $expected);
}
/**
* Test the getExpectedAttributeMethod() method with multiple values (not being allowed).
* @return void
*/
public function testGetExpectedAttributeMultipleValues(): void
{
// check attribute with more than value, that being not allowed
$attributes = [
'attribute' => [
'value1',
'value2',
],
];
$expected = 'attribute';
$this->expectException(Error\Exception::class);
$this->expectExceptionMessage(
'More than one value found for the attribute, multiple values not allowed.'
);
Attributes::getExpectedAttribute($attributes, $expected);
}
/**
* Test that the getExpectedAttribute() method successfully obtains values from the attributes array.
* @return void
*/
public function testGetExpectedAttribute(): void
{
// check one value
$value = 'value';
$attributes = [
'attribute' => [$value],
];
$expected = 'attribute';
$this->assertEquals($value, Attributes::getExpectedAttribute($attributes, $expected));
// check multiple (allowed) values
$value = 'value';
$attributes = [
'attribute' => [$value, 'value2', 'value3'],
];
$expected = 'attribute';
$this->assertEquals($value, Attributes::getExpectedAttribute($attributes, $expected, true));
}
/**
* Test the normalizeAttributesArray() function with input not being an array
* @return void
* @psalm-suppress InvalidArgument
* @deprecated Can be removed as soon as the codebase is fully typehinted
*/
public function testNormalizeAttributesArrayBadInput()
{
$this->expectException(InvalidArgumentException::class);
Attributes::normalizeAttributesArray('string');
}
/**
* Test the normalizeAttributesArray() function with an array with non-string attribute names.
* @return void
*/
public function testNormalizeAttributesArrayBadKeys(): void
{
$this->expectException(InvalidArgumentException::class);
Attributes::normalizeAttributesArray(['attr1' => 'value1', 1 => 'value2']);
}
/**
* Test the normalizeAttributesArray() function with an array with non-string attribute values.
* @return void
*/
public function testNormalizeAttributesArrayBadValues(): void
{
$this->expectException(InvalidArgumentException::class);
Attributes::normalizeAttributesArray(['attr1' => 'value1', 'attr2' => 0]);
}
/**
* Test the normalizeAttributesArray() function.
* @return void
*/
public function testNormalizeAttributesArray(): void
{
$attributes = [
'key1' => 'value1',
'key2' => ['value2', 'value3'],
'key3' => 'value1'
];
$expected = [
'key1' => ['value1'],
'key2' => ['value2', 'value3'],
'key3' => ['value1']
];
$this->assertEquals(
$expected,
Attributes::normalizeAttributesArray($attributes),
'Attribute array normalization failed'
);
}
/**
* Test the getAttributeNamespace() function.
* @return void
*/
public function testNamespacedAttributes(): void
{
// test for only the name
$this->assertEquals(
['default', 'name'],
Attributes::getAttributeNamespace('name', 'default')
);
// test for a given namespace and multiple '/'
$this->assertEquals(
['some/namespace', 'name'],
Attributes::getAttributeNamespace('some/namespace/name', 'default')
);
}
}
|