Skip to content

Commit 32afb4b

Browse files
Add grabEntryFromDatabase and grabEntriesFromDatabase methods
* feat: mysql helper to grab entire rows * fix: use semantic assertions * feat: grabEntryFromDatabase makes the test fail if no row is found * misc: code style * Fix syntax error made while resolving conflict Co-authored-by: Gintautas Miselis <[email protected]>
1 parent d0554b7 commit 32afb4b

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

src/Codeception/Module/Db.php

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ public function _loadDump(string $databaseKey = null, array $databaseConfig = nu
729729
$databaseKey = empty($databaseKey) ? self::DEFAULT_DATABASE : $databaseKey;
730730
$databaseConfig = empty($databaseConfig) ? $this->config : $databaseConfig;
731731

732-
if ($databaseConfig['populator']) {
732+
if (!empty($databaseConfig['populator'])) {
733733
$this->loadDumpUsingPopulator($databaseKey, $databaseConfig);
734734
return;
735735
}
@@ -956,6 +956,77 @@ public function grabFromDatabase(string $table, string $column, array $criteria
956956
return $this->proceedSeeInDatabase($table, $column, $criteria);
957957
}
958958

959+
/**
960+
* Fetches a whole entry from a database.
961+
* Make the test fail if the entry is not found.
962+
* Provide table name, desired column and criteria.
963+
*
964+
* ``` php
965+
* <?php
966+
* $mail = $I->grabEntryFromDatabase('users', array('name' => 'Davert'));
967+
* ```
968+
* Comparison expressions can be used as well:
969+
*
970+
* ```php
971+
* <?php
972+
* $post = $I->grabEntryFromDatabase('posts', ['num_comments >=' => 100]);
973+
* $user = $I->grabEntryFromDatabase('users', ['email like' => 'miles%']);
974+
* ```
975+
*
976+
* Supported operators: `<`, `>`, `>=`, `<=`, `!=`, `like`.
977+
*
978+
* @return array Returns a single entry value
979+
* @throws PDOException|Exception
980+
*/
981+
public function grabEntryFromDatabase(string $table, array $criteria = [])
982+
{
983+
$query = $this->_getDriver()->select('*', $table, $criteria);
984+
$parameters = array_values($criteria);
985+
$this->debugSection('Query', $query);
986+
$this->debugSection('Parameters', $parameters);
987+
$sth = $this->_getDriver()->executeQuery($query, $parameters);
988+
989+
$result = $sth->fetch(PDO::FETCH_ASSOC, 0);
990+
991+
if ($result === false) {
992+
throw new \AssertionError("No matching row found");
993+
}
994+
995+
return $result;
996+
}
997+
998+
/**
999+
* Fetches a set of entries from a database.
1000+
* Provide table name and criteria.
1001+
*
1002+
* ``` php
1003+
* <?php
1004+
* $mail = $I->grabEntriesFromDatabase('users', array('name' => 'Davert'));
1005+
* ```
1006+
* Comparison expressions can be used as well:
1007+
*
1008+
* ```php
1009+
* <?php
1010+
* $post = $I->grabEntriesFromDatabase('posts', ['num_comments >=' => 100]);
1011+
* $user = $I->grabEntriesFromDatabase('users', ['email like' => 'miles%']);
1012+
* ```
1013+
*
1014+
* Supported operators: `<`, `>`, `>=`, `<=`, `!=`, `like`.
1015+
*
1016+
* @return array Returns an array of all matched rows
1017+
* @throws PDOException|Exception
1018+
*/
1019+
public function grabEntriesFromDatabase(string $table, array $criteria = [])
1020+
{
1021+
$query = $this->_getDriver()->select('*', $table, $criteria);
1022+
$parameters = array_values($criteria);
1023+
$this->debugSection('Query', $query);
1024+
$this->debugSection('Parameters', $parameters);
1025+
$sth = $this->_getDriver()->executeQuery($query, $parameters);
1026+
1027+
return $sth->fetchAll(PDO::FETCH_ASSOC);
1028+
}
1029+
9591030
/**
9601031
* Returns the number of rows in a database
9611032
*

tests/unit/Codeception/Module/Db/AbstractDbTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public function testLoadWithPopulator()
175175
'cleanup' => true,
176176
]
177177
);
178-
$this->module->_loadDump();
178+
$this->module->_loadDump(null, $this->getConfig());
179179
$this->assertTrue($this->module->_isPopulated());
180180
$this->module->seeInDatabase('users', ['name' => 'davert']);
181181
}

tests/unit/Codeception/Module/Db/MySqlDbTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public function testInitialQueriesAreExecuted()
9191

9292
public function testGrabColumnFromDatabase()
9393
{
94+
$this->module->_beforeSuite();
9495
$emails = $this->module->grabColumnFromDatabase('users', 'email');
9596
$this->assertSame(
9697
[
@@ -102,6 +103,55 @@ public function testGrabColumnFromDatabase()
102103
$emails);
103104
}
104105

106+
public function testGrabEntryFromDatabaseShouldFailIfNotFound()
107+
{
108+
try {
109+
$this->module->grabEntryFromDatabase('users', ['email' => '[email protected]']);
110+
$this->fail("should have thrown an exception");
111+
} catch (\Throwable $t) {
112+
$this->assertInstanceOf(AssertionError::class, $t);
113+
}
114+
}
115+
116+
public function testGrabEntryFromDatabaseShouldReturnASingleEntry()
117+
{
118+
$this->module->_beforeSuite();
119+
$result = $this->module->grabEntryFromDatabase('users', ['is_active' => true]);
120+
121+
$this->assertArrayNotHasKey(0, $result);
122+
}
123+
124+
public function testGrabEntryFromDatabaseShouldReturnAnAssocArray()
125+
{
126+
$this->module->_beforeSuite();
127+
$result = $this->module->grabEntryFromDatabase('users', ['is_active' => true]);
128+
129+
$this->assertArrayHasKey('is_active', $result);
130+
}
131+
132+
public function testGrabEntriesFromDatabaseShouldReturnAnEmptyArrayIfNoRowMatches()
133+
{
134+
$this->module->_beforeSuite();
135+
$result = $this->module->grabEntriesFromDatabase('users', ['email' => '[email protected]']);
136+
$this->assertEquals([], $result);
137+
}
138+
139+
public function testGrabEntriesFromDatabaseShouldReturnAllMatchedRows()
140+
{
141+
$this->module->_beforeSuite();
142+
$result = $this->module->grabEntriesFromDatabase('users', ['is_active' => true]);
143+
144+
$this->assertCount(3, $result);
145+
}
146+
147+
public function testGrabEntriesFromDatabaseShouldReturnASetOfAssocArray()
148+
{
149+
$this->module->_beforeSuite();
150+
$result = $this->module->grabEntriesFromDatabase('users', ['is_active' => true]);
151+
152+
$this->assertEquals(true, array_key_exists('is_active', $result[0]));
153+
}
154+
105155
public function testHaveInDatabaseAutoIncrementOnANonPrimaryKey()
106156
{
107157
$testData = [

0 commit comments

Comments
 (0)