简体   繁体   中英

How to test custom DQL function?

I wrote a custom DQL function based on this tutorial. I would like to write some tests, but the functionality of DQL is quite complex.

Is there any way how to test it?

As I see, even doctrine project not tests own DQL functions:(

I know it's an old quesion but for the folks who goggled it out as I did.

Testing is simple in fact. The general idea is to generate a query using your custom funciton then compare it against the expected result. Here is the example.

Let's say you have a custom DQL function which translates MY_FUNC(field) to MySQL_INTERNAL_FUNC(field). Then you test it as the following.

/* Somewhere in the PHPUnit test */
/* Assuming $em represents your EntityManager instance */
$query = $em->createQuery('
    SELECT MY_FUNC(entity.someField)
    FROM MyBundle:MyEntity entity
');
self::assertEquals('
    SELECT MySQL_INTERNAL_FUNC(v0_.some_field) AS sclr_0
    FROM my_entity v0_
', $query->getSQL());

That does the trick. Note the indents. They are there just to make the code clear. In practice Doctrine will remove them making SQL. So the expected SQL code should be in one line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM