skip to Main Content

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 IlluminateDatabaseQueryException: 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?

2

Answers


  1. Chosen as BEST ANSWER

    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"/>
                 .........
    

  2. count(‘id’) is wrong , use count(‘id’) or other field .

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search