Skip to content

Commit c3f7bd9

Browse files
committed
add more tests && improve robustness
1 parent bfae73f commit c3f7bd9

File tree

4 files changed

+121
-10
lines changed

4 files changed

+121
-10
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"autoload-dev": {
2727
"classmap": [
2828
"tests/TestCase.php",
29-
"tests/_support/User.php"
29+
"tests/_support/User.php",
30+
"tests/_support/SerializableModel.php"
3031
]
3132
}
3233
}

src/Http/Controllers/ValidateController.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,21 @@ protected function successResponse($username, $attrs, $format)
151151
if (!empty($attrs)) {
152152
$childAttrs = $childSuccess->addChild('cas:attributes');
153153
foreach ($attrs as $key => $value) {
154-
$childAttrs->addChild('cas:'.$key, $value);
154+
if (is_string($value)) {
155+
$str = $value;
156+
} else if (is_object($value) && method_exists($value, '__toString')) {
157+
$str = $value->__toString();
158+
} else if ($value instanceof \Serializable) {
159+
$str = serialize($value);
160+
} else {
161+
//array or object that doesn't have __toString method
162+
//json_encode will return false if encode failed
163+
$str = json_encode($value);
164+
}
165+
166+
if (is_string($str)) {
167+
$childAttrs->addChild('cas:'.$key, $str);
168+
}
155169
}
156170
}
157171

tests/Http/Controllers/ValidateControllerTest.php

Lines changed: 84 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,25 @@
1515
use Leo108\CAS\Models\Ticket;
1616
use Leo108\CAS\Repositories\TicketRepository;
1717
use ReflectionClass;
18+
use SerializableModel;
1819
use SimpleXMLElement;
1920
use TestCase;
2021
use Mockery;
2122
use User;
2223

