PHPUnit – Unit Testing with PHP

Unit Testing
Unit Testing

In modern software development, unit testing can no longer be an ignored activity, especially for object oriented programming (OOP). I have been coding in PHP for several years now. I thought I’d share my thoughts on unit testing in PHP with you.

Unit Testing in PHP

OOP has so many advantages over procedural programming and unit testing is one of them. I’m not saying that unit testing can’t be done with procedural styles, but it has a much better use case in OOP programming. I am not going to get into the nuts and bolts of setting up PHPUnit for your PHP project, but JetBrains, the folks behind my favorite IDE for PHP, have an excellent article about setting up PHPUnit for PhpStorm. It can be found here.

Once PHPUnit is set up, writing tests is easy. Just plug in what you want to send to a method and tell PHPUnit what you expect the results to be.

Skeleton Framework

Here is a basic skeleton framework for writing tests.

 

<?php
/**
 * Created by PhpStorm.
 * User: aaron
 * Date: 8/13/17
 * Time: 12:03 PM
 */

require '../lib/Utilities/Utilities.php';

class UtilitiesTest extends PHPUnit_Framework_TestCase {


/* ** Test Initialization ** */

private $utilities;

protected function setUp()
{
  $this->utilities = new Utilities();
}

protected function tearDown()
{
  $this->utilities = NULL;
}

/* ** Tests ** */


/* ** addTwoNumbers() Tests ** */

public function test_addTwoNumbers() {

  $number1 = 2;
  $number2 = 3;

  $result = $this->utilities->addTwoNumbers($number1, $number2);

  $this->assertEquals(5, $result);

}

/* ** subtractTwoNumbers() Tests ** */

public function test_subtractTwoNumbers() {

  $number1 = 2;
  $number2 = 6;

  $result = $this->utilities->subtractTwoNumbers($number1, $number2);

  $this->assertEquals(-4, $result);

}


}

PHP Assertions

There are a whole slew of assertions built into PHPUnit, assertEquals() is just one of them. Assertions such as assertNull(), assertNotNull(), assertContains() are some of the most common ones used. For a complete list, check out the assertions link of the PHPUnit documentation.

See, that really is pretty simple. Of course, when writing unit tests, there is a danger for the mind to wander and think of ‘what if’ scenarios, especially if you come from a quality background such as a QA Analyst. Unit tests should cover the functionality of the component being tested and nothing else; that is what QA is for; they will find the functional defects, you just need to make sure your code works as expected. That is where the unit tests shine.

The Purpose of Unit Testing

This can’t be stressed enough, the primary purpose of unit tests are not to find defects, they just make sure that the components of code work as they should. Even though the unit tests will find a defect in the above examples if it shows that 2 + 3 = 4, that is not the purpose of the test as this will be found during functional testing. The purpose is just to make sure that the method works correctly; calculator examples are also the easiest examples to demonstrate code.

New Resolutions – Week 2

New Resolutions - Honest Tea

Resolutions from Last Week

Last week, on ‘New Resolutions’…

I wrote about my newfound love of tea and the new teas that I was trying. I tried the Honest Green Dragon Tea and the Honest Assam Black Tea. Both were actually really good. I said something about not liking green tea, but liking dragons; well, the dragons won. I was pleasantly surprised because even though there was a sweetness to it, there was not the over-the-top sweetness that turned me off of most green teas.

The Assam Black Tea was pretty much what I expected, a nice, full body black tea. Not much else to be said about it other than it was good, just as I expected.

New Resolutions - Bizzy CoffeeResolutions This Week

This week, as part of my resolutions series, I leave tea behind and try some coffee. I’ve never been able to get into coffee but people at work drink it like mad. I have tried regular office rot-gut coffee, nice coffee shop coffee of varying varieties and have never really been able to hold an interest in it. Maybe its because I don’t like hot drinks, I don’t know. I’ve never been able to get into coffee.

I am going to try it once again this week.

As I was wondering the tea aisle of the grocery store, I came across some organic, concentrated, cold brew coffee by Bizzy. So I said to myself, “Self, you should try this.” So I am. I am hoping that the not so hot drink will appeal to me a little more, but as I write this and am sipping on it, I think it could use a little bit of warmth to it. I’ll report back the final results next week. For now I am going to (try to) enjoy it.

That’s all for now.

 

Edited – Pink Floyd’s The Dark Side of the Moon timed to the August 21 eclipse

Edit: Start time changed to 12:15:20 PM.

Pink Floyd is one of my favorite bands. I have to admit that when I first heard about the total eclipse of August 21, 2017, The Dark Side of the Moon didn’t immediately pop into my head until just recently. I’m certain it is not a new idea to time TDSotM to the time of totality of the eclipse, but to me, this is something that is right at the top of my bucket list. I know, I’m strange.

The first thing I needed to do was to find out when the totality of the eclipse would occur where I live. Luckily the Eclipse 2017 website has that information readily available. My local time for totality is 12:57:30 PM.

The second thing I needed to do was to find the time to the apex of the song Eclipse on TDSotM. This sounds simple, but can be a little complicated depending on which version of the album you have and how its stored. For me, I have the album on iTunes from the 20th Anniversary Edition, so the time to the apex for me is 42:10. Using this time should get you in the right neighborhood for a pretty good coinciding time for the eclipse.

Finally, subtract the time 42:10 from the time of totality. Again, for me, this is 12:57:30 PM. If all goes right, the time for me to start playing TDSotM is at 12:15:20 PM. Over the next few days, I am going to do some testing to make sure this time works correctly but I think it will.

