Completed
Push — master ( d07ffd...498d0f )
by Kenji
03:27
created

CIPHPUnitTestDbTestCase   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 164
Duplicated Lines 9.76 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 16
loc 164
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A loadDependencies() 0 9 2
A setUp() 0 4 1
A tearDown() 0 10 3
A reconnectDb() 0 9 1
A dontSeeInDatabase() 8 8 1
A seeInDatabase() 8 8 1
A grabFromDatabase() 0 9 2
A hasInDatabase() 0 8 1
A seeNumRecords() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Part of ci-phpunit-test
4
 *
5
 * @author     Kenji Suzuki <https://github.com/kenjis>
6
 * @license    MIT License
7
 * @copyright  2016 Kenji Suzuki
8
 * @link       https://github.com/kenjis/ci-phpunit-test
9
 */
10
11
/**
12
 * Copyright for Original Code
13
 * 
14
 * @author     CodeIgniter Dev Team
15
 * @copyright  Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
16
 * @license    http://opensource.org/licenses/MIT	MIT License
17
 * @link       http://codeigniter.com
18
 * 
19
 * @see        https://github.com/bcit-ci/CodeIgniter4/blob/59e1587a9875141586f8333ff9cc64cdae2173c4/system/Test/CIDatabaseTestCase.php
20
 */
21
22
class CIPHPUnitTestDbTestCase extends CIPHPUnitTestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
23
{
24
	protected $db;
25
26
	/**
27
	 * Stores information needed to remove any
28
	 * rows inserted via $this->hasInDatabase();
29
	 *
30
	 * @var array
31
	 */
32
	protected $insertCache = [];
33
34
	protected function loadDependencies()
35
	{
36
		if ($this->db === null)
37
		{
38
			$CI =& get_instance();
39
			$CI->load->database();
40
			$this->db = $CI->db;
41
		}
42
	}
43
44
	protected function setUp()
45
	{
46
		$this->loadDependencies();
47
	}
48
49
	//--------------------------------------------------------------------
50
51
	/**
52
	 * Takes care of any required cleanup after the test, like
53
	 * removing any rows inserted via $this->hasInDatabase()
54
	 */
55
	protected function tearDown()
56
	{
57
		if (! empty($this->insertCache))
58
		{
59
			foreach ($this->insertCache as $row)
60
			{
61
				$this->db->delete($row[0], $row[1]);
0 ignored issues
show
Bug introduced by
The method delete cannot be called on $this->db (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
62
			}
63
		}
64
	}
65
66
	/**
67
	 * Reconnect to the database
68
	 */
69
	public function reconnectDb()
70
	{
71
		$this->db->close();
72
		$this->db = null;
73
74
		$CI =& get_instance();
75
		$CI->load->database();
76
		$this->db = $this->CI->db;
77
	}
78
79
	//--------------------------------------------------------------------
80
	// Database Test Helpers
81
	//--------------------------------------------------------------------
82
83
	/**
84
	 * Asserts that records that match the conditions in $where do
85
	 * not exist in the database.
86
	 *
87
	 * @param string $table
88
	 * @param array  $where
89
	 *
90
	 * @return bool
91
	 */
92 View Code Duplication
	public function dontSeeInDatabase($table, array $where)
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...
93
	{
94
		$this->db->from($table);
0 ignored issues
show
Bug introduced by
The method from cannot be called on $this->db (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
95
		$this->db->where($where);
0 ignored issues
show
Bug introduced by
The method where cannot be called on $this->db (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
96
		$count = $this->db->count_all_results();
0 ignored issues
show
Bug introduced by
The method count_all_results cannot be called on $this->db (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
97
98
		$this->assertTrue($count == 0, 'Row was found in database');
99
	}
100
	
101
	//--------------------------------------------------------------------
102
103
	/**
104
	 * Asserts that records that match the conditions in $where DO
105
	 * exist in the database.
106
	 * 
107
	 * @param string $table
108
	 * @param array  $where
109
	 *
110
	 * @return bool
111
	 */
112 View Code Duplication
	public function seeInDatabase($table, array $where)
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...
113
	{
114
		$this->db->from($table);
0 ignored issues
show
Bug introduced by
The method from cannot be called on $this->db (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
115
		$this->db->where($where);
0 ignored issues
show
Bug introduced by
The method where cannot be called on $this->db (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
116
		$count = $this->db->count_all_results();
0 ignored issues
show
Bug introduced by
The method count_all_results cannot be called on $this->db (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
117
118
		$this->assertTrue($count > 0, 'Row not found in database');
119
	}
120
121
	//--------------------------------------------------------------------
122
123
	/**
124
	 * Fetches a single column from a database row with criteria
125
	 * matching $where.
126
	 *
127
	 * @param string $table
128
	 * @param string $column
129
	 * @param array  $where
130
	 *
131
	 * @return bool
132
	 */
133
	public function grabFromDatabase($table, $column, array $where)
134
	{
135
		$this->db->select($column);
0 ignored issues
show
Bug introduced by
The method select cannot be called on $this->db (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
136
		$this->db->where($where);
0 ignored issues
show
Bug introduced by
The method where cannot be called on $this->db (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
137
		$query = $this->db->get($table);
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->db (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
138
		$row = $query->row();
139
140
		return isset($row->$column) ? $row->$column : false;
141
	}
142
	
143
	//--------------------------------------------------------------------
144
145
	/**
146
	 * Inserts a row into to the database. This row will be removed
147
	 * after the test has run.
148
	 *
149
	 * @param string $table
150
	 * @param array  $data
151
	 *
152
	 */
153
	public function hasInDatabase($table, array $data)
154
	{
155
		$this->insertCache[] = [
156
			$table, $data
157
		];
158
159
		$this->db->insert($table, $data);
0 ignored issues
show
Bug introduced by
The method insert cannot be called on $this->db (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
160
	}
161
162
	//--------------------------------------------------------------------
163
164
	/**
165
	 * Asserts that the number of rows in the database that match $where
166
	 * is equal to $expected.
167
	 *
168
	 * @param int    $expected
169
	 * @param string $table
170
	 * @param array  $where
171
	 *
172
	 * @return bool
173
	 */
174
	public function seeNumRecords($expected, $table, array $where = [])
175
	{
176
		$this->db->from($table);
0 ignored issues
show
Bug introduced by
The method from cannot be called on $this->db (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
177
		$this->db->where($where);
0 ignored issues
show
Bug introduced by
The method where cannot be called on $this->db (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
178
		$count = $this->db->count_all_results();
0 ignored issues
show
Bug introduced by
The method count_all_results cannot be called on $this->db (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
179
180
		$this->assertEquals($expected, $count, 'Wrong number of matching rows in database.');
181
	}
182
	
183
	//--------------------------------------------------------------------
184
	
185
}
186