mysqlunit-testingsymfonydoctrine-ormliipfunctionaltestbundle

Error when using YEAR, MONTH, DAY mysql functions in LiipFunctionalTest bundle


I'm using DAY, MONTH, YEAR mysql's functions (http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_year) with LiipFunctionalTestBundle. When I run the test I'm getting:

SQLSTATE[HY000]: General error: 1 no such function: YEAR

At the end, the test failed. I guess it's because of that.

This is my config_test file:

imports:
    - { resource: config_dev.yml }

framework:
    test: ~
    session:
        storage_id: session.storage.filesystem
        name: MOCKSESSID

web_profiler:
    toolbar: false
    intercept_redirects: false

swiftmailer:
    disable_delivery: true

liip_functional_test: 
  cache_sqlite_db: true
  authentication:
    username: "root"
    password: "admin"

doctrine:
  dbal:
    default_connection: default
    connections:
      default:
        driver:   pdo_sqlite
        path:     %kernel.cache_dir%/test.db
  orm:
    dql: 
      datetime_functions:
        YEAR: DoctrineExtensions\Query\Mysql\Year
        MONTH: DoctrineExtensions\Query\Mysql\Month
        DAY: DoctrineExtensions\Query\Mysql\Day           

In my code I use them as:

$em = $this->getDoctrine()->getManager();
$emConfig = $em->getConfiguration();
$emConfig->addCustomDatetimeFunction('YEAR', 'DoctrineExtensions\Query\Mysql\Year');
$emConfig->addCustomDatetimeFunction('MONTH', 'DoctrineExtensions\Query\Mysql\Month');
$emConfig->addCustomDatetimeFunction('DAY', 'DoctrineExtensions\Query\Mysql\Day');

....
$em = $this->get('doctrine.orm.entity_manager');
$dql = $em->createQuery('SELECT um FROM Acme\AcmeBundle\Entity\Menu um WHERE 
                  um.user = :id and 
                  YEAR(um.day) = :year and 
                  MONTH(um.day) = :month and 
                  DAY(um.day) = :day');
      $dql->setParameter('id', $id);
      $dql->setParameter('year', $now->format("Y"));
      $dql->setParameter('month', $now->format("m"));
      $dql->setParameter('day', $now->format("d"));

If I run the application I don't have problems.


Solution

  • The problem is not related to mysql but to sqlite. I run my tests using sqlite which throws the exception above. The simplest way I found to solve the issue is change the queries with a general version that both databases could understand.

    I also thought to have two versions of every query, one for mysql and one for sqlite, and play with the environment when the application execute the tests. For example, if I'm using the test environment use the sqlite query, otherwise use mysql. However I consider this option very dependent and hard to maintain in the future.