An Introduction to the Art of Unit Testing in PHP

Introduction
Testing is an essential aspect of developing in any programming language. If you don't test your source code then how can you verify it works as expected? Manual testing can only be performed irregularly and usually only in limited ways. The answer to testing source code regularly, and in depth, is to write automated tests which can be frequently executed. In PHP such tests are usually written using a unit testing framework, a framework which allows the source code of any application or library to be tested as isolated units of functionality such as a single class or method. As unit testing has gained popularity, it has become a standard practice in PHP with libraries and frameworks such as Swiftmailer, the Zend Framework and Symfony all requiring unit test coverage of their source code.

Unit Testing is often seen as an arcane, time consuming task — which it sometimes can be! But the point of spending time writing tests is to improve the quality of your source code so it has fewer overall bugs, many of which are detected early, a continual testing process to prevent new changes from changing the behaviour of older code, and to provide confidence that your code can be depended on. There are other benefits too, and we'll detail these later.

The Testing Fallacies
Unit Testing, and actually all other forms of testing, fall afoul of four common excuses which hinder adoption by developers.
1. It's time consuming and takes too long.
2. Complex code cannot be tested.
3. So long as it works, I don't need to write tests.
4. Testing is boring.

These are testing fallacies, excuses which appear quite reasonable but are actually misinformed in subtle ways. So let's clear up a few things!
Testing does take time. The question is why should that time be considered worthwhile and the answer is that it reduces the future time you would consume in modifying code, maintainance, refactoring and fixing undetected bugs. And we both know there would be tons of undetected bugs if you're not testing comprehensively!

Testing early is like catching the proverbial worm; as you write code, you can use Unit Tests to test isolated methods/classes or groups of functional classes immediately. By doing so you find and fix bugs quickly as they are created. A problem is that you find bugs so often, and fix them so quickly, that you barely notice the time it took. When you make changes later, and a test fails, you can fix the integration problem just as quickly — more saved time you barely notice. The benefits can be so well disguised you may miss them completely — and only see the test code it took you a few hours to write, not the bugs you solved in 10 seconds that would have taken minutes or hours six months down the line.

Secondly, there's complex code — and there's complex code made up of smaller practical parts. In OOP a simple objective is often «being testable». It's like a litmus test for quality decoupled code. If your code can't be easily tested than it's not that Unit Testing has failed, it's that you failed to write practical code which was flexible and decoupled. If you were testing as you wrote code, you would have been forced to decouple classes almost on automatic. The fact code seems too complex to test is usually a symptom of having waited too long to start testing!

Thirdly, working code and working tested code are two different animals. Tests offer a safety net which makes changes, refactoring, and new features less of a pain to add since integration issues will be detected almost immediately. They also improve the efficiency of new programmers on your team who, unfamiliar with the code, see their mistakes detected immediately by the shorter feedback loop and so gain experience on the run.


( Read more )

PHP Tutorials (Else...IF)

The if/else statement

Has any ever said to you “if you work hard, then you will succeed in whatever you do”? And then what happens when you don’t work hard?, Well, you will fail it’s as simple as that. This is an example of an if/else statement.

  • If you work hard then you shall achieve whatever it was that you had set out to do.
  • Else, if you don’t work hard, then you will fail and shall not be rewarded for your lack of effort.
How does this translate into something that is useful for the php developers? Well think about this for instance:

Someone comes to your website and you want to ask this particular visitor if this is their first trip to your website. With an if statement this is easy enough to do. Simply have a statement to check off, “are you visiting this webpage for the first time.” If the condition statement is true then have them directed to the “insert your name” page, else let them view the website as they normally would because you have already asked them for their name in the past.


( Read more )

PHP Tutorials / Lessons

Php tutorials / Learning Php

If you want to learn the premiere functions of a php, simple follow this short tutorial and everything should be clear. The prime goal of this tutorial is to teach you the reader the basics so that you can
• Customize the scripts you download, so that they fit your needs
• Begin to understand the workings of php, so that you can design your own model of the php projects
• Give you a base of php, so that your are deemed more valuable with your future employers

First you need to know what php stands for which is translated to (php hypertext preprocessor)

What is PHP?

It’s an html embedded scripting language mainly designed for producing web pages, created by Rasmus Lerdorf in 1995. The purpose of the language is to allow web developers to write web pages quickly with. In this PHP lessons you will see it.
This might be a good definition. Although it does contain a lot of computer talk, instead of these terms, just think of php as a powerful behind the scenes scripting tool that your web visitors won’t ever see.
When people visit your php webpage, your web server processes the code. It then sees what it needs to show such as content text and pictures and hides the computer language stuff such as the math calculations and file operation, then translates this into html style. Once the translation is finished it then sends the webpage to your visitor’s web browser.


( Read more )