Monday, June 30, 2008

Convert A Quad Into A Buggy

QA Testing methods and static functions in PHP

recently on a blog devoted to testing on Google in one of the posts Fri Defeat "Static Cling" read an interesting suggestion about testing static methods in Java. As in Java and PHP can not create a static method Mock authors suggest creating a special post type calling static methods which would be encapsulated. I admit that, while in Java a compiled language such a solution is most acceptable in an interpreted PHP always try to avoid a proliferation of new classes and types. Using PHPUnit can easily create objects whose implementation is replaced by the test code only required by us and, besides, the whole class behaves just like the original.
Suppose that we have a sample Person class in which we find static method declarations:
 
class Person {public static function
find ($ id) {
/ / Code that gets such a database
return $ person;}



the code in other parts of the code can be used, for example, like this:
 
NotFoundException class extends Exception {} class

PersonsController {public
show_person ( $ id) {
$ p = Person:: find ($ id);
if ($ p) {throw new NotFoundException
();}


/ / some operations on the variable $ p

return $ p;

}}

now wanting to test the controller code will certainly want to be able to this static method pay at the request of different values \u200b\u200bor throw exceptions as such (for example, if we can not find the object with the given ID.) Unfortunately, not easy to get a Person object Mock.
simple solution is to close the static calls Person:: find () from the controller code:

 
class
PersonsController {public function find ($ id) {return
Person:: find ($ id);}
public
show_person ( $ id) {
$ p = $ this-> find ($ id);
if ($ p) {throw new NotFoundException
();}

//...
return $ p;

}}


Now the test code using PHPUnit3 might look like this
 
testShouldThrowAnExceptionIfPersonNotFound public function () {$ controller
= $ this-> getMock ('PersonsController', array ('find'));
$ controller-> expects ($ this-> any ())
-> method ('find')
-> will ($ this-> returnValue (false)) / / simulate situations in which the find is not the object
try
{$ controller-> find (1) / / search for a nonexistent object, to which the controller should react to a specific exception
$ this-> assertFail ('Exception not thrown ');

} catch (NotFoundException $ e) {return
;
} catch (Exception $ e) {
$ this-> assertFail (' Invalid Exception thrown ');}


}



Since functions in PHP have the static bind (this user functions as well as those of the modules and the core of the language for example, all functions working on arrays) and such calls should be enclosed in a non-static object methods and thereby gain the opportunity to exchange implementations for testing a variety of situations.

Tuesday, June 10, 2008

Woodbury Common Hollister

advice from the lady (PHP), vol.1

Recently watched RubyOnRails web framework a lot and I liked it very approach to the HTML template that is used there. I decided that in PHP projects also apply similar structures. As it turns out PHP has a wealth of opportunities in the deposition of code because the language was designed primarily for embedding in HTML. Occasional hash fails
used can be embedded code is to use the html tag with the attribute language = "php":
 
\u0026lt;script language="php">
echo "execute PHP code ';
\u0026lt;/ script>

not I unless specifically highlight how beneficial it is to use this option if a template needs to get a person working on the layout and uses the HTML editor with built-in preview. One drawback is the length of this record - it is suitable mainly for blocks of code whose performance is closely related to the display view - these elements should be as little as possible, of course, but you can block this elegantly put any specific helpers closely linked to the rendered view.

Like Ruby templates is possible to use short tags opening and closing in PHP. As described below, we can transform the array into a html table:
 
\u0026lt;? foreach ($ array as $ row) {?>
\u0026lt;tr>
\u0026lt;? foreach ($ row as $ cell) {?>
\u0026lt;td> \u0026lt;? = $ cell?> \u0026lt;/ td>
\u0026lt;? }?>
\u0026lt;/ tr>
\u0026lt;? }?>

I personally believe that a person who is not too much to do with programming, the presence of braces is less legible than a clear statement that tells where the conditional instruction begins and where it ends: PHP offers an alternative to writing klamrowego which is a record like pseudocode:
 
\u0026lt;? if (isset ($ _REQUEST ['param'])):?>
\u0026lt;div> bestowed on the parameter \u0026lt;/ div>
\u0026lt;? endif;?>

A similar syntax can also be used for the loop (while, for, foreach), and a switch statement.

That's all for today - I greet and wish you have fun with the syntax of PHP!