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

DAOTest::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 4

Duplication

Lines 18
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 18
loc 18
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Win\DAO;
4
5
use Win\Connection\MySQLTest;
6
use Win\Authentication\User;
7
use Win\Authentication\UserDAO;
8
9
/**
10
 * Este teste usa UserDAO como exemplo
11
 * Testa os metodos já existentes em abstract DAO
12
 */
13
class DAOTest extends \PHPUnit_Framework_TestCase {
14
15
	/** @var \PDO */
16
	private static $pdo;
17
18
	/** @var User */
19
	private $user;
20
21
	/** @var UserDAO */
22
	private $dao;
23
24 View Code Duplication
	public static function setUpBeforeClass() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
		static::$pdo = MySQLTest::startMySQLConnection();
0 ignored issues
show
Bug introduced by
Since $pdo is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $pdo to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
26
		static::$pdo->query('DROP TABLE ' . UserDAO::TABLE);
0 ignored issues
show
Bug introduced by
Since $pdo is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $pdo to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
27
		static::$pdo->query('CREATE TABLE `user` (
0 ignored issues
show
Bug introduced by
Since $pdo is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $pdo to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
28
					`id` int(11) NOT NULL AUTO_INCREMENT,
29
					`is_enabled` tinyint(1) NOT NULL,
30
					`access_level` smallint(6) NOT NULL,
31
					`group_id` int(11) NOT NULL,
32
					`name` varchar(45) NOT NULL,
33
					`email` varchar(45) NOT NULL,
34
					`password_hash` varchar(32) NOT NULL,
35
					`recovery_hash` varchar(32) DEFAULT NULL,
36
					`image` varchar(45) DEFAULT NULL,
37
					`last_login` datetime DEFAULT NULL,
38
					PRIMARY KEY(`id`)
39
				  );
40
		');
41
	}
42
43
	public function setUp() {
44
		static::$pdo->query('TRUNCATE TABLE ' . UserDAO::TABLE);
0 ignored issues
show
Bug introduced by
Since $pdo is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $pdo to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
45
	}
46
47 View Code Duplication
	private function insertExample() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
		$user = new User();
49
		$user->setEnabled(true);
50
		$user->setAccessLevel(User::ACCESS_ADMIN);
51
		$user->setName('My name');
52
		$user->setEmail('[email protected]');
53
		$user->setPassword('123456');
54
		$this->user = $user;
55
56
		$this->dao = new UserDAO();
57
		return $this->dao->save($user);
58
	}
59
60
	public function testInsert() {
61
		$error = $this->insertExample();
62
		$this->assertNull($error);
63
64
		$lastUser = $this->dao->fetchById($this->user->getId());
65
		$this->assertEquals($this->user->getName(), $lastUser->getName());
66
	}
67
68 View Code Duplication
	public function testUpdate() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
		$this->insertExample();
70
71
		$this->user->setName('Name Updated');
72
		$error = $this->dao->save($this->user);
73
		$this->assertNull($error);
74
75
		$lastUser = $this->dao->fetchById($this->user->getId());
76
		$this->assertEquals($this->user->getId(), $lastUser->getId());
77
	}
78
79
	public function testDelete() {
80
		$this->insertExample();
81
		$this->dao->delete($this->user);
82
83
		$lastUser = $this->dao->fetchById($this->user->getId());
84
		$this->assertEquals($lastUser->getId(), 0);
85
	}
86
87 View Code Duplication
	public function testDeleteById() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
		$this->insertExample();
89
		$this->dao->deleteById($this->user->getId());
90
91
		$lastUser = $this->dao->fetchById($this->user->getId());
92
		$this->assertEquals($lastUser->getId(), 0);
93
	}
94
95 View Code Duplication
	public function testValidate() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
		$this->insertExample();
97
		$this->user->setEmail('invalid');
98
		$this->dao->save($this->user);
99
100
		$lastUser = $this->dao->fetchById($this->user->getId());
101
		$this->assertEquals($lastUser->getEmail(), '[email protected]');
102
	}
103
104
	public function testFetch() {
105
		$this->insertExample();
106
		$lastUser = $this->dao->fetch(['name = ?' => 'My name']);
107
		$this->assertEquals($lastUser->getId(), $this->user->getId());
108
	}
109
110
	public function testFetchAll() {
111
		$this->dao = new UserDAO();
112
		$userList0 = $this->dao->fetchAll();
113
		$this->assertEquals(count($userList0), 0);
114
115
		/* insert 2 examples */
116
		$this->insertExample();
117
		$this->insertExample();
118
		$userList2 = $this->dao->fetchAll();
119
		$this->assertEquals(count($userList2), 2);
120
	}
121
122
	public function testNumRows() {
123
		$this->dao = new UserDAO();
124
		$numRows0 = $this->dao->numRows();
125
		$this->assertEquals($numRows0, 0);
126
127
		$this->insertExample();
128
		$numRows1 = $this->dao->numRows();
129
		$this->assertEquals($numRows1, 1);
130
	}
131
132 View Code Duplication
	public function testFoundWithFilter() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
		$this->insertExample();
134
		$filters = [
135
			'name = ?' => 'My name',
136
			'email = ?' => '[email protected]',
137
			'access_level > ?' => 0
138
		];
139
		$user = $this->dao->fetch($filters);
140
		$this->assertNotEquals($user->getId(), 0);
141
	}
142
143 View Code Duplication
	public function testNotFoundWithFilter() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
		$this->insertExample();
145
		$filters = [
146
			'name = ?' => 'Name doesnt exist',
147
			'email = ?' => '[email protected]',
148
			'access_level > ?' => 0
149
		];
150
		$user = $this->dao->fetch($filters);
151
		$this->assertEquals($user->getId(), 0);
152
	}
153
154
}
155