Category Archives: PHP

Creating Tests with Laravel

Laravel, Artisan and Testing

By: Tyler Lundstrom

The past few weeks I have been working on a project using the Laravel PHP framework. So far I love it and it has increased my abilities and knowledge for developing PHP immensely. One of the many great things about Laravel is it’s Artisan tool. Laravel’s Artisan tool is used to to create a lot of the boiler plate code for you as well as put the generated files in the proper repositories.

The command I’d  like to talk about today is php artisan make:test this command when given a name will auto generate the test boiler plate you need to running unit tests in Laravel. This saves you time from having to manual type out all the files you are using as well as the class name and extensions. As a side note about the extension, you extend a class called TestCase which is an abstract class that has many of the boiler plate style syntax already filled out and you simply just extend upon that.

Once your file is created and you are ready to start creating your tests writing PHPunit is now as simple as creating jUnit code.

public function testExample(){ $this->assertTrue(true); }

That’s all you need to know about testing with Laravel. Thank you Taylor Otwell for making PHP development a delight!

From the blog CS@Worcester – Tyler Lundstrom by CS@Worcester – Tyler Lundstrom and used with permission of the author. All other rights reserved by the author.

Ugh! PHP Unit Testing

I tend to spend a lot of time programming in PHP in my free time. One of the things I have been looking at, since I am taking a software testing class, is PHPUnit and unit testing in PHP. I was getting discouraged with how odd PHPUnit seemed in comparison to the ease of use of something like JUnit. I stumbled upon this blog post that is a conversation between two people about PHPUnit.

The conversation starts with Ed explaining how he doesn’t like PHPUnit specifically:

Ed: I guess realistically my complaints are aimed at PHPUnit . It’s very powerful and very complete from what I can tell, but I think it’s difficult to pick up and I think that difficulty makes people less likely to use it. Because it’s by far the best known testing tool, I think that tends to limit the use of unit testing, period, in PHP.

He goes on to state how other languages have unit testing support that are far easier to use. Java and Python for example have extremely easy to use unit testing frameworks/built in features.

After a little while he continues to explain the reasons he believes PHPUnit is so intimidating:

Ed: I think boilerplate is part of the issue. I think that’s intimidating. Tools can mitigate that to some extent, but I don’t think it eliminates the problem entirely. I just don’t think writing a simple test should be anything more than a couple lines of code.

Previous to that quote the other host, Chris, gave a snippet of a simple unit test and it required 17 lines of code to simply write one assertion. That seems absurd!

At the end of the post Ed notes something that is very important and relates closely to TDD:

To write testable code, you really have to be thinking about testing when you write your code. It takes a bit of time to get used to that, but I think it’s very doable.

The number one reason that PHP developers have a tough time thinking in this manner is due to the fact that most of them are self taught. PHP has a really small learning curve which is great for people who want to get into web development, but in order for PHP unit testing to move forward and to better things, more developers have to get into the TDD mindset.

 

P.S. I find it amusing that in my WordPress (Mainly written in PHP) post editor, the acronym PHP isn’t included in the base dictionary.

From the blog CS@Worcester – Tyler Lundstrom by CS@Worcester – Tyler Lundstrom and used with permission of the author. All other rights reserved by the author.