Home > Net >  Mysql function fails in Laravel tests
Mysql function fails in Laravel tests

Time:01-28

I have a query below that retrieves groups from the database; it works fine except when I run it through pest php test case and hit the controller; the test fails and says Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such function: YEAR

Here is my sql query

 Group::query() 
    ->selectRaw("YEAR(groups.created_at) as year,
                 MONTH(groups.created_at) as month, 
                 DATE_FORMAT(groups.created_at, '%b') as short_month_format,
                 count('*') as count"
            )
            ->whereIn('id', $ids)
            ->get();

Here is my pest php test case

test('authenticated user with permission will see analytics page ',function(){
    $permission = 'permission';

    actingWithPermission($this->user, $permission)
    ->get($this->route)
    ->assertStatus(302)
    ->assertDontSee("Text not to see goes here", false)
    ;
});

How can I fix mysql error above?

CodePudding user response:

count('id') is wrong , use count('id') or other field .

CodePudding user response:

I was able to resolve the above issue by changing DB CONNECTION in phpunit.xml from sqlite to mysql.

In my phpunit.xml

          .........
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="mysql"/> 
        <server name="DB_DATABASE" value="database-name"/>
             .........
  • Related