Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rcadhikari's avatar

PHPUnit fails to count with group by on query to the database view (not table)

  • Laravel Version: 5.4.*
  • PHP Version: >=5.6.4
  • Database Driver & Version: MySQL (Testing with SqlLite)

Description:

I have an Eloquent model called ProductStatistics with $table='view_products', here view_products is a database view rather than table.

view_products has following:

| product_id | product_name | category_id | category_name | | ------------- |:-------------:| -----:| -----:| | 1 | prod1 | 2 | cat2 | | 3 | prod3 | 2 | cat2 | | 4 | prod4 | 3 | cat3 | | 5 | prod5 | 3 | cat3 | | 6 | prod6 | 3 | cat1 |

| First Header | Second Header | | ------------- | ------------- | | Content Cell | Content Cell | | Content Cell | Content Cell |

And, in my repository, I am testing a query to get products counts by category.

$result = $this->ProductStatistics
                    ->groupBy('category_name')
                    ->count();

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/app.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory suffix="Test.php">./tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_CONNECTION" value="testing"/>
        <env name="DB_DATABASE" value=":memory:"/>
    </php>
</phpunit>

Steps To Reproduce:

However, my Unit Testing with following error: <p class="break-long-words trace-message">SQLSTATE[HY000]: General error: 1 wrong number of arguments to function GROUP_CONCAT() (SQL: select count(&quot;category_name&quot;) as aggregate from &quot;view_products&quot;)</p>

Don't know why SQLite doesn't like counting on the database view. however it's fine with the database table.

0 likes
0 replies

Please or to participate in this conversation.