2009-08-19 84 views
0

我試圖用TDD與PHP並且正在編寫一個基於web的應用程序來訪問MySQL數據庫中的文章;這是測試功能:PHP:__construct的字符串參數沒有正確傳遞

class TestArticleTestCase extends UnitTestCase { 

... 

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

    ... 

} 

,這是文章類:

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

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

} 

我包含在構造函數中的回聲,因爲在數據庫中的日期是不是我初始化與文章,和果然,追查問題,這是回聲的構造函數中的輸出:

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

也許我只是盯着這段代碼太長了,但是如何將字符串從我實例化的地方改變到實例化的地方,在我開始將它作爲日期操作之前(我檢查它是否在允許的日期格式與strtotime()和日期()稍後..)。

有沒有人有什麼想法在哪裏看?

謝謝, 斯蒂芬。

+0

你確定嗎?我在這段代碼中看不到任何錯誤 – knittl 2009-08-19 10:35:28

回答

0

也許緩存問題?或者你編輯了錯誤的文件?之前已經發生;-)
在這種情況下,調試器會很有幫助。但如果你沒有/不能安裝一個像

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']); 
+0

非常感謝那個測試代碼VolkerK!我跑了它,發現問題在 if($ timestamp = strtotime($ pubDateUnchecked)=== false){ 拋出新的異常('Invalid pubDate。Date必須是例如YYYY-MM-DD H:女士'); } \t 它一直產生「1970-01-01 02:00:00」。 ()的作業周圍解決了這個問題.. Dankie! – gouwsmeister 2009-08-19 14:18:01