String Representation of XML Objects in PHP

There’s got to be an easier way to do this.

Perhaps I am unsophisticated. But sometimes when I am debugging I just want to print strings to see what is going on. When working with PHP’s DOM XML stuff, this is difficult. var_dump and print_r don’t do what I’d like. What I really want is just to see the XML of the DOMDocument or the DOMElement or the node list or whatever it is I happen to have in my variable (I may not even know).

There may be a much easier way to get a string representation of an arbitrary XML object in PHP. If so, please link it up in the comments. Failing that, here’s a rough pass at the kind of function I need:

    function printXml($xml) {
        $s = self::xmlToString($xml);
        print "<pre>" . htmlentities($s) . "</pre>";
    }

    function xmlToString($xml) {
        if ($xml instanceof DOMDocument) {
            $s = $xml->saveXml();
        } else if ($xml->length) {
            $s = '';
            foreach ($xml as $element) {
                $s .= self::xmlToString($element);
            }
        } else {
            $s = self::xmlToStringProper($xml);
        }
        return $s;
    }

    function xmlToStringProper($node) {
        $dom = new DOMDocument();
        $xmlContent = $dom->importNode($node, true);
        $dom->appendChild($xmlContent);
        return $dom->saveXml();
    }

History Meme

11:41:13 bakert@bluebones:~$  history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
4611 vi
3728 u
3367 cd
2817 ruby
1832 ls
1589 svn
1230 td
852 mysql
790 ssh
635 gd

My First Program

I wrote my first programs at the age of 8. They are preserved on Tom 1, a 15 minute cassette tape. It starts with copied listings from the ZX Spectrum Introduction book. But before long there is a program called ‘askage’ which I’m pretty sure is an original composition. Here’s the code in it’s entirety:

10 PRINT "How old are you?"
20 INPUT A$
30 PRINT "Really? You look much older than that!" 

Programmatically Find Tests With PHPUnit

Nothing annoys me more than having to manually add tests to a central location in order to get them to run. Here’s some code that automatically and recursively (down the directory tree) finds files ending in Test.php and loads the tests within. Now all you need to do is create the tests.

< ?php

require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/TextUI/TestRunner.php';

define(Test, '/path/to/test/dir');

class AllTests {
    public static function main() {
        PHPUnit_TextUI_TestRunner::run(self::suite());
    }

    public static function suite() {
        $suite = new PHPUnit_Framework_TestSuite('My Test Suite');
        foreach (self::find_all_tests(TEST) as $path) {
            require_once($path);
            $class = preg_replace('%.*/(.*)\.php%', '$1', $path);
            $suite->addTestSuite($class);
        }
        return $suite;
    }

    private static function find_all_tests($start_dir) {
        $res = array();
        foreach (glob("$start_dir/*Test.php") as $path) {
            $res[] = $path;
        }
        foreach (glob($start_dir . '/*', GLOB_ONLYDIR) as $subdir) {
            $res = array_merge($res, self::find_all_tests($subdir));
        }
        return $res;
    }
}

?>