Completed
Pull Request — master (#133)
by Kenji
03:03 queued 27s
created

CIPHPUnitTestDbTestCase::hasInDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 8
rs 9.4285
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]);
62
			}
63
		}
64
	}
65
66
	//--------------------------------------------------------------------
67
	// Database Test Helpers
68
	//--------------------------------------------------------------------
69
70
	/**
71
	 * Asserts that records that match the conditions in $where do
72
	 * not exist in the database.
73
	 *
74
	 * @param string $table
75
	 * @param array  $where
76
	 *
77
	 * @return bool
78
	 */
79 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...
80
	{
81
		$this->db->from($table);
82
		$this->db->where($where);
83
		$count = $this->db->count_all_results();
84
85
		$this->assertTrue($count == 0, 'Row was found in database');
86
	}
87
	
88
	//--------------------------------------------------------------------
89
90
	/**
91
	 * Asserts that records that match the conditions in $where DO
92
	 * exist in the database.
93
	 * 
94
	 * @param string $table
95
	 * @param array  $where
96
	 *
97
	 * @return bool
98
	 */
99 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...
100
	{
101
		$this->db->from($table);
102
		$this->db->where($where);
103
		$count = $this->db->count_all_results();
104
105
		$this->assertTrue($count > 0, 'Row not found in database');
106
	}
107
108
	//--------------------------------------------------------------------
109
110
	/**
111
	 * Fetches a single column from a database row with criteria
112
	 * matching $where.
113
	 *
114
	 * @param string $table
115
	 * @param string $column
116
	 * @param array  $where
117
	 *
118
	 * @return bool
119
	 */
120
	public function grabFromDatabase($table, $column, array $where)
121
	{
122
		$this->db->select($column);
123
		$this->db->where($where);
124
		$query = $this->db->get($table);
125
		$row = $query->row();
126
127
		return isset($row->$column) ? $row->$column : false;
128
	}
129
	
130
	//--------------------------------------------------------------------
131
132
	/**
133
	 * Inserts a row into to the database. This row will be removed
134
	 * after the test has run.
135
	 *
136
	 * @param string $table
137
	 * @param array  $data
138
	 *
139
	 */
140
	public function hasInDatabase($table, array $data)
141
	{
142
		$this->insertCache[] = [
143
			$table, $data
144
		];
145
146
		$this->db->insert($table, $data);
147
	}
148
149
	//--------------------------------------------------------------------
150
151
	/**
152
	 * Asserts that the number of rows in the database that match $where
153
	 * is equal to $expected.
154
	 *
155
	 * @param int    $expected
156
	 * @param string $table
157
	 * @param array  $where
158
	 *
159
	 * @return bool
160
	 */
161
	public function seeNumRecords($expected, $table, array $where = [])
162
	{
163
		$this->db->from($table);
164
		$this->db->where($where);
165
		$count = $this->db->count_all_results();
166
167
		$this->assertEquals($expected, $count, 'Wrong number of matching rows in database.');
168
	}
169
	
170
	//--------------------------------------------------------------------
171
	
172
}
173