SyncOnlineTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A testDBFailure() 0 12 1
B testExecute() 0 37 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 22 and the first side effect is on line 10.

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.

Loading history...
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();
0 ignored issues
show
Bug introduced by
The call to Chat::__construct() misses some required arguments starting with $backendManager.
Loading history...
28
		$this->container = $app->getContainer();
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The method getContainer() does not seem to exist on object<OCA\Chat\App\Chat>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $synConline->execute() (which targets OCA\Chat\OCH\Commands\SyncOnline::execute()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
$this->container is of type array<string,?,{"UserOnlineMapper":"?"}>, but the function expects a object<OCA\Chat\OCH\Db\UserOnlineMapper>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
		$synConline->setRequestData(array());
86
		$result = $synConline->execute();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $synConline->execute() (which targets OCA\Chat\OCH\Commands\SyncOnline::execute()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
87
		
88
		$this->assertEquals(array('session1id', 'session2id'), SyncOnlineTest::$sessionIds);		
89
90
	}
91
}