Installing QBElggTestsForPHPUnit

Posted by on Nov 25, 2011 | 2 comments

In a previous post we introduced QBElggTestsForPHPUnit, an extension of PHPUnit used to test elgg-related functionality (note: PHPUnit is not bundled with this plugin. We assume that PHPUnit is already installed on your machine and the include paths are properly configured). In this post I will show you how to install this plugin and check that everything is running properly.

Basic steps:

  1. First things first, download the package and unzip it in your /mod folder. You can get the package from our plugin’s page or from the elgg site. As a result you should have now a mod/QBElggTestsForPHPUnit folder.
  2. Create a new MySQL test database in the host you are planning to store the test DB (this would normally be in localhost; although I haven’t tried, it should be possible to store the DB in a remote server. I guess however that the tests would run quite slow). For the sake of the example we will call the DB “elgg_test_db” and it will be running on localhost.
  3. While not required, I recommend creating a separate mysql user that only has access to the test DB. In this example the user name/password will be “db_username”/”db_password”.

Ok, that is all we have to do at the “admin” level. We now have to make a couple of configurations:

  • There is a file named “template_config.ini” in the /model directory of the plugin. You should fill the values in it to match your test DB installation and save it as “config.ini”. In our example it should look like:
[Database]
dbuser = "db_username"
dbpass = "db_password"
dbname = "elgg_test_db"
dbhost = "localhost"
dbprefix = "elgg_"
By creating the “config.ini” file you will be changing the settings for all test cases. If you want a different configuration for each test case just place a “config.ini” file in the same location as your test case file and override the global values. These new values will take precedence over the global config only for those tests in that directory.
  • There is a file named “template_replacements.ini” in the /model directory of the plugin. This file basically contains a set of values that will be replaced in the test DB (the idea is to make the basic fixture configurable by extracting installation-dependent stuff). Again, create a “replacements.ini” file using the template as a basis. The file should look like this:
[General Configuration]
= "elgg_"
;This is the prefix for all tables, which is generally "elgg_"

[datalist.sql]
= "/your_elgg_installation_path"
;This path sould match the one used in the real elgg installation

= "/tmp/elgg_test_data/"
;The data path. I like to put it in the standard OS temp directory instead of the
;real elgg /data directory, so that any generated garbage doesn't mess up with the elgg site

[metastrings.sql]
= "nobody@fakemail.com"
;The admin mail. Use a real one if you want.

[sites_entity.sql]
= "Your site name"
;The site name

= "http://yoursiteurl/"
;The site url. It should match the real site url

 

And that’s it. Assuming that you have properly configured those two files in the /model directory you should be able to use the plugin.

So, let’s run the test cases that check the plugin itself :) . To do so we have to do some tricky stuff, like temporary changing MySQL log file to be sure that our plugin is doing the things as it should. For this reason, to run our “metatests”, the database user should have RELOAD and SUPER privileges (note: this privileges are only required to run the two test cases in the QBElggTestsForPHPUnit/tests folder. They are not required to run any other tests).

 

Assuming that the privileges are ok, you can now run:

phpunit --process-isolation tests/ElggTestDBReloadTest.php

which should give you the following output:

PHPUnit 3.6.3 by Sebastian Bergmann.

..

Time: 3 seconds, Memory: 2.75Mb

OK (2 tests, 66 assertions)

 

Ok, now the second test:

phpunit --process-isolation tests/ElggTestRestoredTablesTest.php

which should output something like:

PHPUnit 3.6.3 by Sebastian Bergmann.

....

Time: 6 seconds, Memory: 2.75Mb

OK (4 tests, 131 assertions)

 

If everything went ok, you can now run the ports we made of the core elgg tests to our testing suite. To do so just run:

phpunit --process-isolation tests_elgg

and you should see:

PHPUnit 3.6.3 by Sebastian Bergmann.

...............................................................  63 / 189 ( 33%)

............................................................... 126 / 189 ( 66%)

............................................................... 189 / 189 (100%)

Time: 03:16, Memory: 6.00Mb

OK (189 tests, 1083 assertions)

And that’s it. In the next post I will show you how to start writing tests for elgg using the ElggTestCase class.

2 Comments

  1. Can you verify why i’m getting this error. I tried that with elgg 1.8.3.

    phpunit –process-isolation tests/ElggTestDBReloadTest.php
    PHPUnit 3.6.3 by Sebastian Bergmann.

    FS

    Time: 2 seconds, Memory: 3.75Mb

    There was 1 failure:

    1) ElggTestDBReloadTest::testDBReload
    /DROP TABLE elgg_access_collection_membership/ was matched 0 times in
    Expecting 1 match.
    Failed asserting that false is true.

    /home/tarun/nfsmounts/tarun@ys/etc/php_apps/www/elgg.ys/public_html/mod/QBElggTestsForPHPUnit/tests/ElggTestCaseMetatest.php:143
    /home/tarun/nfsmounts/tarun@ys/etc/php_apps/www/elgg.ys/public_html/mod/QBElggTestsForPHPUnit/tests/ElggTestDBReloadTest.php:127
    /home/tarun/nfsmounts/tarun@ys/etc/php_apps/www/elgg.ys/public_html/mod/QBElggTestsForPHPUnit/tests/ElggTestDBReloadTest.php:72

    FAILURES!
    Tests: 1, Assertions: 1, Failures: 1, Skipped: 1.

    • Hi Tarun, I haven’t yet tested the mod in elgg 1.8.3. However that test is basically checking that the DB is properly reloading before each test. To do that it checks that a “DROP TABLE elgg_access_collection_membership” command is issued. Just to be sure, have you checked that de DB user has the right permissions to execute the metatests as stated in the article (…For this reason, to run our “metatests”, the database user should have RELOAD and SUPER privileges…). Today and tomorrow I’ll be quite busy, but I’ll be able to look at this after that. Please, let me know is the user permissions are ok.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>