Skip to content

TestRepository::read_records() array $query with more than one condition returns duplicate and mismatching records #850

@ScottVerbeek

Description

@ScottVerbeek

The method TestRepositort::read_records takes a parameter array $query. These are the conditions that the data returned must match to. If multiple conditions are passed two problems can happens:

  1. If mutiple conditions are correct, multiple of the same records are added to $matchingrecords.
  2. If not all conditions are met, records are still added to $matchingrecords since only one has to pass to be added.

For ease here is the method:

    /**
     * Reads an array of objects from the store with the given type and query.
     *
     * @param string $type The name of the table to retrieve from.
     * @param array $query Any additional conditions to add to the query.
     * @return array
     */
    public function read_records(string $type, array $query) {
        $records = $this->testdata->$type;
        $matchingrecords = [];

        foreach ($records as $record) {
            foreach ($query as $key => $value) {
                if ($record->$key === $value) {
                    $matchingrecords[] = (object) $record;
                }
            }
        }

        return $matchingrecords;
    }

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