Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Timothy Boronczyk:
PHP Assertions
Nov 08, 2012 @ 17:43:49

Timothy Boronczyk has written up a new post that looks at using assertions in PHP - the actual use of the assert function to evaluate values in your code.

I stumbled upon assertions in PHP today, though why I didn't know they existed after working with the language for so long and what I was looking for originally when I came across them are both mysteries. And with the increasing focus on software quality in the PHP community, I wondered why I hadn't seen them used by others. I decided to ask around, look into PHP's implementation of assertions, and do some tinkering.

He talks some about their usage, some of the common issues surrounding them and compares using them directly on return values vs evaled strings. He also includes an implementation of them in a bit of sample code - a class that uses them (and an assertion callback) to handle the throwing of exceptions.

Assertions are meant to identify program logic/design bugs, not as a run-time error handling mechanism. Isn't this why we do unit testing? Playing devil's advocate, what's wrong with pushing unit tests directly into your code if we have doc comments that are extracted for documentation?
tagged: assertions native function overview exception error

Link:

SitePoint.com:
Be More Asssertive: Getting to Know PHPUnit's Assertions
Jan 20, 2011 @ 19:32:28

On SitePoint.com today there's a new article, the second in the series, introducing unit testing in PHP with PHPUnit. The first article can be found here. (Disclaimer: I am the author of this series of articles).

In my previous article, I took you through some of the basics of unit testing with PHPUnit. I introduced you to what it means to test your code and the real benefits it can for you and your code. This time I want to dive in a little deeper and get to know PHPUnit in a bit more detail.

The tutorial goes into more detail on some of the commonly used assertion methods PHPUnit has to offer including: assertTrue, assertLessThanOrEqual, assertStringStartsWith, assertSame and assertClassHasAttribute. There's also a brief discussion about marking tests incomplete or skipped and how, except in some circumstances, it should be avoided at all costs.

tagged: phpunit assertions unittest tutorial

Link:


Trending Topics: