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
|
<?php
/**
* Tests for SimpleSAML\Utils\Attributes.
*
* @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no>
*/
class Utils_AttributesTest extends PHPUnit_Framework_TestCase
{
/**
* Test the getExpectedAttribute() method with invalid attributes array.
*/
public function testGetExpectedAttributeInvalidAttributesArray()
{
// check with empty array as input
$attributes = 'string';
$expected = 'string';
$this->setExpectedException(
'InvalidArgumentException',
'The attributes array is not an array, it is: '.print_r($attributes, true).'.'
);
\SimpleSAML\Utils\Attributes::getExpectedAttribute($attributes, $expected);
}
/**
* Test the getExpectedAttributeMethod() method with invalid expected attribute parameter.
*/
public function testGetExpectedAttributeInvalidAttributeName()
{
// check with invalid attribute name
$attributes = array();
$expected = false;
$this->setExpectedException(
'InvalidArgumentException',
'The expected attribute is not a string, it is: '.print_r($expected, true).'.'
);
\SimpleSAML\Utils\Attributes::getExpectedAttribute($attributes, $expected);
}
/**
* Test the getExpectedAttributeMethod() method with a non-normalized attributes array.
*/
public function testGetExpectedAttributeNonNormalizedArray()
{
// check with non-normalized attributes array
$attributes = array(
'attribute' => 'value',
);
$expected = 'attribute';
$this->setExpectedException(
'InvalidArgumentException',
'The attributes array is not normalized, values should be arrays.'
);
\SimpleSAML\Utils\Attributes::getExpectedAttribute($attributes, $expected);
}
/**
* Test the getExpectedAttribute() method with valid input but missing expected attribute.
*/
public function testGetExpectedAttributeMissingAttribute()
{
// check missing attribute
$attributes = array(
'attribute' => array('value'),
);
$expected = 'missing';
$this->setExpectedException(
'SimpleSAML_Error_Exception',
"No such attribute '".$expected."' found."
);
\SimpleSAML\Utils\Attributes::getExpectedAttribute($attributes, $expected);
}
/**
* Test the getExpectedAttribute() method with an empty attribute.
*/
public function testGetExpectedAttributeEmptyAttribute()
{
// check empty attribute
$attributes = array(
'attribute' => array(),
);
$expected = 'attribute';
$this->setExpectedException(
'SimpleSAML_Error_Exception',
"Empty attribute '".$expected."'.'"
);
\SimpleSAML\Utils\Attributes::getExpectedAttribute($attributes, $expected);
}
/**
* Test the getExpectedAttributeMethod() method with multiple values (not being allowed).
*/
public function testGetExpectedAttributeMultipleValues()
{
// check attribute with more than value, that being not allowed
$attributes = array(
'attribute' => array(
'value1',
'value2',
),
);
$expected = 'attribute';
$this->setExpectedException(
'SimpleSAML_Error_Exception',
'More than one value found for the attribute, multiple values not allowed.'
);
\SimpleSAML\Utils\Attributes::getExpectedAttribute($attributes, $expected);
}
/**
* Test that the getExpectedAttribute() method successfully obtains values from the attributes array.
*/
public function testGetExpectedAttribute()
{
// check one value
$value = 'value';
$attributes = array(
'attribute' => array($value),
);
$expected = 'attribute';
$this->assertEquals($value, \SimpleSAML\Utils\Attributes::getExpectedAttribute($attributes, $expected));
// check multiple (allowed) values
$value = 'value';
$attributes = array(
'attribute' => array($value, 'value2', 'value3'),
);
$expected = 'attribute';
$this->assertEquals($value, \SimpleSAML\Utils\Attributes::getExpectedAttribute($attributes, $expected, true));
}
/**
* Test the normalizeAttributesArray() function with input not being an array
*
* @expectedException InvalidArgumentException
*/
public function testNormalizeAttributesArrayBadInput()
{
SimpleSAML\Utils\Attributes::normalizeAttributesArray('string');
}
/**
* Test the normalizeAttributesArray() function with an array with non-string attribute names.
*
* @expectedException InvalidArgumentException
*/
public function testNormalizeAttributesArrayBadKeys()
{
SimpleSAML\Utils\Attributes::normalizeAttributesArray(array('attr1' => 'value1', 1 => 'value2'));
}
/**
* Test the normalizeAttributesArray() function with an array with non-string attribute values.
*
* @expectedException InvalidArgumentException
*/
public function testNormalizeAttributesArrayBadValues()
{
SimpleSAML\Utils\Attributes::normalizeAttributesArray(array('attr1' => 'value1', 'attr2' => 0));
}
/**
* Test the normalizeAttributesArray() function.
*/
public function testNormalizeAttributesArray()
{
$attributes = array(
'key1' => 'value1',
'key2' => array('value2', 'value3'),
'key3' => 'value1'
);
$expected = array(
'key1' => array('value1'),
'key2' => array('value2', 'value3'),
'key3' => array('value1')
);
$this->assertEquals(
$expected,
SimpleSAML\Utils\Attributes::normalizeAttributesArray($attributes),
'Attribute array normalization failed'
);
}
}
|