Skip to content

Suggestion: Add a function that waits for a value to appear in database #27

@dimitris-am

Description

@dimitris-am

This is a suggestion for a new function that waits for a value to appear in the database.
That would be helpful for many engineers in order to reduce actual needs of using explicit waiting when running tests.

In the example below, the actual waiting time (0.5 seconds) could also be an argument and many more improvements that you guys could think of :)

Thanks for reading and for your great work!

Proof of concept:

/**
* Waits for a certain value to appear in the database.
 *
 * @param string $expectedValue      Expected value to be used in assertion.
 * @param string $table              DB Table.
 * @param string $field              Field of db table.
 * @param array  $criteria           Criteria for db query.
 * @param int    $maxNumberOfRetries Maximum number of retries.
 *
 * @throws Exception
 */
public function waitForValueInDb( $expectedValue, $table, $field, $criteria, $maxNumberOfRetries = 10 ) {

	$actualValue = null;

	for ( $i = 0; $i <= $maxNumberOfRetries; $i++ ) {
		$actualValue = $this->grabFromDatabase( $table, $field, $criteria );

		if ( $actualValue !== false ) {
			continue;
		}
		$this->wait( 0.5 );
	}
	$this->assertEquals( $expectedValue, $actualValue, "Expected value wasn't matched with the actual one fetched from db." );
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions