1515use Leo108 \CAS \Models \Ticket ;
1616use Leo108 \CAS \Repositories \TicketRepository ;
1717use ReflectionClass ;
18+ use SerializableModel ;
1819use SimpleXMLElement ;
1920use TestCase ;
2021use Mockery ;
2122use User ;
2223
24+ function method_exists ($ obj , $ method )
25+ {
26+ return ValidateControllerTest::$ functions ->method_exists ($ obj , $ method );
27+ }
28+
2329class 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 );
0 commit comments