|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: leo108 |
|
5
|
|
|
* Date: 2016/10/25 |
|
6
|
|
|
* Time: 15:12 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Leo108\CAS\Responses; |
|
10
|
|
|
|
|
11
|
|
|
use Mockery; |
|
12
|
|
|
use SerializableModel; |
|
13
|
|
|
use SimpleXMLElement; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
15
|
|
|
use TestCase; |
|
16
|
|
|
|
|
17
|
|
|
function method_exists($obj, $method) |
|
18
|
|
|
{ |
|
19
|
|
|
return BaseXmlResponseTest::$functions->method_exists($obj, $method); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
class BaseXmlResponseTest extends TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
protected $testObj; |
|
25
|
|
|
public static $functions; |
|
26
|
|
|
|
|
27
|
|
|
public function setUp() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->testObj = new BaseXmlResponse(); |
|
30
|
|
|
self::$functions = Mockery::mock(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testStringify() |
|
34
|
|
|
{ |
|
35
|
|
|
$method = self::getNonPublicMethod($this->testObj, 'stringify'); |
|
36
|
|
|
|
|
37
|
|
|
$objWithToString = Mockery::mock()->shouldReceive('__toString')->andReturn('string from __toString'); |
|
38
|
|
|
self::$functions |
|
|
|
|
|
|
39
|
|
|
->shouldReceive('method_exists') |
|
40
|
|
|
->with($objWithToString, '__toString') |
|
41
|
|
|
->andReturn(true) |
|
42
|
|
|
->shouldReceive('method_exists') |
|
43
|
|
|
->andReturn(false); |
|
44
|
|
|
$serializableModel = new SerializableModel(); |
|
45
|
|
|
$resource = fopen(__FILE__, 'a'); |
|
46
|
|
|
$this->assertEquals('string', $method->invoke($this->testObj, 'string')); |
|
47
|
|
|
$this->assertEquals(json_encode([1, 2, 3]), $method->invoke($this->testObj, [1, 2, 3])); |
|
48
|
|
|
$this->assertEquals(json_encode(['key' => 'value']), $method->invoke($this->testObj, ['key' => 'value'])); |
|
49
|
|
|
$this->assertEquals( |
|
50
|
|
|
json_encode(['key' => 'value']), |
|
51
|
|
|
$method->invoke($this->testObj, (object) ['key' => 'value']) |
|
52
|
|
|
); |
|
53
|
|
|
$this->assertEquals($objWithToString->__toString(), $method->invoke($this->testObj, $objWithToString)); |
|
54
|
|
|
$this->assertEquals(serialize($serializableModel), $method->invoke($this->testObj, $serializableModel)); |
|
55
|
|
|
$this->assertFalse($method->invoke($this->testObj, $resource)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testRemoveXmlFirstLine() |
|
59
|
|
|
{ |
|
60
|
|
|
$xml = new SimpleXMLElement('<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas"/>'); |
|
61
|
|
|
$method = self::getNonPublicMethod($this->testObj, 'removeXmlFirstLine'); |
|
62
|
|
|
$this->assertNotContains('<?xml version="1.0"?>', $method->invoke($this->testObj, $xml->asXML())); |
|
63
|
|
|
|
|
64
|
|
|
$normalStr = 'some string'; |
|
65
|
|
|
$this->assertEquals($normalStr, $method->invoke($this->testObj, $normalStr)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testRemoveByXPath() |
|
69
|
|
|
{ |
|
70
|
|
|
$xml = new SimpleXMLElement('<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas"/>'); |
|
71
|
|
|
$xml->addChild('cas:tag', '123'); |
|
72
|
|
|
$this->assertContains('cas:tag', $xml->asXML()); |
|
73
|
|
|
$this->assertContains('123', $xml->asXML()); |
|
74
|
|
|
$method = self::getNonPublicMethod($this->testObj, 'removeByXPath'); |
|
75
|
|
|
$method->invoke($this->testObj, $xml, 'cas:tag'); |
|
76
|
|
|
$this->assertNotContains('cas:tag', $xml->asXML()); |
|
77
|
|
|
$this->assertNotContains('123', $xml->asXML()); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testGetRootNode() |
|
81
|
|
|
{ |
|
82
|
|
|
$method = self::getNonPublicMethod($this->testObj, 'getRootNode'); |
|
83
|
|
|
$this->assertContains( |
|
84
|
|
|
'<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas"/>', |
|
85
|
|
|
$method->invoke($this->testObj)->asXML() |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testToResponse() |
|
90
|
|
|
{ |
|
91
|
|
|
$resp = Mockery::mock(BaseXmlResponse::class, []) |
|
92
|
|
|
->makePartial() |
|
93
|
|
|
->shouldAllowMockingProtectedMethods() |
|
94
|
|
|
->shouldReceive('removeXmlFirstLine') |
|
95
|
|
|
->andReturn('some string') |
|
96
|
|
|
->getMock(); |
|
97
|
|
|
|
|
98
|
|
|
$ret = $resp->toResponse(); |
|
99
|
|
|
$this->assertInstanceOf(Response::class, $ret); |
|
100
|
|
|
$this->assertEquals(200, $ret->getStatusCode()); |
|
101
|
|
|
$this->assertEquals('some string', $ret->getContent()); |
|
102
|
|
|
$this->assertEquals('application/xml', $ret->headers->get('Content-Type')); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.