Skip to content

Commit

Permalink
Add support for multi-valued attributes for XML, and add appropriate …
Browse files Browse the repository at this point in the history
…tests for XML and JSON
  • Loading branch information
phy25 authored and leo108 committed Apr 3, 2018
1 parent d4520e3 commit da17740
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/Responses/XmlAuthenticationSuccessResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ public function setAttributes($attributes)
$this->removeByXPath($authNode, 'cas:attributes');
$attributesNode = $authNode->addChild('cas:attributes');
foreach ($attributes as $key => $value) {
$str = $this->stringify($value);
if (is_string($str)) {
$attributesNode->addChild('cas:'.$key, $str);
$value_arr = (array) $value;
foreach($value_arr as $v){
$str = $this->stringify($v);
if (is_string($str)) {
$attributesNode->addChild('cas:'.$key, $str);
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions tests/Responses/JsonAuthenticationSuccessResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ public function testSetAttributes()
$this->assertEquals(['serviceResponse' => ['authenticationSuccess' => ['attributes' => $attr2]]], $data);
}

public function testSetMultiValuedAttributes()
{
$resp = new JsonAuthenticationSuccessResponse();
$attr1 = ['key1' => ['value1', 'value2']];
$resp->setAttributes($attr1);
$data = $this->getData($resp);
$this->assertEquals(['serviceResponse' => ['authenticationSuccess' => ['attributes' => $attr1]]], $data);
}

protected function getData(JsonAuthenticationSuccessResponse $resp)
{
$property = self::getNonPublicProperty($resp, 'data');
Expand Down
20 changes: 14 additions & 6 deletions tests/Responses/XmlAuthenticationSuccessResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ public function testSetProxies()

public function testSetAttributes()
{
$resp = Mockery::mock(XmlAuthenticationSuccessResponse::class, [])
->makePartial()
->shouldAllowMockingProtectedMethods()
->shouldReceive('stringify')
->andReturn('string')
->getMock();
$resp = new XmlAuthenticationSuccessResponse();
$content = $this->getXML($resp);
$this->assertNotContains('cas:attributes', $content);

Expand All @@ -76,6 +71,19 @@ public function testSetAttributes()
$this->assertContains('cas:key3', $content);
}

public function testSetMultiValuedAttributes()
{
$resp = new XmlAuthenticationSuccessResponse();
$content = $this->getXML($resp);
$this->assertNotContains('cas:attributes', $content);

$resp->setAttributes(['key1' => ['value1', 'value2']]);
$content = $this->getXML($resp);
$this->assertContains('cas:attributes', $content);
$this->assertContains('<cas:key1>value1</cas:key1>', $content);
$this->assertContains('<cas:key1>value2</cas:key1>', $content);
}

public function testSetProxyGrantingTicket()
{
$resp = new XmlAuthenticationSuccessResponse();
Expand Down

0 comments on commit da17740

Please sign in to comment.