Assertions within PHPUnit SetUp/ TearDown

PHPUnit includes setUp() and tearDown() methods which are called for each test method. These are used to create pre-conditions for test methods or set up external resources used by each test.

It’s useful to note that assertions can be called within these methods to test if they have worked correctly, for example creating and then removing some database settings:

public function setUp()
{
	$this->db->set( 'foo', 'foo' );

	$this->db->set( 'bar', 'bar' );

	$this->assertSame( 2, $this->db->count() );
}



public function tearDown()
{
	$this->db->delete( 'foo' );

	$this->db->delete( 'bar' );

	$this->assertSame( 0, $this->options->count() );
}

Of course, if the assertions fail they’ll be repeated for every test method, but that’s probably better than wasting time investigating why tests are failing when it is setUp() that is the problem.

They can also be used to ensure that the tearDown() method has worked and doesn’t leave something persistent to interfere with subsequent tests.

Hooks Trait

This PHP trait provides methods for adding ‘hooks’ into a class, similar to how WordPress actions and hooks work: https://bitbucket.org/dgifford/hooks-trait

Initially, I needed this to modify some legacy code, but it could be useful in other classes where you want to intercept and modify variables within standard methods.

Perhaps needing this trait is a ‘code smell’ but sometimes needs must.

 

Tag PHP class for dynamically generating HTML

This is a utility class which I’ve used in a basic form for a while now to quickly generate HTML programmatically in PHP.

The idea behind it is that I wanted a fairly simple object that would represent an HTML tag element.  Once created, the content and attributes can be changed dynamically and when ready, a render() method can be called to output the HTML.

I know there are loads of templating classes out there and you could use the built in PHP XML functions to do this, but they are overkill for what I want. This is particularly useful for rapid prototyping and testing. You can quickly create HTML output without having to actually write any HTML. Plus this is an interesting programming exercise.

So far, it’s pretty simple:

$tag = new Tag( 'div', ['class' => 'my_class'], 'my content' );

$tag->render(); // outputs <div class="my_class">my content</>

$tag->setAttribute('class' => 'new_class');

$tag->render(); // outputs <div class="new_class">my content</>

The next step is to make it recursive and then add some kind of find/ search method to target embedded Tags.

The project is here: https://bitbucket.org/dgifford/tag

Array helpers trait

I’ve collected a number of utility functions for working with arrays over the years, mixed in with collections of ‘helper’ functions used in various projects and added to various classes.

They include testing for multi-dimensional arrays, obtaining a hash of an array and flattening arrays so they only contain values.

The way they were getting repeated in classes made me think they should be a trait: https://bitbucket.org/dgifford/array-helpers-trait