Search code examples
phplaraveltestingcodeception

Using logical or in seeInDatabase


How can I implement a logic 'or' operator in Codeception's seeInDatabase method?.

For example: the methods syntax is

seeInDatabase('table', array('column1' => 'value1', 'column2' => 'value2')); 

Which generates this query

SELECT COUNT(*) FROM `table`
WHERE `column1` = 'value1'
    AND `column2` = 'value2'

How can I generate the following query?

SELECT COUNT(*) FROM `table`
WHERE `column1` = 'value1'
    AND (
            `column2` = 'value2'
            OR `column2` = 'value3'
        )

Solution

  • you can try this-

    $this->seeInDatabase('table', function($query)
    {
        $query->where('column1', 'value1')->where('column2', 'value2')->orWhere('column2','value3');
    });
    

    Or You can do this split into some simple steps. First get the records-

    $model = Model::where('column1', 'value1')->where('column2','value2')->orWhere('column2','value3')->first();
    

    then test-

    $this->assertNotNull($model);
    

    For better understanding, you can see this.