Some weeks back I bought an album because of this photo. I chose this album for last weeks AOTW: Carried to Dust by Calexico.
My favorite tracks are:
- The News About William Lyrics
- House of Valparaiso Lyrics
- Two Silver Trees
Some weeks back I bought an album because of this photo. I chose this album for last weeks AOTW: Carried to Dust by Calexico.
My favorite tracks are:
6 more down!
Completely forgot about AOTW again!
Last week I listened a lot to the latest album from Opeth: Watershed. I saw them at Rockefeller with Kristian from work last Tuesday which was great!
My three favorite songs from this album:
As many others I have had some problems with svndumpfilter in the last couple of days. Yesterday I decided to move yet another directory from our old subversion repository to a fresh repository of its own. As I have mentioned earlier I simply tried to issue the following command:
svnadmin dump /path/to/repos | svndumpfilter include rss > rss.dump
This failed with the dreaded svndumpfilter: Invalid copy source path error message. After asking my friend google I found a replacement for svndumpfilter made by my hero of the day Simon Tatham.
After issuing the following command:
svnadmin dump /path/to/repos | ./svndumpfilter2 /path/to/repos rss > rss.dump
everything seemed to be working as expected.
On the way to work today I wrote some more unit tests for our internal framework. This time it was for a component that we use to fetch remote content. The component is called VG_Wget and one of its features is something called availability caching. What that does is that it caches the content of an URL in case it is unavailable the next time the content is fetched. There is a “regular” caching layer as well that can be enabled but the availability cache is only used when the external resource is unavailable.
I came up with a nice solution on how to test this, and since I’m such a nice guy I’ll share it with the lot of you.
Here is how we use the component:
<?php
require_once 'VG/Wget.php';
$wget = new VG_Wget();
$content = $wget->get('http://localhost/file.php'');
Since I know the contents of file.php I can easily check that the get method works as expected. But how can I make a file unavailable on the next request automagically? I’m glad you asked!
First I wrote a script that dynamically creates another php script, which in turn create some output, and then deletes itself.
The create.php script looks like this:
<?php // Generate a filename and create the file $filename = microtime(true) . '.php'; $fp = fopen($filename, 'w'); // String to write to the file $contents = '<?php print(\'please cache me\'); unlink(__FILE__);'; fwrite($fp, $contents); // Close the file fclose($fp); // Output the filename of the tmp file print($filename);
First we generate a unique filename, and then create a file with the following content:
<?php
print('please cache me');
unlink(__FILE__);
After the file is created we output the unique filename so we know which file to fetch when testing the availability cache mechanism. When we access that file in a test the file will output “please cache me” and then self destruct. The next time we access that file the VG_Wget component will receive an HTTP 404, and the availability cache will be used instead.
The test method looks something like this:
<?php
class VG_Wget_WgetTest extends PHPUnit_Framework_TestCase {
/**
* Wget object
*
* @var VG_Wget
*/
protected $wget = null;
/**
* Setup method
*/
public function setUp() {
$this->wget = new VG_Wget();
}
/**
* Teardown method
*/
public function tearDown() {
$this->wget = null;
}
/**
* Test method
*/
public function testGetFileUsingAvailabilityCache() {
// Base url
$url = 'http://localhost/';
// Call an url that creates a tmp file that is deleted after first request
$tmpFilename = trim(file_get_contents($url . 'create.php'));
$url .= $tmpFilename;
// Fetch the file effectively deleting it so that the next request fails
$content = trim($this->wget->get($url));
// Fetch it again (this time the cache will be used)
$content2 = trim($this->wget->get($url));
$this->assertSame('please cache me', $content);
$this->assertSame($content, $content2);
}
}
I have removed some logic from the test method but you can see roughly how I test the component. Comments anyone?
First a small warning: The solution presented in this post is not very secure and is prone to errors. It’s a quick and dirty solution to a simple problem. But since we all like to get dirty sometimes I’ll present it to you anyway.
Naveed at work is making a small chat application and wanted to store some information about which users were banned from the chat. This feature needed to be done relatively fast and since the chat does not use a database (apc only) we did not want to use a database to store this information either. I figured we could implement it fast by storing it in a regular php array.
The “database” might look like this:
<?php return array(123 => true, 456 => true, 789 => true);
As you can see we have three banned users. The keys in the array represent the IDs of these users. In the chat application we fetch the database by doing:
<?php // Fetch the "database" containing the array $bannedUsers = require 'database.php';
Now, if we want to ban another user we can do something like:
<?php
$bannedUsers = require 'database.php';
if (!is_array($bannedUsers)) {
$bannedUsers = array();
}
// Ban user with id == 234
$bannedUsers[234] = true;
// Update "database"
file_put_contents('database.php', '<?php return ' . var_export($bannedUsers, true) . ';');
And thats that. A quick and dirty solution. :)
On my way home from work today I wrote some unit tests for some components of an internal framework we have at work. Some of the components produce small chunks of HTML, be it the header of a page, the main menu, or an ad.
There are probably loads of ways to write unit tests that enables you to test the output from such a component. I thought I could share with you how I decided to do it, and hopefully someone out there might benefit from it..
One of the components I wrote a tests for produces several <ul> lists with some nested <ol> lists. Lets imagine the output would look something like this:
<div id="menu">
<ul id="news">
<li>
main 1
<ol id="business" class="active">
<li>sub 1.1</li>
<li>sub 1.2</li>
</ol>
</li>
<li>
main 2
<ol id="politics">
<li>sub 2.1</li>
<li>sub 2.2</li>
</ol>
</li>
</ul>
<ul id="sports">
<li>
main 3
<ol id="football">
<li>sub 3.1</li>
<li>sub 3.2</li>
</ol>
</li>
<li>
main 4
<ol id="golf">
<li>sub 4.1</li>
<li>sub 4.2</li>
</ol>
</li>
</ul>
</div>
By default all the nested <ol> lists are retracted except the first one. The list that we want to have expanded is given a css class called active. Here is how we use the component:
<?php
// Print the default menu (with the first <ol> expanded)
$menu = new VG_Page_Element_Menu();
print($menu);
// Print the menu with the "golf" <ol> expanded
$options = array('activeSection' => VG_Page_Element_Menu::SECTION_GOLF);
$menu = new VG_Page_Element_Menu($options);
print($menu);
Now, how to test this? I used SimpleXML and xpath for this case. My test class looks something like this:
<?php
class VG_Page_Element_Menu_MenuTest extends PHPUnit_Framework_TestCase {
/**
* Local menu element
*
* @var VG_Page_Element_Menu
*/
protected $element = null;
/**
* Set up method
*
* Create a fresh menu element before every test
*/
public function setUp() {
$this->element = new VG_Page_Element_Menu();
}
/**
* Tear down method
*
* Destroy the menu element after every test
*/
public function tearDonw() {
$this->element = null;
}
/**
* Test the default output
*/
public function testRenderDefaultMenu() {
$menu = simplexml_load_string((string) $this->element);
$activeSection = $menu->xpath('/div/ul[@id="news"]/li/ol[@id="business" and @class="active"]');
// Assert that we have one hit
$this->assertSame(1, count($activeSection));
// Assert that there is only one expanded section in total
$activeSections = $menu->xpath('/div/ul/li/ol[@class="active"]');
$this->assertSame(1, count($activeSections));
}
/**
* Test output with other section expanded
*/
public function testRenderNonDefaultMenuWithExpandedSection() {
$section = VG_Page_Element_Menu::SECTION_GOLF;
$this->element->setOption('activeSection', $section);
$menu = simplexml_load_string((string) $this->element);
$activeSection = $menu->xpath('/div/ul[@id="sports"]/li/ol[@id="' . $section . '" and @class="active"]');
// Assert that we have one hit
$this->assertSame(1, count($activeSection));
// Assert that there is only one expanded section in total
$activeSections = $menu->xpath('/div/ul/li/ol[@class="active"]');
$this->assertSame(1, count($activeSections));
}
}
In the first test I look for an <ol> element with id == "business" and class == "active", inside an <li> element, inside an <ul> element with id == "news", inside a <div>. I do a similar check in the other test but with some other values for the id’s of the two lists.
As I mentioned there are probably loads of ways to solve this, but I happened to like this approach. Comments anyone?
Forgot this last week. The award goes to one of my favorite death metal albums: Individual Thought Patterns from 1993 by Death.
My three favorite tracks are:
|