24+
function method_exists($obj, $method)
25+
{
26+
return ValidateControllerTest::$functions->method_exists($obj, $method);
27+
}
28+
2329
class ValidateControllerTest extends TestCase
2430
{
31+
public static $functions;
32+
2533
public function setUp()
2634
{
2735
parent::setUp();
36+
self::$functions = Mockery::mock();
2837
app()->instance(TicketLocker::class, Mockery::mock(TicketLocker::class));
2938
}
3039

@@ -386,12 +395,29 @@ function ($xml) {
386395
$method = self::getMethod($controller, 'successResponse');
387396
$this->assertEquals('returnXML called', $method->invokeArgs($controller, ['test_name', [], 'XML']));
388397

398+
$objWithToString = Mockery::mock()->shouldReceive('__toString')->andReturn('string from __toString');
399+
self::$functions
400+
->shouldReceive('method_exists')
401+
->with($objWithToString, '__toString')
402+
->andReturn(true)
403+
->shouldReceive('method_exists')
404+
->andReturn(false);
405+
406+
$attributes = [
407+
'string' => 'real_name',
408+
'simple_array' => [1, 2, 3],
409+
'kv_array' => ['key' => 'value'],
410+
'simple_object' => (object) ['key' => 'value'],
411+
'obj_with_to_string' => $objWithToString,
412+
'serializable' => new SerializableModel(),
413+
'resource' => fopen(__FILE__, 'a'),
414+
];
389415
$controller = Mockery::mock(ValidateController::class)
390416
->makePartial()
391417
->shouldAllowMockingProtectedMethods()
392418
->shouldReceive('returnXML')
393419
->andReturnUsing(
394-
function ($xml) {
420+
function ($xml) use ($attributes) {
395421
$this->assertInstanceOf(SimpleXMLElement::class, $xml);
396422
/* @var SimpleXMLElement $xml */
397423
$children = $xml->xpath('cas:authenticationSuccess');
@@ -401,19 +427,40 @@ function ($xml) {
401427
$this->assertEquals('test_name', $user[0]->__toString());
402428
$attr = $children[0]->xpath('cas:attributes');
403429
$this->assertCount(1, $attr);
404-
$rn = $attr[0]->xpath('cas:real_name');
405-
$this->assertCount(1, $rn);
406-
$this->assertEquals('real_name', $rn[0]->__toString());
430+
431+
$str = $attr[0]->xpath('cas:string');
432+
$this->assertCount(1, $str);
433+
$this->assertEquals($attributes['string'], $str[0]->__toString());
434+
435+
$str = $attr[0]->xpath('cas:simple_array');
436+
$this->assertCount(1, $str);
437+
$this->assertEquals(json_encode($attributes['simple_array']), $str[0]->__toString());
438+
439+
$str = $attr[0]->xpath('cas:kv_array');
440+
$this->assertCount(1, $str);
441+
$this->assertEquals(json_encode($attributes['kv_array']), $str[0]->__toString());
442+
443+
$str = $attr[0]->xpath('cas:simple_object');
444+
$this->assertCount(1, $str);
445+
$this->assertEquals(json_encode($attributes['simple_object']), $str[0]->__toString());
446+
447+
$str = $attr[0]->xpath('cas:obj_with_to_string');
448+
$this->assertCount(1, $str);
449+
$this->assertEquals($attributes['obj_with_to_string']->__toString(), $str[0]->__toString());
450+
451+
$str = $attr[0]->xpath('cas:serializable');
452+
$this->assertCount(1, $str);
453+
$this->assertEquals(serialize($attributes['serializable']), $str[0]->__toString());
454+
455+
$str = $attr[0]->xpath('cas:resource');
456+
$this->assertCount(0, $str);
407457

408458
return 'returnXML called';
409459
}
410460
)
411461
->getMock();
412462
$method = self::getMethod($controller, 'successResponse');
413-
$this->assertEquals(
414-
'returnXML called',
415-
$method->invokeArgs($controller, ['test_name', ['real_name' => 'real_name'], 'XML'])
416-
);
463+
$this->assertEquals('returnXML called', $method->invokeArgs($controller, ['test_name', $attributes, 'XML',]));
417464
}
418465

419466
public function testFailureResponse()
@@ -457,6 +504,35 @@ function ($xml) {
457504
$this->assertEquals('returnXML called', $method->invokeArgs($controller, ['code', 'desc', 'XML']));
458505
}
459506

507+
public function testRemoveXmlFirstLine()
508+
{
509+
$xml = new SimpleXMLElement(ValidateController::BASE_XML);
510+
$controller = Mockery::mock(ValidateController::class);
511+
$method = self::getMethod($controller, 'removeXmlFirstLine');
512+
$this->assertNotContains('<?xml version="1.0"?>', $method->invoke($controller, $xml->asXML()));
513+
514+
$normalStr = 'some string';
515+
$this->assertEquals($normalStr, $method->invoke($controller, $normalStr));
516+
}
517+
518+
public function testReturnXML()
519+
{
520+
$xml = new SimpleXMLElement(ValidateController::BASE_XML);
521+
$controller = Mockery::mock(ValidateController::class)
522+
->makePartial()
523+
->shouldAllowMockingProtectedMethods()
524+
->shouldReceive('removeXmlFirstLine')
525+
->andReturn('parsed string')
526+
->getMock();
527+
528+
$method = self::getMethod($controller, 'returnXML');
529+
$resp = $method->invoke($controller, $xml);
530+
$this->assertInstanceOf(Response::class, $resp);
531+
$this->assertEquals(200, $resp->getStatusCode());
532+
$this->assertTrue($resp->headers->has('Content-Type'));
533+
$this->assertEquals('application/xml', $resp->headers->get('Content-Type'));
534+
}
535+
460536
protected static function getMethod($obj, $name)
461537
{
462538
$class = new ReflectionClass($obj);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/**
4+
* Created by PhpStorm.
5+
* User: leo108
6+
* Date: 2016/10/15
7+
* Time: 11:26
8+
*/
9+
class SerializableModel implements Serializable
10+
{
11+
public function serialize()
12+
{
13+
return 'serialized str';
14+
}
15+
16+
public function unserialize($serialized)
17+
{
18+
// TODO: Implement unserialize() method.
19+
}
20+
}

0 commit comments

Comments
 (0)