Good luck to all who plan on trying this.

What I’m Reading – The Magic of Thinking Big

So far, I am 25% done with this book by David J. Schwartz. Its not a bad book, full of reenforcing quotes and ideas that, if one took the time to think for a few moments in this fast-paced world, they would come to the same conclusion. I like books like these, because they serve as a condensed reminder of the importance of thinking big and setting goals higher than you otherwise would.

These ideas are timeless, going back to the age of the Greeks or before. They survived through the Middle Ages, thrived in the Renaissance, reemerged again in the Age of American Exceptionalism (note: I personally mark this period as the post WWII era to the 1960s). Its a pity that critical thinking and goal setting are not taught in public schools anymore.

I see so much promise in the generation coming of age and am optimistic about the future and the youth of that future, skills like the ones preached in this book will make the future generations unstoppably successful.

If you get a chance, check out this book. It has a lot of little nuggets of wisdom such as these:

  • “See what can be, not just what is.”
  • “Build castles, don’t dig graves!”
  • “To think confidently, act confidently.”

https://www.goodreads.com/book/show/23658680-the-magic-of-thinking-big

Simplifications and Improvements

“Simplicity is the art of knowing what to take away.”

I have removed the menu bar for now (until more features are added) and made it simpler to add recipes by clicking the “Quick Add Recipe” button.

Enjoy!


Active development continues and some exciting things are being worked on.

Current work:

– Support work such as documentation, automation tests, etc. This work will not be noticeable to the end users, but it serves to support active development to ensure a quality product.
– Enhanced recipe forms. This form will allow users to enter advanced recipe information such as separate ingredients, separate notes and other information.
– Account validation through email
– Message feed improvements
– Password reset options
– Basic image manipulation
– Recipe privacy for sharing recipes.

I hope to have many of these completed by the end of the month.

Why I Stopped Saying “Busy” and Started Saying “Productive”

From the Archives – originally published on: 01/17/2017

One of my transformation in the last year, really the last quarter of last year, was to stop saying that I was busy. My wife or someone else would ask me how my day was and my typical response was, “It was busy.”

It was true that I had a lot of work to do, but all my recent work has been goal driven and purposeful; I did not have much if any busy work.

bus·y·work
ˈbizēˌwərk
noun: busy-work
work that keeps a person busy but has little value in itself.

So what has been my response when challenged with these questions? It’s simple, really, I have been telling people that my day was “productive”. The word “Productive” is one of those powerful words in the English language. It has the benefit of, once in the habit of using it, reinforcing the goals of the person who is using it to describe his/her day.

pro·duc·tive
prəˈdəktiv
adjective: doing or achieving a lot: working hard and getting good results

Working without a goal is just work, there is no purpose other than to make money. It will ultimately be flat and sour no matter how much money one makes. Working with goals gives you the chance to see real-life accomplishments; to cross off items and projects from to do lists which acts as an extremely powerful motivator, stress reliever, and productivity reinforcer.

Some of the happiest people, career-wise, that I have seen have been in the field of construction. Why is that? Because construction is an inherently goal driven field. The construction company gets a contract and a set of plans to build a building. Tasks need to be completed during the construction phase such as a foundation being poured, framing, plumbing, electricity, etc. Each one of these tasks gets checked off giving the crew a sense of accomplishment. Once the building is complete, that project can be marked as completed; the goal has been achieved and the crew was productive.

The flip side of that coin are some office jobs that require the employee to do the same thing day in and day out, these are often temporary jobs. Doing the same thing day in and day out is just passing a widget back and forth. Sure you could mark the pass as complete, but there is another, exact same widget right around the corner.

So what am I mumbling on about here? Simply put, try to be productive, not busy.

That is my thought for the day.

 

New Resolutions

While out shopping for groceries today, I came up with a wild idea. More of that later.

Grocery shopping is always fun. I get to look at all the stuff out of the ordinary. Organic, ethnic, in-store specials, cookware, etc. We were about three-quarters finished shopping, or, as I like to call it, seven-fourths of the amount of time and money I had planned on this outing when I came up with an idea for a resolution. I am going to try something new every week. This will not necessarily apply to food or food related items, but, at this point in time, it most likely will.

So my new resolution for this month is some new tea I found in the organic food section. Since I gave up alcohol and bad eating more than a year and a half ago, I have found an affinity for tea. I like black unsweetened teas that are served at room temperature. I know that this is probably breaking so many different rules of tea drinking decorum and that I will likely be confronted by the tea drinking police at some point in time, but this is how I like to drink my tea.

So, my new thing for this week is Green Dragon Tea by Honest Tea. It is fair trade organic which is always a good thing. In the past, I have not cared for green tea but I do like dragons so I’m hoping its a wash.

I’ll let you know how it goes.

Recifeed.com is launched

For several years, it has been a dream of mine to launch a recipe sharing social media website. In the last few weeks, I have promoted that dream to goals, those goals to action, and that action to fruition. The result is https://recipes.recifeed.com. Right now and for the foreseeable future, the site will be in beta where rapid changes can occur. The foundation is there, now its time to start building out the modules that will make this the best recipe sharing website out there.

Stay tuned for more great things to come.

…And we’re back

aaronhollman.com is back up and running. I have recently migrated my site to aws and updated site security. It is my hope that, this time, I keep up with regular blog posts. Almost nothing is off limits for this blog, but the focus will primarily be on technology, especially computer science, quality assurance, and anything that interests me.

I am going to try to recover old blog posts but there wasn’t much anyway so it may be best for me just to start anew; time will tell.

Thanks for reading.

Aaron