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("", null, null, 'unavailable', 'admin'),
$this->generateFactoryData("", null, null, 'unavailable', 'admin'),
$this->generateFactoryData("", null, null, 'online', 'admin'),
$this->generateFactoryData("ack", null, null, 'online', 'admin'),
$this->generateFactoryData("", null, null, 'online', 'admin'),
$this->generateFactoryData("chat", null, null, 'chat', 'admin'),
$this->generateFactoryData("dnd", null, null, 'dnd', 'admin'),
$this->generateFactoryData("ea", null, null, 'ea', 'admin'),
$this->generateFactoryData("away", null, null, 'away', 'admin'),
$this->generateFactoryData("online", 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@own.dev', 'derp@own.dev', 'chat', '
chat'),
$this->generateSerializeData('admin@own.dev', 'derp@own.dev', 'online', ''),
$this->generateSerializeData('admin@own.dev', 'derp@own.dev', 'away', 'away'),
$this->generateSerializeData('admin@own.dev', 'derp@own.dev', 'unavailable', ''),
$this->generateSerializeData('admin@own.dev', 'derp@own.dev', 'ea', 'ea'),
$this->generateSerializeData('admin@own.dev', 'derp@own.dev', 'dnd', 'dnd'),
];
}
/**
* @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);
}
}