views:

182

answers:

1

Hi!

I'm trying my hand at TDD with PHP and am writing a webbased app to access articles in a MySQL database; this is the test function:

class TestArticleTestCase extends UnitTestCase {

...

public function testArticleGenerateInsertSqlString() {
    $testArticle = new Article("12345", "2009-09-13 20:20:20", "Test heading", "Test text");

    ...

}

and this is the Article class:

class Article {
    private $_articleId;
    private $_pubDate;
    private $_heading;
    private $_text;

    public function __construct($articleId, $pubDateUnchecked, $headingUnescaped, $textUnescaped) {
     echo "pubDateUnchecked == $pubDateUnchecked </BR>";
            ...

 }

I included the echo in the constructor because the dates in the database was not what I initialised the Article with, and sure enough, tracing the problem, this is the output of that echo in the constructor:

pubDateUnchecked == 2005-06-01 12:00:00

Maybe I've just stared at this code too long, but how can the string change from where I instantiate it to directly where it gets instantiated, BEFORE I start manipulating it as a date (I check that it's in am allowable date format with strtotime() and date() later on..).

Does anyone have any ideas on where to look?

Thank you, Stephan.

A: 

Maybe a cache problem? Or you've edited the wrong file? Has happened before ;-)
A debugger would be helpful in this case. But if you don't have/can't install one try something like

public function testArticleGenerateInsertSqlString() {
  $testdata = array(
    array('articleId'=>"12345", 'pubDateUnchecked'=>"2009-09-13 20:20:20", 'headingUnescaped'=>"Test heading", 'textUnescaped'=>"Test text")
  );
  echo '<div>Test. Now=', date('Y-m-d H:i:s'),' modtime(__FILE__)=', date('Y-m-d H:i:s', filemtime(__FILE__)), "</div>\n";
  foreach( $testdata as $t ) {
    echo "<div>Test. new Article({$t['articleId']}, {$t['pubDateUnchecked']}, {$t['headingUnescaped']}, {$t['textUnescaped']})</div>";
    $testArticle = new Article($t['articleId'], $t['pubDateUnchecked'], $t['headingUnescaped'], $t['textUnescaped']);
VolkerK
Thank you so much for that test code VolkerK! I ran it and found the problem to be in the line if ( $timestamp = strtotime($pubDateUnchecked) === false ) { throw new Exception('Invalid pubDate. Date must be e.g. YYYY-MM-DD H:M:S'); } which produced "1970-01-01 02:00:00" consistently. Surrounding the assignment with ()'s solved the problem..Dankie!
gouwsmeister