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
|
<?php
namespace OCA\OJSXC\Db;
use OCA\OJSXC\Utility\MapperTestUtility;
use OCP\AppFramework\Db\DoesNotExistException;
function uniqid() {
return 4; // chosen by fair dice roll.
// guaranteed to be unique.
}
/**
* @group DB
*/
class MessageMapperTest extends MapperTestUtility {
/**
* @var StanzaMapper
*/
protected $mapper;
protected function setUp() {
$this->entityName = 'OCA\OJSXC\Db\Message';
$this->mapperName = 'MessageMapper';
parent::setUp();
}
public function insertProvider() {
return [
[
'john@localhost',
'thomas@localhost',
'abcd',
'test',
'Test Message',
'<message to="thomas@localhost" from="john@localhost" type="test" xmlns="jabber:client" id="4-msg">Test Message</message>'
]
];
}
/**
* @dataProvider insertProvider
*/
public function testInsert($from, $to, $data, $type, $msg, $expectedStanza) {
$stanza = new Message();
$stanza->setFrom($from);
$stanza->setTo($to);
$stanza->setStanza($data);
$stanza->setType($type);
$stanza->setValue($msg);
$this->assertEquals($stanza->getFrom(), $from);
$this->assertEquals($stanza->getTo(), $to);
$this->assertEquals($stanza->getStanza(), $data);
$this->assertEquals($stanza->getType(), $type);
$this->mapper->insert($stanza);
$result = $this->fetchAll();
$this->assertCount(1, $result);
$this->assertEquals($stanza->getFrom(), $result[0]->getFrom());
$this->assertEquals($stanza->getTo(), $result[0]->getTo());
$this->assertEquals($expectedStanza, $result[0]->getStanza());
$this->assertEquals(null, $result[0]->getType()); // type is saved into the XML string, not the DB.
}
/**
* @expectedException \OCP\AppFramework\Db\DoesNotExistException
*/
public function testFindByToNotFound() {
$this->mapper->findByTo('test');
}
/**
* @expectedException \OCP\AppFramework\Db\DoesNotExistException
*/
public function testFindByToNotFound2() {
$stanza = new Message();
$stanza->setFrom('john@localhost');
$stanza->setTo('john@localhost');
$stanza->setStanza('abcd');
$stanza->setType('test');
$stanza->setValue('message abc');
$this->mapper->insert($stanza);
$this->mapper->findByTo('test');
}
public function testFindByToFound() {
$stanza1 = new Message();
$stanza1->setFrom('jan@localhost');
$stanza1->setTo('john@localhost');
$stanza1->setStanza('abcd1');
$stanza1->setType('test');
$stanza1->setValue('Messageabc');
$this->mapper->insert($stanza1);
$stanza2 = new Message();
$stanza2->setFrom('thomas@localhost');
$stanza2->setTo('jan@localhost');
$stanza2->setStanza('abcd2');
$stanza2->setType('test2');
$stanza2->setValue('Message');
$this->mapper->insert($stanza2);
// check if two elements are inserted
$result = $this->fetchAll();
$this->assertCount(2, $result);
// check findByTo
$result = $this->mapper->findByTo('john@localhost');
$this->assertCount(1, $result);
$this->assertEquals('<message to="john@localhost" from="jan@localhost" type="test" xmlns="jabber:client" id="4-msg">Messageabc</message>', $result[0]->getStanza());
// check if element is deleted
$result = $this->fetchAll();
$this->assertCount(1, $result);
$this->assertEquals($stanza2->getFrom(), $result[0]->getFrom());
$this->assertEquals($stanza2->getTo(), $result[0]->getTo());
$this->assertEquals('<message to="jan@localhost" from="thomas@localhost" type="test2" xmlns="jabber:client" id="4-msg">Message</message>', $result[0]->getStanza());
}
}
|