friendica/friendica — pull request #132 by campino (2012-03-14 12:56:43+00:00)
Title: Test cases

I wrote some test cases.

Steps to execute:

(1) install phing. I recommend using pear. Under Linux Fedora (and several other distros) just install pear with your package manager (like yum, apt, aptitude, zypper, ...). After that, you need to add the phing channel:
pear channel-discover pear.phing.info
and install phing
pear install phing/phing

(2) I use phpUnit for the test, so you also need that one.
pear install phpunit

(3) after that, switch to your friendica directory and type
phing test
This executes the test cases.

(4) After that, there is a file report/phpunit-noframes.html. Open it and see the results. As the documentation, that file is on .gitignore. It makes no sense to post results of a test everybody may execute on his/her own.

How to write tests:
Create a new file in the tests directory. Its name should end with "test.php". There is a class in it, that inherits PHPUnit_Framework_TestCase. If it contains a function named "setUp", it is called before the tests are executed. If it contains a function named "tearDown", it is called after the tests. Please mind, that this methods are executed before and after every single test.

Tests are functions who's name starts with "test" in the same class. They should contain some calls to assert- functions. Those functions mean: We expect something to have a specific value, the test will fail if it has another value. (e.g. $this->assertTrue(1==2)) will always fail, because 1 is not equals 2, so 1==2 is false, we asserted true.
phpUnit executes all tests, but it does not guarantee order. So don't rely on test1 being execute before or after test2.
In general, I wouldn't recommend using a database in tests, it is too much configuration for everybody who wants to execute tests locally. I used another pattern in my get_tags_test. It simulates a database by overwriting "q".
If you use a database, you may want to write appropiate values into the database in setUp and remove them in tearDown (you may also do it in the test itself, if you don't need the values in all your tests). Please don't forget removing them, we don't want to junk the database.

By now, there are tests for several stuff from text.php, including functions to prevent XSS, the get_tags method and other stuff. Some of them fail. I tried to test all possible input and most functions do not behave well on invalid input. In my eyes there are several possibilities how to solve that:
(1) this input is invalid, so the functions return values are undefined. Don't test it.
(2) input is invalid right now, but it should be valid. So fix the functions.
(3) invalid input should be documented. Throw an exception.

I don't like (1), because if somebody uses the function with invalid input by accident, he will not notice. (3) throws an exception, so if someone mis-uses the function, we will see the problem immediately. On the other hand it may cause performance problems to test for validity in every method first. If (2) applies, depends on the function.
So this question should be looked into for every function individually.

PS: I would like to connect to some friendica developers on friendica. I'm campino@friendica.eu.