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
|
<?php
namespace OCA\OJSXC\Db;
use Sabre\Xml\Reader;
use Sabre\Xml\Writer;
use Sabre\Xml\LibXMLException;
use Sabre\Xml\ParseException;
use OCA\OJSXC\Utility\TestCase;
class PresenceTest extends TestCase
{
private function generateFactoryData($xml, $from, $to, $presence, $userId)
{
$reader = new Reader();
$reader->xml($xml);
$reader->elementMap = [
'{jabber:client}presence' => function (Reader $reader) use ($userId) {
return Presence::createFromXml($reader, $userId);
}
];
$expected = new Presence();
$expected->setFrom($from);
$expected->setTo($to);
$expected->setPresence($presence);
$expected->setUserid($userId);
return [
$reader,
$userId,
$expected
];
}
public function factoryProvider()
{
return [
$this->generateFactoryData("<presence xmlns='jabber:client' type='unavailable'/>", null, null, 'unavailable', 'admin'),
$this->generateFactoryData("<presence xmlns='jabber:client' type='unavailable'></presence>", null, null, 'unavailable', 'admin'),
$this->generateFactoryData("<presence xmlns='jabber:client'/>", null, null, 'online', 'admin'),
$this->generateFactoryData("<presence xmlns='jabber:client'><test>ack</test></presence>", null, null, 'online', 'admin'),
$this->generateFactoryData("<presence xmlns='jabber:client'></presence>", null, null, 'online', 'admin'),
$this->generateFactoryData("<presence xmlns='jabber:client'><show>chat</show></presence>", null, null, 'chat', 'admin'),
$this->generateFactoryData("<presence xmlns='jabber:client'><show>dnd</show></presence>", null, null, 'dnd', 'admin'),
$this->generateFactoryData("<presence xmlns='jabber:client'><show>ea</show></presence>", null, null, 'ea', 'admin'),
$this->generateFactoryData("<presence xmlns='jabber:client'><show>away</show></presence>", null, null, 'away', 'admin'),
$this->generateFactoryData("<presence xmlns='jabber:client'><show>online</show></presence>", null, null, 'online', 'admin'),
];
}
/**
* @dataProvider factoryProvider
*/
public function testFactory($reader, $userid, Presence $expectedElement)
{
$result = $reader->parse();
$result = $result['value'];
$this->assertEquals($expectedElement->getTo(), $result->getTo());
$this->assertEquals($expectedElement->getFrom(), $result->getFrom());
$this->assertEquals($expectedElement->getPresence(), $result->getPresence());
$this->assertEquals($expectedElement->getUserid(), $result->getUserid());
}
private function generateSerializeData($to, $from, $presence, $expected)
{
$writer = new Writer();
$writer->openMemory();
$writer->startElement('body');
$writer->writeAttribute('xmlns', 'http://jabber.org/protocol/httpbind');
$presenceEntity = new Presence();
$presenceEntity->setPresence($presence);
$presenceEntity->setFrom($from);
$presenceEntity->setTo($to);
return [
$writer,
$presenceEntity,
$expected,
$to,
$from,
$presence
];
}
public function serializeProvider()
{
return [
$this->generateSerializeData(
'admin',
'derp',
'chat',
'<body xmlns="http://jabber.org/protocol/httpbind"><presence from="derp" to="admin" xmlns="jabber:client"><show>chat</show></presence></body>'
),
$this->generateSerializeData(
'admin',
'derp',
'online',
'<body xmlns="http://jabber.org/protocol/httpbind"><presence from="derp" to="admin" xmlns="jabber:client" /></body>'
),
$this->generateSerializeData(
'admin',
'derp',
'away',
'<body xmlns="http://jabber.org/protocol/httpbind"><presence from="derp" to="admin" xmlns="jabber:client"><show>away</show></presence></body>'
),
$this->generateSerializeData(
'admin',
'derp',
'unavailable',
'<body xmlns="http://jabber.org/protocol/httpbind"><presence from="derp" to="admin" xmlns="jabber:client" type="unavailable"/></body>'
),
$this->generateSerializeData(
'admin',
'derp',
'ea',
'<body xmlns="http://jabber.org/protocol/httpbind"><presence from="derp" to="admin" xmlns="jabber:client"><show>ea</show></presence></body>'
),
$this->generateSerializeData(
'admin',
'derp',
'dnd',
'<body xmlns="http://jabber.org/protocol/httpbind"><presence from="derp" to="admin" xmlns="jabber:client"><show>dnd</show></presence></body>'
),
];
}
/**
* @dataProvider serializeProvider
*/
public function testSerialize(Writer $writer, Presence $presenceEntity, $expected, $to, $from, $presence)
{
$writer->write($presenceEntity);
$writer->endElement();
$result = $writer->outputMemory();
$this->assertEquals($to, $presenceEntity->getTo());
$this->assertEquals($from, $presenceEntity->getFrom());
$this->assertEquals($presence, $presenceEntity->getPresence());
$this->assertSabreXmlEqualsXml($expected, $result);
}
}
|