eZ Community » Forums » eZ Publish 5 Platform » How to test a bundle
expandshrink

How to test a bundle

How to test a bundle

Sunday 05 January 2014 2:05:11 pm - 6 replies

Hi, I'm developing an utility bundle with some helper classes and twig functions.Now I want to create some tests but I don't find example on how to test against the eZ repository.Any suggestion or example to point me on the right direction?Thanks, Gabriele

Sunday 05 January 2014 5:49:53 pm

If you want to test against the existing database, maybe you could try to do something like:

You need to setup the ezpublish installation to use the localhost url.

You need to extend WebTestCase and in your test function you need to do something like this:

public function testEmptySearchText()
{
    $options = array(
        'environment' => 'test'
    );
    $client = static::createClient($options);
    $repository = $client->getContainer()->get('ezpublish.api.repository');
 
   ...
}.

And then you could create your test environment like this:

config_test.yml

imports:
    - { resource: config_dev.yml }
 
framework:
    test: ~
    session:
        storage_id: session.storage.mock_file
    profiler:
        enabled: false
 
web_profiler:
    toolbar: false
    intercept_redirects: false

And copy your ezpublish_dev.yml content to ezpublish_test.yml.

Finally run your tests:

phpunit -c ezpublish

Modified on Monday 06 January 2014 1:30:08 am by Thiago Campos Viana

Monday 06 January 2014 12:18:25 am

Thanks for the hints Thiago, my test class now extends BaseContentServiceTest but I got this exception

 Exception: exception 'ErrorException' with message 'Missing mandatory setting $_ENV["setupFactory"]

What am I supposed to set in that env variable?

Thanks, Gabriele

Monday 06 January 2014 1:33:19 am

Quote from Gabriele Perego :

Thanks for the hints Thiago, my test class now extends BaseContentServiceTest but I got this exception

 Exception: exception 'ErrorException' with message 'Missing mandatory setting $_ENV["setupFactory"]

What am I supposed to set in that env variable?

Thanks, Gabriele

I fixed my first answer, actually you need to extend WebTestCase and install ez publish using the localhost url, so you need to be able to access ez publish typing http://localhost

Then you need to create the config_test.yml and the ezpublish_test.yml as specified in my first post. And finally you need to start the test function as I specified in my first post.

I don't know if that is the only, or recommended, way to do this, but that is how I was able to access the repository.

Modified on Monday 06 January 2014 1:34:02 am by Thiago Campos Viana

Tuesday 07 January 2014 10:31:09 am

Thanks Thiago, but with this approach the bundle is not testable itself.

Your first solution (the one that extends BaseContentServiceTest using the Memory Back End) based on my knowledge seems better to me

Tuesday 14 January 2014 10:04:34 am

Hi Gabriele

If you have dependencies on services for the service you're testing, you need to use mocks. You can find a few simple examples in CommentsBundle (including phpunit.xml and bootstrap.php). You'll also find a lot of examples in the test directories in ezpublish-kernel (check in EzPublishCoreBundle first maybe).

And of course, don't forget to define and install your dependencies in your composer.json (can be in require-dev, unless it's already an upstream dependency) blunk.gif Emoticon.

Happy testing !

Sunday 19 January 2014 2:14:03 pm

Thanks Jérôme, I'm working on that!

I'll post my progress here

 

Gabriele

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from