Jonnny
Jonnny

Reputation: 5039

How would I test for this line in Codeception

I am writing some unit tests and struggling to capture the 1 remaining line of this small model in Yii2.

UserSearch.php

public function search($params)
{
    $query = User::find();

    // add conditions that should always apply here

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $this->load($params);

    if (!$this->validate()) {
        // $query->where('0=1');
        return $dataProvider; // This line in tests is red and marked as not executed
    }

    // grid filtering conditions
    $query->andFilterWhere([
        'id' => $this->id,
        'date_added' => $this->date_added,
        'last_login' => $this->last_login,
    ]);

    $query->andFilterWhere(['like', 'username', $this->username])

    return $dataProvider;
}

UserTest.php

public function testUserSearch()
{
    $model = new UserSearch();
    expect_that($model->search(['id' => 2]));
}

public function testInvalidDataProvider()
{
    $model = new UserSearch();
    expect_that($model->search(['id' => '2']));
}

The second test passes correctly as !this->Validate() method fails as id isn't an integer, why isn't the return statement reflected as executed in the code coverage. what am I misunderstanding here?

Upvotes: 0

Views: 233

Answers (1)

uldis
uldis

Reputation: 362

Upvotes: 2

Related Questions