MessageMapperTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
c 3
b 0
f 0
lcom 1
cbo 5
dl 0
loc 113
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
B messageProvider() 0 27 2
A testGetMessagesByConvIdWitStartPoint() 0 15 3
A testGetMessagesByConvIdWithoutStartPoint() 0 15 3
A tearDown() 0 7 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: tobis
5
 * Date: 8/19/14
6
 * Time: 2:50 PM
7
 */
8
9
namespace OCA\Chat\OCH\Db;
10
11
use OCA\Chat\App\Container;
12
13
14
class MessageMapperTest extends \PHPUnit_Framework_TestCase {
15
16
	/**
17
	 * @var \OCA\Chat\App\Container
18
	 */
19
	public $container;
20
21
	/**
22
	 * @var \OCA\Chat\OCH\Db\MessageMapper
23
	 */
24
	public $messageMapper;
25
26
	/**
27
	 * @var \OCA\Chat\OCH\Db\UserMapper
28
	 */
29
	public $userMapper;
30
31
32
	public function setUp(){
33
		$this->container = new Container();
34
		$this->messageMapper = $this->container->query('MessageMapper');
35
		$this->userMapper = $this->container->query('UserMapper');
36
	}
37
38
	public function messageProvider(){
39
		$msgs = array();
40
		$users = array('foo', 'bar', 'foobar');
41
		$convId = md5(time());
42
43
		for ($i =0; $i < 20; $i++){
44
			$msg = new Message();
45
			$rand = rand(0,2);
46
			$msg->setUser($users[$rand]);
47
			$msg->setConvid($convId);
48
			$msg->setMessage('Test Message');
49
			$msg->setTimestamp($i);
50
			$msgs[] = $msg;
51
		}
52
53
		$user = new User();
54
		$user->setConversationId($convId);
55
		$user->setUser('foo');
56
		$user->setJoined(5);
57
		return array(
58
			array(
59
				$msgs,
60
				$convId,
61
				$user
62
			)
63
		);
64
	}
65
66
	/**
67
	 * Test if only messages are received send later than we joined AND later than $startpoint = 8
68
	 * @dataProvider messageProvider
69
	 * @param $msgs array()
70
	 * @param $convId string
71
	 * @param $user \OCA\Chat\OCH\Db\User;
72
	 */
73
	public function testGetMessagesByConvIdWitStartPoint($msgs, $convId, $user){
74
		foreach($msgs as $msg){
75
			$this->messageMapper->insert($msg);
76
		}
77
		$this->userMapper->insert($user);
78
79
80
		$result = $this->messageMapper->getMessagesByConvId($convId, 'foo', 8);
81
82
		$this->assertEquals(11, count($result)); // we set startpoint at time "8"
83
84
		foreach($result as $r){
85
			$this->assertGreaterThan(8, $r->getTimestamp());
86
		}
87
	}
88
89
	/**
90
	 * Test if only messages are received send later than we joined
91
	 * @dataProvider messageProvider
92
	 * @param $msgs array()
93
	 * @param $convId string
94
	 * @param $user \OCA\Chat\OCH\Db\User;
95
	 */
96
	public function testGetMessagesByConvIdWithoutStartPoint($msgs, $convId, $user){
97
		foreach($msgs as $msg){
98
			$this->messageMapper->insert($msg);
99
		}
100
		$this->userMapper->insert($user);
101
102
103
		$result = $this->messageMapper->getMessagesByConvId($convId, 'foo');
104
105
		$this->assertEquals(14, count($result)); // We joined at time "6", messages start at time "0" and there are 20 messages
106
107
		foreach($result as $r){
108
			$this->assertGreaterThan(5, $r->getTimestamp());
109
		}
110
	}
111
112
113
	/**
114
	 * Remove all records from the table so future test can run without problems
115
	 */
116
	public function tearDown(){
117
		$query = \OCP\DB::prepare('DELETE FROM `' . $this->messageMapper->getTableName() . '`');
118
		$query->execute(array());
119
120
		$query = \OCP\DB::prepare('DELETE FROM `' . $this->userMapper->getTableName() . '`');
121
		$query->execute(array());
122
	}
123
	
124
125
126
}