1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2014, Tobia De Koninck hey--at--ledfan.be |
4
|
|
|
* This file is licensed under the AGPL version 3 or later. |
5
|
|
|
* See the COPYING file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace OCA\Chat\OCH\Commands; |
9
|
|
|
|
10
|
|
|
include_once(__DIR__ . '/../../../autoloader.php'); |
11
|
|
|
include_once(__DIR__ . '/../../../vendor/Pimple/Pimple.php'); |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use OCA\Chat\Core\API; |
15
|
|
|
use OCA\Chat\OCH\Commands\SyncOnline; |
16
|
|
|
use OCA\Chat\App\Chat; |
17
|
|
|
use OCA\Chat\Db\DBException; |
18
|
|
|
use OCA\Chat\OCH\Exceptions\RequestDataInvalid; |
19
|
|
|
use OCA\Chat\OCH\Db\UserOnline; |
20
|
|
|
|
21
|
|
|
// DONE |
22
|
|
|
class SyncOnlineTest extends \PHPUnit_Framework_TestCase { |
23
|
|
|
|
24
|
|
|
public static $sessionIds; |
25
|
|
|
|
26
|
|
|
public function setUp(){ |
27
|
|
|
$app = new Chat(); |
|
|
|
|
28
|
|
|
$this->container = $app->getContainer(); |
|
|
|
|
29
|
|
|
$this->container['API'] = $this->getMockBuilder('\OCA\Chat\Core\API') |
30
|
|
|
->disableOriginalConstructor() |
31
|
|
|
->getMock(); |
32
|
|
|
$this->container['API']->expects($this->any()) |
33
|
|
|
->method('log') |
34
|
|
|
->will($this->returnValue(null)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Testcase: if there is a PDOException in the datamapper a DBException must be thrown |
39
|
|
|
* with the same message as in the PDOException |
40
|
|
|
*/ |
41
|
|
|
public function testDBFailure(){ |
42
|
|
|
$this->setExpectedException('\OCA\Chat\Db\DBException', 'Something went wrong with the DB!'); |
43
|
|
|
// config |
44
|
|
|
$this->container['API']->expects($this->any()) |
45
|
|
|
->method('prepareQuery') |
46
|
|
|
->will($this->throwException(new \PDOException('Something went wrong with the DB!'))); |
47
|
|
|
|
48
|
|
|
// logic |
49
|
|
|
$synConline = new SyncOnline($this->container); |
50
|
|
|
$synConline->setRequestData(array()); |
51
|
|
|
$result = $synConline->execute(); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testExecute(){ |
55
|
|
|
$session1 = new UserOnline(); |
56
|
|
|
$session1->setUser('admin'); |
57
|
|
|
$session1->setSessionId('session1id'); // must be deleted |
58
|
|
|
$session1->setLastOnline(time() - 200); |
59
|
|
|
|
60
|
|
|
$session2 = new UserOnline(); |
61
|
|
|
$session2->setUser('derp'); |
62
|
|
|
$session2->setSessionId('session2id'); // must be deleted |
63
|
|
|
$session2->setLastOnline(time() - 320); |
64
|
|
|
|
65
|
|
|
$session3 = new UserOnline(); |
66
|
|
|
$session3->setUser('derp'); |
67
|
|
|
$session3->setSessionId('session3id'); // must NOT be deleted |
68
|
|
|
$session3->setLastOnline(time() - 10); |
69
|
|
|
|
70
|
|
|
$this->container['UserOnlineMapper'] = $this->getMockBuilder('\OCA\Chat\OCH\Db\UserOnlineMapper') |
71
|
|
|
->disableOriginalConstructor() |
72
|
|
|
->getMock(); |
73
|
|
|
|
74
|
|
|
$this->container['UserOnlineMapper']->expects($this->any()) |
75
|
|
|
->method('getAll') |
76
|
|
|
->will($this->returnValue(array($session1, $session2, $session3))); |
77
|
|
|
|
78
|
|
|
$this->container['UserOnlineMapper']->expects($this->any()) |
79
|
|
|
->method('deleteBySessionId') |
80
|
|
|
->will($this->returnCallback(function($sessionId){ |
81
|
|
|
SyncOnlineTest::$sessionIds[] = $sessionId; |
82
|
|
|
})); |
83
|
|
|
|
84
|
|
|
$synConline = new SyncOnline($this->container); |
|
|
|
|
85
|
|
|
$synConline->setRequestData(array()); |
86
|
|
|
$result = $synConline->execute(); |
|
|
|
|
87
|
|
|
|
88
|
|
|
$this->assertEquals(array('session1id', 'session2id'), SyncOnlineTest::$sessionIds); |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.