Completed
Push — master ( 659231...788af6 )
by Wanderson
05:06
created

MySQLTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testPdoIsNullOnErrorConnection() 0 12 1
A testRedirectToPage500() 0 15 1
A testConnectionWithSuccess() 0 4 1
A startMySQLConnection() 0 6 1
1
<?php
2
3
namespace Win\Connection;
4
5
use Win\Helper\Url;
6
use Win\Mvc\Application;
7
8
class MySQLTest extends \PHPUnit_Framework_TestCase {
9
10
	public function testPdoIsNullOnErrorConnection() {
11
		$dbConfig = [
12
			'host' => 'localhost',
13
			'user' => 'no-name',
14
			'pass' => 'no-pass',
15
			'dbname' => 'database-doesnt-exist'
16
		];
17
18
		new Application();
19
		$db = new MySQL($dbConfig);
20
		$this->assertNull($db->getPDO());
21
	}
22
23
	public function testRedirectToPage500() {
24
		$dbConfig = [
25
			'host' => 'localhost',
26
			'user' => 'no-name',
27
			'pass' => 'no-pass',
28
			'dbname' => 'database-doesnt-exist'
29
		];
30
31
		Url::instance()->setUrl('my-page/test');
32
		$app = new Application();
33
		$this->assertEquals($app->getPage(), 'my-page');
34
35
		new MySQL($dbConfig);
36
		$this->assertEquals($app->getPage(), '503');
37
	}
38
39
	public function testConnectionWithSuccess() {
40
		static::startMySQLConnection();
41
		$this->assertTrue(MySQL::instance()->getPDO() instanceof \PDO);
42
	}
43
44
	/**
45
	 * @return \PDO
46
	 */
47
	static function startMySQLConnection() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
48
		require BASE_PATH . '/app/config/database.php';
49
		$db['dbname'] = 'win_test';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$db was never initialized. Although not strictly required by PHP, it is generally a good practice to add $db = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
50
		$mysql = new MySQL($db);
51
		return $mysql->getPDO();
52
	}
53
54
}
55