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.
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.
UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl', 'http://www.newurl') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl','http://www.newurl');
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl', 'http://www.newurl');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl','http://www.newurl');
This is a very common task when developing WordPress sites and there are loads of sites that show this query.
The problem is, I suffer from some kind of Google search blindness and it always takes a few tries to find the right page. Much easier to put it on my own site.
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