FREELessons: 13Length: 1.3 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.1 Regular Testing

Here we will examine the process of how someone may go about testing a PHP application by writing manual tests, after writing application-specific code. This is a good learning exercise because you get to start with something very easy, and it is still better than writing no tests at all.

2.1 Regular Testing

So there are many different ways that you can approach to test your PHP web applications. In this course, we'll be covering three different testing methodologies. We'll start with just some regular old manual testing, then we'll move on to test-driven development, and the last one will be behavior-driven development. For this lesson, let's start with the first one, just the regular old PHP testing method that many beginners use. We're not even going to use a framework. We'll keep it as simple as possible while learning so that you can focus on the core concepts of testing and why some of them may be a bad practice rather than focusing on the actual application code. Now when I refer to application code, I'm talking about the PHP code that you wrote for your PHP web application. For example, application code is the PHP code that might have been written to save some type of data that was submitted from a form into a database. Now, a test code is the code that you write to test your application code. We'll learn more about this later on. Okay. So, let's get started by creating a simple PHP script, which will be our application code. So with just this small little example, you should be able to see the advantages, that proper PHP testing, has over this common beginner method of testing PHP applications. Let's begin. So I like to store my applications in my Sites directory. I'll just create a New Folder in here. I'll name this phptestingbasics, and this is going to hold our application code. And I'm just going to pull up my terminal here. And let's change directories into Sites/phptestingbasics. And then I'll just open this up in sublime, which is my text editor. All right, let's create a new file inside of our phptestingbasics folder. I'll save this as app.php. And let's just get rid of this other blank one here. All right. Let's now create a basic web page with a form where we'll be able to prompt a visitor for their first name. We'll then use a bit of PHP to fetch their name from the form and give them a funny, customized message. Let's start by creating some basic HTML. I'll give this a title of Funny Message Application Code. And then down here in the body, let's create an h1 heading. This'll say, Funny Message Generator. And now let's create our form. We'll set the action just to submit to the current file, app.php, and we'll set the method to post, and let's close the form, and let's have our labels say, What's your first name? Put a br tag here and then we'll create the input field. This'll have a type of text. And a placeholder of First name. And we'll also give it a name of fname. That's just short for first name. Now let's create the submit button. Set its type to submit and we'll also give it a name of submit. So that we can reference it in our PHP code. And that's it. Now that we have our form, let's write a bit of PHP to process the form from within this same app.php file to give the visitor their custom funny message using their submitted name. So, right after the form, I'll open up my PHP tags. And let's start by retrieving the submitted first name and the submit form's values and storing those in variables so that we can reference them in a minute. I'll create a new variable named fname. We'll use POST and we'll grab the submitted fname value from the form. Let's create another variable. I'll name this submit. We'll grab POST. Submit. And this'll grab whether or not that submit button was submitted with the form as well. This way we know that the form has been submitted. Now let's create another variable to hold our funny message. I'll just give this a name of message and we'll have this hold a default value of an empty string. Okay. Let's now process our form submission. We want to make sure that they don't try to submit the form without entering in their first name. If they do, we'll give them an error message. Then we also want to check if they did submit the first name field properly. Meaning that it's not empty. And if so, we fill in this message variable with the funny message so that we can display it to the visitor. So let's use an if statement here. And we're going to check if that fname variable is empty using the empty method. And then we're also going to use the && operator here to check a second value. We're going to check if the submit variable is not empty. So if the first name variable is empty and the submit variable is not empty we're going to display an error message because that means they didn't enter in their first name. Let's open up our curly brackets here. We'll use our message variable, and we'll give it a new value. This will just say, You must enter in your first name above, to get your funny message. Now let's use an elseif statement to check another condition. This time we're gonna check if, that fname variable's value is not empty. If that variable's value is not empty, that means they submitted the form, and we can then display their custom funny message using their name. So let's again reassign the value of that message variable. We'll use that fname variable, and we'll concatenate on a funny message. You could use whatever funny message you'd like. I'm going to use one that I remember as a kid. Okay, perfect. Our application code is complete. Let's now test it by going to the browser and seeing if it works. First, we need to start up our server. I'm just going to use the built-in PHP server, and I'll switch over to my terminal. And from within the PHP testing basics folder, I'll run the following command. Php -S localhost:8000 and that will start up a server at this URL for us. Here I am in Firefox with my server running. We can then visit localhost:8000/app.php that's our application code and let's run that. And great, here's our app. You can see our heading and our form. So let's try using the form to see if that little bit of validation that we wrote works as we expect. If we leave the form empty we should get an error message when we submit it, let's try. Okay, so you can see that our error message did not show up. Let's try it again here. Nope, I'm not getting anything. Let's try entering in a name and see what we get. All right, still nothing. Now at this point, we would just have to go back into our application code and try to locate where the error might be. We don't have any error message here, no hints, no line numbers or anything. Since our application is small and you probably already know where the error lies. We can easily just go into one file. Let's switch back in the sublime here. We can just go back into our app.php file. And we can easily spot that, the error is that we never actually echoed out our message variable. However, in larger applications with more complex code, it's much harder to spot your errors and it becomes very time consuming and reduces your overall efficiency. Now we can easily fix this and just echo out our message variable. So right after this PHP statement, I'll open up another PHP tag and let's echo out a paragraph tag. And then we'll just concatenate on our message variable. And let's close the paragraph, and close our PHP tag. All right. Now if we go back into the browser. And let's just refresh this page here. And let's try the form again. I'll just leave the first name field empty and submit it. And now you can see we get our error message here saying that we need to enter in our first name to get the funny message. Now if we enter in our name, I'll put in Andrew, and submit, there we go, we get our funny little message or whatever you put in there for yours. So now this process that we just went through, is the most common way that beginning PHP web developers test their web applications. They write their code up to a point to where they feel that they've implemented a feature. And then they attempt to load it up in the browser to see if it works. Many times it might work fine, other times the application doesn't do anything and you're not sure what happened. Sometimes the application may appear to be working fine and it's actually not working fine. So there's an easier way to do this. We can use testing. We can write separate PHP files that we can use to check that our application PHP code is doing just what we expect it to do. Our tests will give us hints as to where the problem might be. If our application doesn't run properly, we can check our tests for more information. Of course, it's not bullet proof, and your tests aren't going to protect you from everything, but a little bit of testing would be better than no testing at all. Now even better than just writing some tests after we've already written the code for our application, is that we can write our tests before we attempt to write any application code, to save ourselves time and to improve our developer productivity, reducing the time that we spend debugging manually. Now, this is the topic of the next lesson where we'll learn about test-driven development. So, if you're ready, I'll see you there.

Back to the top